ChangingThePresent
The Few, The Proud, The Pradipta 416
My Resume
Web DOC PDF
RTF ODT TXT
powered by emurse

However poor the taste of the author in choosing a name, it is indeed a great way to monitor, nay, control pretty much anything.

I use god on my local dev box (mac) as well to keep all things developery running.

I got the init script from http://ezinearticles.com/?Why-Rails-Developers-Need-God&id=845672 and all the good info from http://god.rubyforge.org/

Handily, a god init script will of course start your mongrels et al when properly configured. Here is how I use a config template for the many gods-mongrels


host = `hostname`
app = "foo" 

case
when host =~ /theproductionbox/
  ports = %w{ 6400 6401 } 
  RAILS_ROOT = "/u/apps/#{app}/current" 
  shared_path = "/u/apps/#{app}/shared" 
  mongrel_cmd = "/usr/local/bin/ruby /usr/local/bin/mongrel_rails" 
  pid_dir = "#{shared_path}/pids" 
  log_file = "#{shared_path}/log/mongrel.log" 
  uid = 'production_guy'
  gid = uid
  ENV['RAILS_ENV'] ||= 'production'
  rails_env ||= ENV['RAILS_ENV']
  http_host = "fancy.productionbox.com" 
  http_path = "/" 
  http_code = "200" 
when host =~ /somedevbox/
  ports = %w{ 6400 } 
  RAILS_ROOT = "/u/apps/#{app}" 
  shared_path = "/u/apps/#{app}" 
  mongrel_cmd = "/usr/local/bin/ruby /usr/local/bin/mongrel_rails" 
  pid_dir = "#{shared_path}/tmp/pids" 
  log_file = "#{shared_path}/log/mongrel.log" 
  uid = 'development_guy'
  gid = uid
  ENV['RAILS_ENV'] ||= 'development'
  rails_env ||= ENV['RAILS_ENV']
  http_host = "ratbike.devbox.com" 
  http_path = "/" 
  http_code = "200" 
end

ports.each do |port|
  God.watch do |w|
    w.group = app
    w.uid = uid
    w.gid = gid
    w.name = "#{app}-mongrel-#{port}" 
    w.interval = 30.seconds
    w.start = "#{mongrel_cmd} start -d -a 0.0.0.0 -p #{port} -P #{pid_dir}/dispatch.#{port}.pid -e #{rails_env} -c #{RAILS_ROOT} -l #{log_file}" 
    w.stop = "#{mongrel_cmd} stop -P #{pid_dir}/dispatch.#{port}.pid" 
    w.start_grace = 30.seconds
    w.restart_grace = 30.seconds
    w.pid_file = File.join(pid_dir, "dispatch.#{port}.pid")

    w.behavior(:clean_pid_file)

    w.transition(:init, { true => :up, false => :start }) do |on|
      on.condition(:process_running) do |c|
        c.running = true
      end
    end

    w.transition([:start, :restart], :up) do |on|
      on.condition(:process_running) do |c|
        c.running = true
      end
      on.condition(:tries) do |c|
        c.times = 5
        c.transition = :start
      end
    end

    w.transition(:up, :start) do |on|
      on.condition(:process_exits)
    end  

    w.transition(:up, :restart) do |on|
      on.condition(:memory_usage) do |c|
        c.interval = 60.seconds
        c.above = 57.megabytes
        c.times = [4, 5]
      end
      on.condition(:cpu_usage) do |c|
        c.interval = 60.seconds
        c.above = 10.percent
        c.times = [3, 5]
      end
      on.condition(:http_response_code) do |c|
        c.host = http_host
        c.path = http_path
        c.code_is_not = http_code
        c.timeout = 30.seconds
    c.interval = 30.minutes
      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
end

God::Contacts::Email.message_settings = {
  :from => 'god@rubyisbeautiful.com'
}

God::Contacts::Email.server_settings = {
  #settings
}

God.contact(:email) do |c|
  c.name = 'bryan'
  c.email = 'bryan@rubyisbeautiful.com'
end
		
	

Leave a Comment

back to top

micro theme by seaofclouds, and powered with Mephisto Hosting by hostingrails.com