Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell unicorn to understand Heroku's signals?

Perhaps you've seen this...

2012-03-07T15:36:25+00:00 heroku[web.1]: Stopping process with SIGTERM
2012-03-07T15:36:36+00:00 heroku[web.1]: Stopping process with SIGKILL
2012-03-07T15:36:36+00:00 heroku[web.1]: Error R12 (Exit timeout) -> Process failed to exit within 10 seconds of SIGTERM
2012-03-07T15:36:38+00:00 heroku[web.1]: Process exited with status 137

This is a well known problem when running unicorn on heroku...

  • heroku uses SIGTERM for graceful shutdown
  • unicorn uses SIGTERM for quick shutdown

Can I tell heroku to send SIGQUIT? Or can I tell unicorn to treat SIGTERM as graceful shutdown?

like image 609
Seamus Abshere Avatar asked Mar 07 '12 16:03

Seamus Abshere


1 Answers

Heroku now provides instruction for this here: https://blog.heroku.com/archives/2013/2/27/unicorn_rails

Their suggested unicorn.rb file is:

# config/unicorn.rb
worker_processes 3
timeout 30
preload_app true

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end
like image 187
Clay Avatar answered Oct 25 '22 05:10

Clay