Skip to content

Instantly share code, notes, and snippets.

@pablitoc
Created August 22, 2014 19:12
Show Gist options
  • Save pablitoc/22a56736931c548f2667 to your computer and use it in GitHub Desktop.
Save pablitoc/22a56736931c548f2667 to your computer and use it in GitHub Desktop.
Unicorn Config
require 'dotenv'
worker_processes Integer(ENV['WEB_CONCURRENCY'] || 3)
timeout 15
preload_app true
stderr_path "/srv/#{application}/shared/log/"ENV['RAILS_ENV']".log"
stdout_path "/srv/#{application}/shared/log/"ENV['RAILS_ENV']".log"
pid "/srv/#{application}/shared/tmp/pids/#{application}.pid"
before_fork do |server, worker|
f = File.open("#{server.config[:pid]}.lock", 'w')
exit unless f.flock(File::LOCK_SH)
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
@pjkelly
Copy link

pjkelly commented Aug 22, 2014

Ok, how about something like this then? It would be ideal if we could keep this in the app repo (just like sidekiq.yml) because it's easier to make changes if need-be. Consider this a rough-draft...

require 'dotenv'

worker_processes Integer(ENV['UNICORN_CONCURRENCY'] || 3)
timeout Integer(ENV['UNICORN_TIMEOUT'] || 15)
preload_app true

stderr_path ENV['UNICORN_LOG'] if ENV['UNICORN_LOG']
stdout_path ENV['UNICORN_LOG'] if ENV['UNICORN_LOG']
pid ENV['UNICORN_LOG'] if ENV['UNICORN_PID']

before_fork do |server, worker|
  if ENV['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

@pablitoc
Copy link
Author

looks awesome. However, can you explain what
stderr_path ENV['UNICORN_LOG'] if ENV['UNICORN_LOG']
stdout_path ENV['UNICORN_LOG'] if ENV['UNICORN_LOG']
pid ENV['UNICORN_LOG'] if ENV['UNICORN_PID']
does? That is confusing to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment