aboutsummaryrefslogtreecommitdiff
path: root/nginx_unicorn/unicorn.rb
diff options
context:
space:
mode:
authorMiquel Sabaté <mikisabate@gmail.com>2011-11-04 20:48:16 +0100
committerMiquel Sabaté <mikisabate@gmail.com>2011-11-04 20:48:16 +0100
commit93de9728efb0e689ce413e980dae685ac0b4b9c3 (patch)
tree18bbb262eeeab567f2438d38c7f8134391a67ad8 /nginx_unicorn/unicorn.rb
parent173bf566f6a82d578780fad9b14b42f20b704161 (diff)
downloaddotfiles-93de9728efb0e689ce413e980dae685ac0b4b9c3.tar.gz
dotfiles-93de9728efb0e689ce413e980dae685ac0b4b9c3.zip
Added my nginx/unicorn configuration
Diffstat (limited to 'nginx_unicorn/unicorn.rb')
-rw-r--r--nginx_unicorn/unicorn.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/nginx_unicorn/unicorn.rb b/nginx_unicorn/unicorn.rb
new file mode 100644
index 0000000..5149547
--- /dev/null
+++ b/nginx_unicorn/unicorn.rb
@@ -0,0 +1,61 @@
+##
+# This is the configuration file for Unicorn. It m
+
+# TODO: Change it at your own needs
+WD = '/my/app/root'
+NCORES = `cat /proc/cpuinfo | grep 'cores' | uniq | sed 's/[^0-9]//g'`.to_i
+
+# Setting up the working directory and some paths
+working_directory WD
+pid "#{WD}/tmp/pids/unicorn.pid"
+stderr_path "#{WD}/log/unicorn.rem.sock"
+stdout_path "#{WD}/log/unicorn.rem.sock"
+
+# We want one worker per core.
+worker_processes NCORES
+
+# Listen on a Unix domain socket. I'm using a shorter backlog
+# for quicker failover when busy.
+listen '/tmp/unicorn.rem.sock', backlog: 64
+
+# nuke workers after 30 seconds instead of 60 seconds (the default)
+timeout 30
+
+# Preloading app for memory saving on COW-friendly GC's
+preload_app true
+GC.respond_to?(:copy_on_write_friendly=) and
+ GC.copy_on_write_friendly = true
+
+
+before_fork do |server, worker|
+ ##
+ # The following is highly recomended for Rails + "preload_app true"
+ # as there's no need for the master process to hold a connection
+ defined?(ActiveRecord::Base) and
+ ActiveRecord::Base.connection.disconnect!
+
+ ##
+ # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
+ # immediately start loading up a new version of itself (loaded with a new
+ # version of our app). When this new Unicorn is completely loaded
+ # it will begin spawning workers. The first worker spawned will check to
+ # see if an .oldbin pidfile exists. If so, this means we've just booted up
+ # a new Unicorn and need to tell the old one that it can now die. To do so
+ # we send it a QUIT.
+ #
+ # Using this method we get 0 downtime deploys.
+ old_pid = "#{WD}/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
+
+after_fork do |server, worker|
+ # The following is *required* for Rails + "preload_app true",
+ defined?(ActiveRecord::Base) and
+ ActiveRecord::Base.establish_connection
+end