Skip to content

Instantly share code, notes, and snippets.

@omarqureshi
Created February 18, 2013 10:09
Show Gist options
  • Save omarqureshi/4976373 to your computer and use it in GitHub Desktop.
Save omarqureshi/4976373 to your computer and use it in GitHub Desktop.
BASE_PATH = "<%= _base_path %>"
working_directory "#{BASE_PATH}/current"
pid "#{BASE_PATH}/shared/pids/unicorn.pid"
stderr_path "#{BASE_PATH}/shared/log/unicorn.stderr.log"
stdout_path "#{BASE_PATH}/shared/log/unicorn.stdout.log"
listen 3000
worker_processes <%= _worker_processes %>
timeout 30
GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
preload_app true
before_fork do |server, worker|
# Disconnect since the database connection will not carry over
if defined? ActiveRecord::Base
ActiveRecord::Base.connection.disconnect!
end
# Quit the old unicorn process
old_pid = "#{server.config[: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
after_fork do |server, worker|
GC.disable
# Start up the database connection again in the worker
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
child_pid = server.config[:pid].sub(".pid", ".#{worker.nr}.pid")
system("echo #{Process.pid} > #{child_pid}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment