# run with: god -c /usr/local/etc/god/config.god # # require 'rubygems' require 'yaml' require 'angel' DEFAULT = { 'servers' => '1', 'address' => '127.0.0.1', 'environment' => 'production', 'user' => 'nobody', 'group' => 'nobody', 'web_server' => 'thin', 'pid_file' => '/var/run/god-app.pid', 'log_file' => '/var/log/god-app.log', } #God.load "/usr/local/etc/god/contacts.god" Dir["/usr/local/etc/god/service/*.yml"].each do |config_file| config = DEFAULT.merge(YAML.load_file(config_file)) config['app'] = File.basename(config_file, ".yml") raise "File #{config_file} is missing cwd definition" unless config['cwd'] config['servers'].to_i.times do |n| this = config.merge('port' => config['port'] + n) Angel.set_pid_file(this) Angel.set_log_file(this) God.watch do |w| w.name = Angel.process_name(this) w.group = this['app'] w.stop = Angel.stop_cmd(this) w.start = Angel.start_cmd(this) w.restart = Angel.restart_cmd(this) w.interval = 30.seconds w.start_grace = 10.seconds w.restart_grace = 10.seconds w.pid_file = this['pid_file'] w.uid = this['user'] w.gid = this['group'] # clean pid file before start if necessary w.behavior(:clean_pid_file) # determine state on god 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 end # failsafe on.condition(:tries) do |c| c.times = 5 c.transition = :start end end # start if process is not running w.transition(:up, :start) do |on| on.condition(:process_exits) end # Restart if memory or cpu usage is too high w.transition(:up, :restart) do |on| on.condition(:memory_usage) do |c| c.interval = 20 c.above = 50.megabytes c.times = [3,5] # three out of five intervals end on.condition(:cpu_usage) do |c| c.interval = 10 c.above = 10.percent c.times = [3,5] # three out of five intervals end end # Life!!! w.lifecycle do |on| on.condition(:flapping) do |c| c.to_state = [:start, :restart] c.times = 5 c.within = 5.minutes c.transition = :unmonitored c.retry_in = 10.minutes c.retry_times = 5 c.retry_within = 2.hours end end end end end