Skip to content

Instantly share code, notes, and snippets.

@drush
Last active July 13, 2020 13:17
Show Gist options
  • Save drush/9796013 to your computer and use it in GitHub Desktop.
Save drush/9796013 to your computer and use it in GitHub Desktop.
Run Delayed Job Worker as a Thread in Web Process
# 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
@lulalala
Copy link

lulalala commented Feb 4, 2018

Wondering if there were any gotchas or side-effects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment