agibralter (owner)

Fork Of

Revisions

  • ad177d digital... Fri Feb 27 12:02:05 -0800 2009
  • 1aac1d Fri Feb 20 17:04:21 -0800 2009
gist: 113917 Download_button fork
public
Public Clone URL: git://gist.github.com/113917.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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 = '/var/www/myapp'
 
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
0.upto(2) do |num|
  God.watch do |w|
    script = "RAILS_ENV=#{RAILS_ENV} #{RAILS_ROOT}/script/workling_client --number #{num}"
    w.name = "myapp-workling-#{num}"
    w.group = "myapp-worklings"
    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-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`"
  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