Skip to content

Instantly share code, notes, and snippets.

@blotto
Last active August 29, 2015 14:02
Show Gist options
  • Save blotto/8005531b9c94f3732240 to your computer and use it in GitHub Desktop.
Save blotto/8005531b9c94f3732240 to your computer and use it in GitHub Desktop.
Sample Unicorn.rb File
#see https://devcenter.heroku.com/articles/rails-unicorn#the-unicorn-server
worker_processes ENV['WEB_CONCURRENCY'].to_i > 0 ? ENV['WEB_CONCURRENCY'].to_i : 3
timeout ENV['REQUEST_TIMEOUT'].to_i > 0 ? ENV['REQUEST_TIMEOUT'].to_i : 15
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
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
Rails.logger.info('Disconnected from ActiveRecord')
end
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
if defined?(ActiveRecord::Base)
config = Rails.application.config.database_configuration[Rails.env]
config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10
config['pool'] = ENV['DB_POOL'] || 2
ActiveRecord::Base.establish_connection(config)
Rails.logger.info('Connected to ActiveRecord')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment