Skip to content

Instantly share code, notes, and snippets.

@digitalhobbit
Created February 21, 2009 01:04

Revisions

  1. digitalhobbit revised this gist Feb 27, 2009. 1 changed file with 4 additions and 8 deletions.
    12 changes: 4 additions & 8 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@

    # Based on example from <http://railscasts.com/episodes/130-monitoring-with-god>

    RAILS_ROOT = File.dirname(File.dirname(__FILE__))
    RAILS_ROOT = '/var/www/myapp'

    def generic_monitoring(w, options = {})
    w.start_if do |start|
    @@ -42,15 +42,11 @@ def generic_monitoring(w, options = {})
    end

    # Start 3 workling daemons
    # TODO: Verify that this really works. Starting should be fine, but dealing with
    # individual dead workling daemons might be tricky, as there's only a single
    # command to start one or stop all worklings. Might have to use kill + cat
    # to kill a specific workling process, similar to the starling config below.
    0.upto(2) do |num|
    God.watch do |w|
    script = "RAILS_ENV=#{RAILS_ENV} #{RAILS_ROOT}/script/workling_client"
    script = "RAILS_ENV=#{RAILS_ENV} #{RAILS_ROOT}/script/workling_client --number #{num}"
    w.name = "myapp-workling-#{num}"
    w.group = "myapp"
    w.group = "myapp-worklings"
    w.interval = 60.seconds
    w.start = "#{script} start"
    w.restart = "#{script} restart"
    @@ -67,7 +63,7 @@ def generic_monitoring(w, options = {})

    God.watch do |w|
    w.name = "myapp-starling"
    w.group = "myapp"
    w.group = "myapp-starlings"
    w.interval = 60.seconds
    w.start = "/usr/bin/starling -d -P #{RAILS_ROOT}/log/starling.pid -q #{RAILS_ROOT}/log/ -p #{STARLING_PORT} -h #{STARLING_HOST}"
    w.stop = "kill `cat #{RAILS_ROOT}/log/starling.pid`"
  2. @invalid-email-address Anonymous created this gist Feb 21, 2009.
    81 changes: 81 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    # If we ever end up running God / Starling on other environments than production.
    # we'll have to find a different way to set the values below.
    STARLING_PORT = 15151
    STARLING_HOST = '<my ip address>'
    RAILS_ENV = 'production'

    # Based on example from <http://railscasts.com/episodes/130-monitoring-with-god>

    RAILS_ROOT = File.dirname(File.dirname(__FILE__))

    def generic_monitoring(w, options = {})
    w.start_if do |start|
    start.condition(:process_running) do |c|
    c.interval = 10.seconds
    c.running = false
    end
    end

    w.restart_if do |restart|
    restart.condition(:memory_usage) do |c|
    c.above = options[:memory_limit]
    c.times = [3, 5] # 3 out of 5 intervals
    end

    restart.condition(:cpu_usage) do |c|
    c.above = options[:cpu_limit]
    c.times = 5
    end
    end

    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

    # Start 3 workling daemons
    # TODO: Verify that this really works. Starting should be fine, but dealing with
    # individual dead workling daemons might be tricky, as there's only a single
    # command to start one or stop all worklings. Might have to use kill + cat
    # to kill a specific workling process, similar to the starling config below.
    0.upto(2) do |num|
    God.watch do |w|
    script = "RAILS_ENV=#{RAILS_ENV} #{RAILS_ROOT}/script/workling_client"
    w.name = "myapp-workling-#{num}"
    w.group = "myapp"
    w.interval = 60.seconds
    w.start = "#{script} start"
    w.restart = "#{script} restart"
    w.stop = "#{script} stop"
    w.start_grace = 20.seconds
    w.restart_grace = 20.seconds
    w.pid_file = "#{RAILS_ROOT}/log/workling#{num}.pid"

    w.behavior(:clean_pid_file)

    generic_monitoring(w, :cpu_limit => 25.percent, :memory_limit => 100.megabytes)
    end
    end

    God.watch do |w|
    w.name = "myapp-starling"
    w.group = "myapp"
    w.interval = 60.seconds
    w.start = "/usr/bin/starling -d -P #{RAILS_ROOT}/log/starling.pid -q #{RAILS_ROOT}/log/ -p #{STARLING_PORT} -h #{STARLING_HOST}"
    w.stop = "kill `cat #{RAILS_ROOT}/log/starling.pid`"
    w.start_grace = 10.seconds
    w.restart_grace = 10.seconds
    w.pid_file = "#{RAILS_ROOT}/log/starling.pid"

    w.behavior(:clean_pid_file)

    generic_monitoring(w, :cpu_limit => 30.percent, :memory_limit => 30.megabytes)
    end