Skip to content

Instantly share code, notes, and snippets.

@inspire22
Created June 18, 2013 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inspire22/f82c77c0a465f1945305 to your computer and use it in GitHub Desktop.
Save inspire22/f82c77c0a465f1945305 to your computer and use it in GitHub Desktop.
rails_env = ENV['RAILS_ENV'] || 'development'
# thorn is having some swap problems, lowinger # servers to 4 since it's running two sites now
num_workers = 6
worker_processes num_workers
preload_app true
timeout 20
if `hostname` =~ /rose|everybean/
listen '/tmp/pro.unicorn.sock', :backlog => 128
else
listen 3001, :backlog => 128
end
app_base = "/u/apps/ap.pro"
stderr_path "/tmp/pro.err.unicorn"
stdout_path "/tmp/pro.out.unicorn"
pid "#{app_base}/shared/pids/pro.unicorn.pid"
working_directory "#{app_base}/current"
user 'kevin', 'kevin' if `hostname` !~ /everybean|duke/
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{app_base}/current/Gemfile"
end
before_fork do |server, worker|
# USR2 = restart gracefully
# old_pid = '/tmp/pro.unicorn.pid.oldbin'
old_pid = "#{app_base}/shared/pids/pro.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
ActiveRecord::Base.connection.disconnect!
end
# runs in the 'ruby' environment/etc
after_fork do |server, worker|
# reconnect db
db = $cache['db']
if Rails.env.production?
c = $cache.get('db') || 'memory'
# d "new unicorn connecting to #{c}"
ActiveRecord::Base.establish_connection c
$db_current = c
elsif Rails.env.development?
ActiveRecord::Base.establish_connection( 'development' )
end
begin
Rails.cache.reset
$cache.reset
rescue Exception => e
d "rescued unicorn exception #{e.class} #{e.message}"
end
end
# will just this be enough? Hmm, do some tests w/ adding + removing gems?
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "/u/apps/ap.pro/current/Gemfile"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment