Created
December 1, 2014 08:35
-
-
Save tsechingho/e6de3ab9a86194c2a7a4 to your computer and use it in GitHub Desktop.
unicorn example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## development | |
# app_path = `pwd`.strip | |
# app_shared_path = app_path | |
# app_current_path = app_path | |
# master_port = 4000 | |
## production | |
app_path = '/srv/deploy/ufolio' | |
app_shared_path = "#{app_path}/shared" | |
app_current_path = "#{app_path}/current" | |
master_port = 4090 | |
# at least equal "cat /proc/cpuinfo | grep vendor_id | wc -l" | |
worker_processes 2 | |
working_directory app_current_path | |
listen "#{app_shared_path}/tmp/sockets/unicorn.sock", backlog: 64 | |
listen master_port, tcp_nopush: true | |
timeout 30 | |
pid "#{app_shared_path}/tmp/pids/unicorn.pid" | |
stderr_path "#{app_shared_path}/log/unicorn.stderr.log" | |
stdout_path "#{app_shared_path}/log/unicorn.stdout.log" | |
preload_app true | |
GC.respond_to?(:copy_on_write_friendly=) and | |
GC.copy_on_write_friendly = true | |
check_client_connection false | |
# http://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-before_exec | |
before_exec do |server| | |
ENV['BUNDLE_GEMFILE'] ||= "#{app_current_path}/Gemfile" | |
end | |
before_fork do |server, worker| | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.connection.disconnect! | |
old_pid = "#{server.config[:pid]}.oldbin" | |
if old_pid != server.pid | |
begin | |
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU | |
Process.kill(sig, File.read(old_pid).to_i) | |
rescue Errno::ENOENT, Errno::ESRCH | |
end | |
end | |
sleep 1 | |
end | |
after_fork do |server, worker| | |
worker_address = "127.0.0.1:#{master_port + worker.nr + 1}" | |
server.listen(worker_address, tries: -1, delay: 5, tcp_nopush: true) | |
worker_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid") | |
system "echo #{Process.pid} > #{worker_pid}" | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.establish_connection | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment