Last active
July 13, 2020 13:17
-
-
Save drush/9796013 to your computer and use it in GitHub Desktop.
Run Delayed Job Worker as a Thread in Web Process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Only run in server process, not console or rake tasks | |
if !Rails.const_defined?('Console') && !($0 =~ /rake$/) && !Rails.env.test? | |
Rails.application.config.after_initialize do | |
(1..2).each do |thread_id| | |
Thread.new { | |
Thread.current[:thread_name] = "DJ Web Worker Thread #{thread_id}" | |
ActiveRecord::Base.connection_pool.with_connection do |conn| | |
dj = Delayed::Worker.new | |
Rails.logger.warn "Starting #{Thread.current[:thread_name]}" | |
at_exit { Rails.logger.warn "Stopping background thread"; dj.stop } | |
dj.start | |
end | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wondering if there were any gotchas or side-effects?