Skip to content

SlidesLive/delayed_job_worker_pool

 
 

Repository files navigation

Delayed Job Worker Pool

Gem Version Build Status Code Climate

Delayed Job's built-in worker pooling daemonizes all worker processes. This is great for certain environments but not so great for environments like Heroku that really want your processes to run in the foreground. Delayed Job Worker Pool runs a pool of Delayed Job workers without daemonizing them.

Salsify is currently using Delayed Job Worker Pool to run multiple Delayed Job workers on a single Heroku PX dyno. Read more about our experience using this gem on our blog.

This gem only works with MRI on Linux/MacOS X.

Installation

Add this line to your application's Gemfile:

gem 'delayed_job_worker_pool'

And then execute:

$ bundle

Or install it yourself as:

$ gem install delayed_job_worker_pool

Usage

From your Rails root directory run:

delayed_job_worker_pool <config file>

The config file is a Ruby DSL inspired by the Puma configuration DSL. Here's an example:

workers Integer(ENV['NUM_WORKERS'] || 1)
queues (ENV['QUEUES'] || ENV['QUEUE'] || '').split(',')
sleep_delay ENV['WORKER_SLEEP_DELAY']

preload_app

# This runs in the master process after it preloads the app
after_preload_app do
  puts "Master #{Process.pid} preloaded app"
  
  # Don't hang on to database connections from the master after we've 
  # completed initialization
  ActiveRecord::Base.connection_pool.disconnect!
end

# This runs in the worker processes after it has been forked
on_worker_boot do |worker_info|
  puts "Worker #{Process.pid} started"
  
  # Reconnect to the database
  ActiveRecord::Base.establish_connection
end

# This runs in the master process after a worker starts
after_worker_boot do |worker_info|
  puts "Master #{Process.pid} booted worker #{worker_info.name} with " \
        "process id #{worker_info.process_id}"
end

# This runs in the master process after a worker shuts down
after_worker_shutdown do |worker_info|
  puts "Master #{Process.pid} detected dead worker #{worker_info.name} " \
        "with process id #{worker_info.process_id}"
end

Here's more information on each setting:

  • workers - The number of Delayed Job worker processes to fork. The master process will relaunch workers that fail.
  • Delayed Job worker settings (queues, min_priority, max_priority, sleep_delay, read_ahead) - These are passed through to the Delayed Job worker.
  • preload_app - This forces the master process to load Rails before forking worker processes causing the memory consumed by the code to be shared between workers. If you use this setting make sure you re-establish any necessary connections in the on_worker_boot callback.
  • after_preload_app - A callback that runs in the master process after preloading the app but before forking any workers.
  • on_worker_boot - A callback that runs in the worker process after it has been forked.
  • after_worker_boot - A callback that runs in the master process after a worker has been forked.
  • after_worker_shutdown - A callback that runs in the master process after a worker has been shutdown.

All settings are optional and nil values are ignored.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/salsify/delayed_job_worker_pool.

License

The gem is available as open source under the terms of the MIT License.

About

Worker process pooling for Delayed Job

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 85.4%
  • HTML 11.6%
  • CSS 1.6%
  • JavaScript 1.4%