Skip to content

Instantly share code, notes, and snippets.

@slopeofhope81
Forked from drush/delayed_job_threads.rb
Created July 13, 2020 13:17
Show Gist options
  • Save slopeofhope81/e18d2de741aac960182bb937b1646d9f to your computer and use it in GitHub Desktop.
Save slopeofhope81/e18d2de741aac960182bb937b1646d9f 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment