Skip to content

Instantly share code, notes, and snippets.

@nragaz
Created July 12, 2010 03:16
Show Gist options
  • Save nragaz/472092 to your computer and use it in GitHub Desktop.
Save nragaz/472092 to your computer and use it in GitHub Desktop.
God configs for starting Resque and Unicorn
PID_DIR = '/srv/myapp/shared/pids'
RAILS_ENV = ENV['RAILS_ENV'] = 'production'
RAILS_ROOT = ENV['RAILS_ROOT'] = '/srv/myapp/current'
BIN_PATH = "/home/rails/.rvm/gems/ree-1.8.7-2010.02/bin"
God.log_file = "#{RAILS_ROOT}/log/god.log"
God.log_level = :info
%w(unicorn resque).each do |config|
God.load "#{RAILS_ROOT}/config/god/#{config}.god"
end
num_workers = (RAILS_ENV == 'production') ? 4 : 2
num_workers.times do |num|
God.watch do |w|
w.name = "resque-#{num}"
w.group = 'resque'
w.interval = 30.seconds
w.env = {"QUEUE"=>"*", "RAILS_ENV"=>RAILS_ENV}
# resque:work has a dependency on environment in my Rakefile, so no need to call it first
w.start = "#{BIN_PATH}/rake -f #{RAILS_ROOT}/Rakefile resque:work"
w.uid = 'rails' # user id
w.gid = 'rails' # group id
# restart if memory gets too high
w.transition(:up, :restart) do |on|
on.condition(:memory_usage) do |c|
c.above = 350.megabytes
c.times = 2
end
end
# determine the state on startup
w.transition(:init, { true => :up, false => :start }) do |on|
on.condition(:process_running) do |c|
c.running = true
end
end
# determine when process has finished starting
w.transition([:start, :restart], :up) do |on|
on.condition(:process_running) do |c|
c.running = true
c.interval = 5.seconds
end
# failsafe
on.condition(:tries) do |c|
c.times = 5
c.transition = :start
c.interval = 5.seconds
end
end
# start if process is not running
w.transition(:up, :start) do |on|
on.condition(:process_running) do |c|
c.running = false
end
end
end
end
# http://unicorn.bogomips.org/SIGNALS.html
God.watch do |w|
w.name = "unicorn"
w.interval = 30.seconds # default
# unicorn needs to be run from the rails root
w.start = "cd #{RAILS_ROOT} && #{BIN_PATH}/unicorn_rails -c #{RAILS_ROOT}/config/unicorn.rb -E #{RAILS_ENV} -D"
# QUIT gracefully shuts down workers
w.stop = "kill -QUIT `cat #{PID_DIR}/unicorn.pid`"
# USR2 causes the master to re-create itself and spawn a new worker pool
w.restart = "kill -USR2 `cat #{PID_DIR}/unicorn.pid`"
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = "#{PID_DIR}/unicorn.pid"
w.uid = 'rails'
w.gid = 'rails'
w.behavior(:clean_pid_file)
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 5.seconds
c.running = false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = 300.megabytes
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = 50.percent
c.times = 5
end
end
# lifecycle
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 5.minute
c.transition = :unmonitored
c.retry_in = 10.minutes
c.retry_times = 5
c.retry_within = 2.hours
end
end
end
Copy link

ghost commented Mar 15, 2011

When you say "resque:work has a dependency on environment in my Rakefile", what does that code look like?

@nragaz
Copy link
Author

nragaz commented Mar 15, 2011

Add this to a file named lib/tasks/resque.rake (or any other rake tasks file that's loaded by your app:

namespace :resque do
  task work: :environment
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment