Skip to content

Commit

Permalink
Unicorn support added
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Dec 8, 2011
1 parent 56d5cfc commit 0b8404b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ gem "erubis"
gem "haml"
gem "pdfkit"
gem "pg"

group :production do
gem 'unicorn'
end
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GEM
erubis (2.6.6)
abstract (>= 1.0.0)
haml (3.0.21)
kgio (2.6.0)
pdfkit (0.4.6)
pg (0.10.1)
rack (1.1.0)
Expand All @@ -27,7 +28,12 @@ GEM
activeresource (= 2.3.8)
activesupport (= 2.3.8)
rake (>= 0.8.3)
raindrops (0.8.0)
rake (0.8.7)
unicorn (4.1.1)
kgio (~> 2.4)
rack
raindrops (~> 0.6)
warden (0.10.7)
rack (>= 1.0.0)

Expand All @@ -41,3 +47,4 @@ DEPENDENCIES
pdfkit
pg
rails (= 2.3.8)
unicorn
53 changes: 53 additions & 0 deletions config/unicorn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
deploy_to = "/srv/taurus"
rails_root = "#{deploy_to}/current"
pid_file = "#{deploy_to}/shared/pids/unicorn.pid"
socket_file= "#{deploy_to}/shared/pids/unicorn.sock"
log_file = "#{rails_root}/log/unicorn.log"
err_log = "#{rails_root}/log/unicorn_error.log"
old_pid = pid_file + '.oldbin'

timeout 60
worker_processes 4
listen socket_file, :backlog => 2048
pid pid_file
stderr_path err_log
stdout_path log_file

preload_app true

GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)

before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "#{rails_root}/Gemfile"
end

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.

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|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end

0 comments on commit 0b8404b

Please sign in to comment.