Skip to content

Instantly share code, notes, and snippets.

@pjkelly
Forked from pablitoc/unicorn.rb
Last active April 19, 2017 09:27
Show Gist options
  • Save pjkelly/1122cc189c7525a8bb2e to your computer and use it in GitHub Desktop.
Save pjkelly/1122cc189c7525a8bb2e to your computer and use it in GitHub Desktop.
require 'dotenv'
Dotenv.load
listen ENV.fetch('UNICORN_PORT', 5000), :backlog => ENV.fetch('UNICORN_BACKLOG', 200).to_i
worker_processes ENV.fetch('UNICORN_CONCURRENCY', 3).to_i
timeout ENV.fetch('UNICORN_TIMEOUT', 15).to_i
preload_app true
if ENV.include?('UNICORN_LOG')
stderr_path ENV.fetch('UNICORN_LOG')
stdout_path ENV.fetch('UNICORN_LOG')
end
pid ENV.fetch('UNICORN_PID') if ENV.include?('UNICORN_PID')
before_fork do |server, worker|
if ENV.include?('UNICORN_PID')
f = File.open("#{server.config[:pid]}.lock", 'w')
exit unless f.flock(File::LOCK_SH)
end
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment