Unicorn.rb, Heroku, Delayed_Job config
I am successfully using Unicorn server and Delayed_Job on Heroku for my site. However, I'm not sure if it will set up a better way, and I would like more information on how to view workflows, etc. My config / unicorn.rb file that works is pasted below:
worker_processes 3
preload_app true
timeout 30
# setting the below code because of the preload_app true setting above:
# http://unicorn.bogomips.org/Unicorn/Configurator.html#preload_app-method
@delayed_job_pid = nil
before_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
Rails.logger.info('Disconnected from ActiveRecord')
# start the delayed_job worker queue in Unicorn, use " -n 2 " to start 2 workers
if Rails.env == "production"
# @delayed_job_pid ||= spawn("RAILS_ENV=production ../script/delayed_job start")
# @delayed_job_pid ||= spawn("RAILS_ENV=production #{Rails.root.join('script/delayed_job')} start")
@delayed_job_pid ||= spawn("bundle exec rake jobs:work")
elsif Rails.env == "development"
@delayed_job_pid ||= spawn("script/delayed_job start")
# @delayed_job_pid ||= spawn("rake jobs:work")
end
end
end
after_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
Rails.logger.info('Connected to ActiveRecord')
end
end
- delayed_job says to use
RAILS_ENV=production script/delayed_job start
to start worker processes in production mode, but if I use this command I get "file not found" errors in Heroku. So, at the moment I'm usingbundle exec rake jobs:work
in production, which seems to work, but is it correct? - How many processes are actually running in this setup and can it be optimized? I am assuming there is 1 Unicorn master process, 3 web workers and 1 superseded worker, for a total of 5? When I run in dev mode locally I see 5 ruby pids being spawned. It might be better to use only 2 web workers and then give 2 workers a Delayed_job (I have pretty low traffic).
- It all runs in one Heroku dinar, so I have no idea how to check the status of Unicorn workers, any idea how?
** Note I have commented out the lines that break the site in production because Heroku says it "can't find the file"
source to share
Yours config/unicorn.rb
shouldn't spawn such DJ workers. You must specify a separate workflow in the Procfile
following way:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: bundle exec rake jobs:work
You can use foreman
for local development to deploy both Unicorn and DJ. Then the resulting file config/unicorn.rb
will be simpler:
worker_processes 3
preload_app true
timeout 30
# setting the below code because of the preload_app true setting above:
# http://unicorn.bogomips.org/Unicorn/Configurator.html#preload_app-method
before_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
Rails.logger.info('Disconnected from ActiveRecord')
end
end
after_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
Rails.logger.info('Connected to ActiveRecord')
end
end
As I mentioned in the comments, you are spawning child processes that you never reap and will most likely become zombies. Even though you've added code to try and account for this, you're still trying to get single dinos to serve multiple roles (web and background worker) and will likely cause future problems (memory errors, etc.).
source to share