Skip to content

Instantly share code, notes, and snippets.

@IPrism
Forked from defunkt/gist:208581
Created July 6, 2011 16:03
Show Gist options
  • Save IPrism/1067612 to your computer and use it in GitHub Desktop.
Save IPrism/1067612 to your computer and use it in GitHub Desktop.
god script for unicorn + rvm
# http://unicorn.bogomips.org/SIGNALS.html
# change these to match your app
app_name = "APP_NAME"
rvm_string = "RVM_STRING"
user = "USER"
pid_file = "/srv/webapps/#{app_name}/shared/pids/#{app_name}.pid"
gem_home = "/usr/local/rvm/gems/#{rvm_string}"
app_root = "/srv/webapps/#{app_name}/current"
rails_env = ENV['RAILS_ENV'] || 'production'
set_path = "cd #{app_root}; rvm use #{rvm_string}; export GEM_HOME=#{gem_home}"
unicorn_cmd = "#{set_path}; #{gem_home}/bin/unicorn_rails -c #{rails_root}/config/unicorn.rb -E #{rails_env} -D"
God.watch do |w|
w.name = "unicorn"
w.interval = 30.seconds # default
# unicorn needs to be run from the rails root
w.start = unicorn_cmd
# QUIT gracefully shuts down workers
w.stop = "kill -QUIT `cat #{pid_file}`"
# USR2 causes the master to re-create itself and spawn a new worker pool
w.restart = "kill -USR2 `cat #{pid_file}`"
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = pid_file
w.uid = user
w.gid = user
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 = 150.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment