Permalink
Browse files

Move to unicorn

  • Loading branch information...
1 parent 0662921 commit b478a8ea337362d7d4bedf226705f94b6459e6b5 @sudara committed Sep 14, 2011
Showing with 88 additions and 17 deletions.
  1. +3 −1 Gemfile
  2. +23 −16 Gemfile.lock
  3. +62 −0 config/unicorn.rb
View
4 Gemfile
@@ -1,9 +1,11 @@
source :gemcutter
-gem "rails", "2.3.11"
+gem "rails", "2.3.14"
#gem "mongrel", :require => nil
#gem "mongrel_cluster"
gem "mysql2", "~> 0.2.7"
+gem "unicorn"
+
# bundler requires these gems in all environments
gem "nokogiri"
View
39 Gemfile.lock
@@ -10,16 +10,16 @@ GEM
remote: http://rubygems.org/
specs:
BlueCloth (1.0.1)
- actionmailer (2.3.11)
- actionpack (= 2.3.11)
- actionpack (2.3.11)
- activesupport (= 2.3.11)
+ actionmailer (2.3.14)
+ actionpack (= 2.3.14)
+ actionpack (2.3.14)
+ activesupport (= 2.3.14)
rack (~> 1.1.0)
- activerecord (2.3.11)
- activesupport (= 2.3.11)
- activeresource (2.3.11)
- activesupport (= 2.3.11)
- activesupport (2.3.11)
+ activerecord (2.3.14)
+ activesupport (= 2.3.14)
+ activeresource (2.3.14)
+ activesupport (= 2.3.14)
+ activesupport (2.3.14)
aws-s3 (0.5.1)
builder
mime-types
@@ -29,18 +29,20 @@ GEM
googlecharts (1.6.7)
haml (2.0.9)
hpricot (0.8.4)
+ kgio (2.6.0)
mime-types (1.16)
mysql2 (0.2.13)
newrelic_rpm (3.1.2)
nokogiri (1.5.0)
rack (1.1.2)
- rails (2.3.11)
- actionmailer (= 2.3.11)
- actionpack (= 2.3.11)
- activerecord (= 2.3.11)
- activeresource (= 2.3.11)
- activesupport (= 2.3.11)
+ rails (2.3.14)
+ actionmailer (= 2.3.14)
+ actionpack (= 2.3.14)
+ activerecord (= 2.3.14)
+ activeresource (= 2.3.14)
+ activesupport (= 2.3.14)
rake (>= 0.8.3)
+ raindrops (0.7.0)
rake (0.9.2)
rmagick (2.13.1)
rspec (2.6.0)
@@ -56,6 +58,10 @@ GEM
rspec (>= 1.3.0)
ruby-mp3info (0.6.15)
rubyzip (0.9.4)
+ unicorn (4.1.1)
+ kgio (~> 2.4)
+ rack
+ raindrops (~> 0.6)
will_paginate (2.3.16)
xml-simple (1.1.0)
@@ -72,11 +78,12 @@ DEPENDENCIES
mysql2 (~> 0.2.7)
newrelic_rpm
nokogiri
- rails (= 2.3.11)
+ rails (= 2.3.14)
reportable!
rmagick
rspec (>= 1.2.0)
rspec-rails (>= 1.2.0)
ruby-mp3info
rubyzip
+ unicorn
will_paginate (~> 2.3.14)
View
62 config/unicorn.rb
@@ -0,0 +1,62 @@
+
+config = Hash.new
+base = File.open(File.join(Dir.pwd,'../shared/config/unicorn.conf'),"r")
+base.each do |line|
+ unless /\A\n|#.+/ =~ line
+ config["#{line.split(/=/)[0]}"] = line.split(/=/)[1].to_s.chomp
+ end
+end
+
+working_directory config['APP_ROOT']
+listen config['UNICORN_SOCKET'], :backlog => 1024
+worker_processes 3
+timeout 300
+pid config['UNICORN_PID']
+logger Logger.new("log/unicorn.log")
+
+##
+# Load the app into the master before forking workers for super-fast worker spawn times
+# Not good for some apps, this causes the master to use slighty more ram than a
+# worker process. Otherwise it is about 14MB
+# can't convert strings to booleans
+if config['UNICORN_PRELOAD_APP'] == 'true'
+ preload_app true
+elsif config['UNICORN_PRELOAD_APP'] == 'false'
+ preload_app false
+else
+ preload_app true
+end
+
+
+before_fork do |server, worker|
+ # the following is recomended for Rails + "preload_app true"
+ # as there's no need for the master process to hold a connection
+ if defined?(ActiveRecord::Base)
+ ActiveRecord::Base.connection.disconnect!
+ end
+
+ ##
+ # Method for 0 Dowtime Deploys
+ ##
+ old_pid = "#{server.config[:pid]}.oldbin"
+
+ if File.exists?(old_pid) && server.pid != old_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
+ # someone else did our job for us
+ end
+ end
+end
+
+after_fork do |server, worker|
+ #Make the AR connection if defined
+ if defined?(ActiveRecord::Base)
+ ActiveRecord::Base.establish_connection
+ end
+
+ #Write the pid file for this worker
+ worker_pid = File.join(File.dirname(server.config[:pid]), "unicorn_worker_#{worker.nr}.pid")
+ File.open(worker_pid, "w") { |f| f.puts Process.pid }
+end

0 comments on commit b478a8e

Please sign in to comment.