Skip to content

Instantly share code, notes, and snippets.

@manchunlam
Last active September 28, 2015 13:08
Show Gist options
  • Save manchunlam/1443417 to your computer and use it in GitHub Desktop.
Save manchunlam/1443417 to your computer and use it in GitHub Desktop.
Sample Unicorn Configuration File
# _*_ coding: utf-8 _*_
require 'yaml'
require 'erb'
# source: http://d.hatena.ne.jp/milk1000cc/20100804/1280893810
# Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches
worker_processes 4
# Make sure unicorn worker process doesn't kill itself if page takes longer to
# load
timeout 60
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
# this will evaluate to something like "/Users/joelam/Projects/Vitrue/tabs"
# contributed by Kyle Bock
rails_root = File.expand_path('../..', __FILE__)
working_directory rails_root
# listen on a Unix domain socket
# directory path_to_project/tmp/sockets must exist
listen File.expand_path('tmp/sockets/unicorn.sock', ENV['RAILS_ROOT']), :backlog => 64
# pid
# directory path_to_project/tmp/pids must exist
pid File.expand_path('tmp/pids/unicorn.pid', ENV['RAILS_ROOT'])
# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
# combine REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{ server.config[:pid] }.oldbin"
unless old_pid == server.pid
begin
# SIGTTOU だと worker_processes が多いときおかしい気がする
Process.kill :QUIT, File.read(old_pid).to_i
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
after_fork do |server, worker|
if defined?(Rails)
ActiveRecord::Base.establish_connection
else
if defined?(ActiveRecord::Base)
env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
path = File.expand_path('../database.yml', __FILE__)
file = File.open(path, 'r')
template = ERB.new(file.read).result(binding)
config = YAML.load(template).fetch(env, {})
ActiveRecord::Base.establish_connection(config)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment