Skip to content
This repository
Browse code

Move to unicorn

  • Loading branch information...
commit b478a8ea337362d7d4bedf226705f94b6459e6b5 1 parent 0662921
Sudara authored

Showing 3 changed files with 88 additions and 17 deletions. Show diff stats Hide diff stats

  1. +3 1 Gemfile
  2. +23 16 Gemfile.lock
  3. +62 0 config/unicorn.rb
4 Gemfile
... ... @@ -1,9 +1,11 @@
1 1 source :gemcutter
2   -gem "rails", "2.3.11"
  2 +gem "rails", "2.3.14"
3 3 #gem "mongrel", :require => nil
4 4 #gem "mongrel_cluster"
5 5 gem "mysql2", "~> 0.2.7"
6 6
  7 +gem "unicorn"
  8 +
7 9 # bundler requires these gems in all environments
8 10 gem "nokogiri"
9 11
39 Gemfile.lock
@@ -10,16 +10,16 @@ GEM
10 10 remote: http://rubygems.org/
11 11 specs:
12 12 BlueCloth (1.0.1)
13   - actionmailer (2.3.11)
14   - actionpack (= 2.3.11)
15   - actionpack (2.3.11)
16   - activesupport (= 2.3.11)
  13 + actionmailer (2.3.14)
  14 + actionpack (= 2.3.14)
  15 + actionpack (2.3.14)
  16 + activesupport (= 2.3.14)
17 17 rack (~> 1.1.0)
18   - activerecord (2.3.11)
19   - activesupport (= 2.3.11)
20   - activeresource (2.3.11)
21   - activesupport (= 2.3.11)
22   - activesupport (2.3.11)
  18 + activerecord (2.3.14)
  19 + activesupport (= 2.3.14)
  20 + activeresource (2.3.14)
  21 + activesupport (= 2.3.14)
  22 + activesupport (2.3.14)
23 23 aws-s3 (0.5.1)
24 24 builder
25 25 mime-types
@@ -29,18 +29,20 @@ GEM
29 29 googlecharts (1.6.7)
30 30 haml (2.0.9)
31 31 hpricot (0.8.4)
  32 + kgio (2.6.0)
32 33 mime-types (1.16)
33 34 mysql2 (0.2.13)
34 35 newrelic_rpm (3.1.2)
35 36 nokogiri (1.5.0)
36 37 rack (1.1.2)
37   - rails (2.3.11)
38   - actionmailer (= 2.3.11)
39   - actionpack (= 2.3.11)
40   - activerecord (= 2.3.11)
41   - activeresource (= 2.3.11)
42   - activesupport (= 2.3.11)
  38 + rails (2.3.14)
  39 + actionmailer (= 2.3.14)
  40 + actionpack (= 2.3.14)
  41 + activerecord (= 2.3.14)
  42 + activeresource (= 2.3.14)
  43 + activesupport (= 2.3.14)
43 44 rake (>= 0.8.3)
  45 + raindrops (0.7.0)
44 46 rake (0.9.2)
45 47 rmagick (2.13.1)
46 48 rspec (2.6.0)
@@ -56,6 +58,10 @@ GEM
56 58 rspec (>= 1.3.0)
57 59 ruby-mp3info (0.6.15)
58 60 rubyzip (0.9.4)
  61 + unicorn (4.1.1)
  62 + kgio (~> 2.4)
  63 + rack
  64 + raindrops (~> 0.6)
59 65 will_paginate (2.3.16)
60 66 xml-simple (1.1.0)
61 67
@@ -72,11 +78,12 @@ DEPENDENCIES
72 78 mysql2 (~> 0.2.7)
73 79 newrelic_rpm
74 80 nokogiri
75   - rails (= 2.3.11)
  81 + rails (= 2.3.14)
76 82 reportable!
77 83 rmagick
78 84 rspec (>= 1.2.0)
79 85 rspec-rails (>= 1.2.0)
80 86 ruby-mp3info
81 87 rubyzip
  88 + unicorn
82 89 will_paginate (~> 2.3.14)
62 config/unicorn.rb
... ... @@ -0,0 +1,62 @@
  1 +
  2 +config = Hash.new
  3 +base = File.open(File.join(Dir.pwd,'../shared/config/unicorn.conf'),"r")
  4 +base.each do |line|
  5 + unless /\A\n|#.+/ =~ line
  6 + config["#{line.split(/=/)[0]}"] = line.split(/=/)[1].to_s.chomp
  7 + end
  8 +end
  9 +
  10 +working_directory config['APP_ROOT']
  11 +listen config['UNICORN_SOCKET'], :backlog => 1024
  12 +worker_processes 3
  13 +timeout 300
  14 +pid config['UNICORN_PID']
  15 +logger Logger.new("log/unicorn.log")
  16 +
  17 +##
  18 +# Load the app into the master before forking workers for super-fast worker spawn times
  19 +# Not good for some apps, this causes the master to use slighty more ram than a
  20 +# worker process. Otherwise it is about 14MB
  21 +# can't convert strings to booleans
  22 +if config['UNICORN_PRELOAD_APP'] == 'true'
  23 + preload_app true
  24 +elsif config['UNICORN_PRELOAD_APP'] == 'false'
  25 + preload_app false
  26 +else
  27 + preload_app true
  28 +end
  29 +
  30 +
  31 +before_fork do |server, worker|
  32 + # the following is recomended for Rails + "preload_app true"
  33 + # as there's no need for the master process to hold a connection
  34 + if defined?(ActiveRecord::Base)
  35 + ActiveRecord::Base.connection.disconnect!
  36 + end
  37 +
  38 + ##
  39 + # Method for 0 Dowtime Deploys
  40 + ##
  41 + old_pid = "#{server.config[:pid]}.oldbin"
  42 +
  43 + if File.exists?(old_pid) && server.pid != old_pid
  44 + begin
  45 + sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
  46 + Process.kill(sig, File.read(old_pid).to_i)
  47 + rescue Errno::ENOENT, Errno::ESRCH
  48 + # someone else did our job for us
  49 + end
  50 + end
  51 +end
  52 +
  53 +after_fork do |server, worker|
  54 + #Make the AR connection if defined
  55 + if defined?(ActiveRecord::Base)
  56 + ActiveRecord::Base.establish_connection
  57 + end
  58 +
  59 + #Write the pid file for this worker
  60 + worker_pid = File.join(File.dirname(server.config[:pid]), "unicorn_worker_#{worker.nr}.pid")
  61 + File.open(worker_pid, "w") { |f| f.puts Process.pid }
  62 +end

0 comments on commit b478a8e

Please sign in to comment.
Something went wrong with that request. Please try again.