Skip to content

Instantly share code, notes, and snippets.

@gerhard
Forked from kyledrake/gist:2120107
Created April 28, 2012 08:43
Show Gist options
  • Save gerhard/2517250 to your computer and use it in GitHub Desktop.
Save gerhard/2517250 to your computer and use it in GitHub Desktop.
Rainbows! config file for use with EventMachine
# This is a configuration script for using Rainbows with EventMachine. I'm providing it incase somebody finds it useful. But honestly, you
# should stop using EventMachine and use threads instead. The ThreadPool version of this is also in my gist list, and I recommend taking a look at that instead.
# NO, SERIOUSLY, STOP USING EVENTMACHINE.
Rainbows! do
name = 'yourappname'
use :EventMachine
client_max_body_size nil # This is set in nginx
# keepalive_timeout 1
case ENV['RACK_ENV'].to_sym
when :development
# This has weird issues with kqueue on OSX for me, so I use thin for development.
listen 9292
when :production
# Number of workers. I recommend one per CPU core to get full utilization.
worker_processes 4
# The number of simultaneous connections the server will accept.
worker_connections 1024
timeout 30
# I've been told the backlog here might be a problem if you're using a load balancer.
listen "unix:/var/run/yourcompany/#{name}.sock", :backlog => 4096
pid "/var/run/yourcompany/#{name}.pid"
stderr_path "/var/log/yourcompany/#{name}.log"
stdout_path "/var/log/yourcompany/#{name}.log"
###
# Hardcore performance tweaks, described here: https://github.com/blog/517-unicorn
###
# This loads the app in master, and then forks workers. Kill with USR2 and it will do a graceful restart using the block proceeding.
preload_app true
before_fork do |server, worker|
##
# 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 = "/var/run/yourcompany/#{name}.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
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment