tighten up Hub and add comments
[god.git] / examples / events.god
blob34a1aa22add07bc72534c189fe2b6946d8d1664e
1 # This example shows how you might keep a local development Rails server up
2 # and running on your Mac.
4 # Run with:
5 # god -c /path/to/events.god
7 RAILS_ROOT = ENV['GOD_TEST_RAILS_ROOT']
9 %w{3002}.each do |port|
10   God.watch do |w|
11     w.name = "local-#{port}"
12     w.interval = 5.seconds
13     w.start = "mongrel_rails start -p #{port} -P #{RAILS_ROOT}/log/mongrel.#{port}.pid -c #{RAILS_ROOT} -d"
14     w.stop = "mongrel_rails stop -P #{RAILS_ROOT}/log/mongrel.#{port}.pid -c #{RAILS_ROOT}"
15     w.pid_file = File.join(RAILS_ROOT, "log/mongrel.#{port}.pid")
16     w.log = File.join(RAILS_ROOT, "log/commands.#{port}.log")
17   
18     # clean pid files before start if necessary
19     w.behavior(:clean_pid_file)
20   
21     # determine the state on startup
22     w.transition(:init, { true => :up, false => :start }) do |on|
23       on.condition(:process_running) do |c|
24         c.running = true
25       end
26     end
27   
28     # determine when process has finished starting
29     w.transition([:start, :restart], :up) do |on|
30       on.condition(:process_running) do |c|
31         c.running = true
32       end
33     
34       # failsafe
35       on.condition(:tries) do |c|
36         c.times = 8
37         c.within = 2.minutes
38         c.transition = :start
39       end
40     end
42     # start if process is not running
43     w.transition(:up, :start) do |on|
44       on.condition(:process_exits)
45     end
46   
47     # restart if memory or cpu is too high
48     w.transition(:up, :restart) do |on|
49       on.condition(:memory_usage) do |c|
50         c.interval = 20
51         c.above = 50.megabytes
52         c.times = [3, 5]
53       end
54     
55       on.condition(:cpu_usage) do |c|
56         c.interval = 10
57         c.above = 10.percent
58         c.times = 5
59       end
60     
61       on.condition(:http_response_code) do |c|
62         c.host = 'localhost'
63         c.port = port
64         c.path = '/'
65         c.code_is = 500
66         c.timeout = 10.seconds
67         c.times = [3, 5]
68       end
69     end
70   
71     # lifecycle
72     w.lifecycle do |on|
73       on.condition(:flapping) do |c|
74         c.to_state = [:start, :restart]
75         c.times = 5
76         c.within = 1.minute
77         c.transition = :unmonitored
78         c.retry_in = 10.minutes
79         c.retry_times = 5
80         c.retry_within = 2.hours
81       end
82     end
83   end
84 end