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
@pablitoc
Copy link
Author

I want to achieve something like this for a standard unicorn config. It is not done but I wanted to ask you if doing #{application} was feasible in the unicorn.rb file. Ill be doing some more you-googlizing. 😄 👍

@pjkelly
Copy link

pjkelly commented Aug 22, 2014

@pablitoc, this will work in terms of variable interpolation, but you'd need to define application first.

Also, this particular configuration is server-specific, we couldn't use this locally in development. How were you thinking of deploying this file? As part of the repo or provisioned by Ansible?

@pjkelly
Copy link

pjkelly commented Aug 22, 2014

Here's an example with the variable definition. Have we ever tried loading dotenv like this?

require 'dotenv'

application = 'pristine'

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

@pablitoc
Copy link
Author

I was thinking with the app, but if we deploy via Ansible then I can make a role out of it and use the jinja2 variable syntax and get rid of the #{application}. Just looking into options so that we do not need to keep changing the unicorn config.

@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