Skip to content

Instantly share code, notes, and snippets.

@ajsharp
Created May 24, 2011 01:17
Show Gist options
  • Save ajsharp/987989 to your computer and use it in GitHub Desktop.
Save ajsharp/987989 to your computer and use it in GitHub Desktop.
RACK_ENV = ENV['RACK_ENV'] || "production"
APP_ROOT = "/var/www/api/current"
PID_DIR = "/var/www/api/shared/pids"
$stderr.puts("*" * 80)
$stderr.puts("Current environment is #{RACK_ENV}")
$stderr.puts("*" * 80)
God.pid = "/var/run/god.pid"
God.log_file = "/var/www/api/shared/log/god.log"
God.log_level = ((RACK_ENV == 'production') ? :info : :debug)
%w(unicorn resque).each do |config|
God.load "#{APP_ROOT}/config/god/#{config}.god"
end
# ATTENTION! DO NOT LOAD THIS FILE DIRECTLY.
# Please use init.god, which will load this file.
num_workers = (RACK_ENV == 'production' ? 5 : 2)
num_workers.times do |num|
God.watch do |w|
w.name = "resque-#{num}"
w.group = 'resque'
w.interval = 30.seconds
w.env = {"QUEUE"=>"*", "RACK_ENV"=>RACK_ENV, "PIDFILE" => "#{APP_ROOT}/tmp/pids/resque-#{num}.pid"}
w.start = "bundle exec rake environment resque:work"
w.stop = "kill -QUIT `cat #{APP_ROOT}/tmp/pids/resque-#{num}.pid`"
w.dir = APP_ROOT
w.start_grace = 20.seconds
w.restart_grace = 20.seconds
w.uid = 'deploy'
w.gid = 'deploy'
# retart if memory gets too high
w.transition(:up, :restart) do |on|
on.condition(:memory_usage) do |c|
c.above = 200.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
# ATTENTION! DO NOT LOAD THIS FILE DIRECTLY.
# Please use init.god, which will load this file.
#
# For global variable setup, see init.god.
God.watch do |w|
w.name = "api-unicorn-master-1"
w.group = 'api-unicorn-master'
w.interval = 30.seconds
w.env = {"RACK_ENV" => RACK_ENV}
w.start = "cd #{APP_ROOT} && bundle exec unicorn -c /var/www/api/current/config/unicorn.rb -E #{RACK_ENV} -D"
w.stop = "kill -QUIT `cat #{PID_DIR}/unicorn.pid`"
w.restart = "kill -USR2 `cat #{PID_DIR}/unicorn.pid`"
w.pid_file = PID_DIR + '/unicorn.pid'
w.start_grace = 20.seconds
w.restart_grace = 20.seconds
w.uid = 'deploy'
w.gid = 'deploy'
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment