put watch back into previous state after a god load
[god.git] / examples / events.god
blob76d45da8634c8e7f90e2d2d2540e05c9d4c786e4
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 God.watch do |w|
10   w.name = "local-3000"
11   w.interval = 5.seconds
12   w.start = "mongrel_rails start -P #{RAILS_ROOT}/log/mongrel.pid -c #{RAILS_ROOT} -d"
13   w.stop = "mongrel_rails stop -P #{RAILS_ROOT}/log/mongrel.pid -c #{RAILS_ROOT}"
14   w.pid_file = File.join(RAILS_ROOT, "log/mongrel.pid")
15   w.log = File.join(RAILS_ROOT, "log/commands.log")
16   w.autostart = false
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.transition = :start
38     end
39   end
41   # start if process is not running
42   w.transition(:up, :start) do |on|
43     on.condition(:process_exits)
44   end
45   
46   # restart if memory or cpu is too high
47   w.transition(:up, :restart) do |on|
48     on.condition(:memory_usage) do |c|
49       c.interval = 20
50       c.above = 50.megabytes
51       c.times = [3, 5]
52     end
53     
54     on.condition(:cpu_usage) do |c|
55       c.interval = 10
56       c.above = 10.percent
57       c.times = 5
58     end
59     
60     on.condition(:http_response_code) do |c|
61       c.host = 'localhost'
62       c.port = '3000'
63       c.path = '/'
64       c.code_is = 500
65       c.timeout = 10.seconds
66       c.times = [3, 5]
67     end
68   end
69   
70   # lifecycle
71   w.lifecycle do |on|
72     on.condition(:flapping) do |c|
73       c.to_state = [:start, :restart]
74       c.times = 5
75       c.within = 1.minute
76       c.transition = :unmonitored
77       c.retry_in = 10.minutes
78       c.retry_times = 5
79       c.retry_within = 2.hours
80     end
81   end
82 end