Skip to content

Instantly share code, notes, and snippets.

@sentientmonkey
Created April 8, 2011 00:52
Show Gist options
  • Save sentientmonkey/909099 to your computer and use it in GitHub Desktop.
Save sentientmonkey/909099 to your computer and use it in GitHub Desktop.
This is our unicorn config! It's magical!
worker_processes 6
preload_app true
timeout 180
listen 8100
working_directory "/opt/teachstreet/web-app/current"
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
after_fork do |server, worker|
ActiveRecord::Base.establish_connection
##
# Unicorn master is started as root, which is fine, but let's
# drop the workers to www-data:www-data
begin
uid, gid = Process.euid, Process.egid
user, group = 'www-data', 'www-data'
target_uid = Etc.getpwnam(user).uid
target_gid = Etc.getgrnam(group).gid
worker.tmp.chown(target_uid, target_gid)
if uid != target_uid || gid != target_gid
Process.initgroups(user, target_gid)
Process::GID.change_privilege(target_gid)
Process::UID.change_privilege(target_uid)
end
rescue => e
if RAILS_ENV == 'development'
STDERR.puts "couldn't change user, oh well"
else
raise e
end
end
end
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!
old_pid = RAILS_ROOT + '/tmp/pids/unicorn.pid.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment