Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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