finish lifecycle conditions handling and add flapper condition
[god.git] / examples / events.god
blob362775665d2e6b06023d20568a03481cf7e6ebe9
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 = "/Users/tom/dev/git/helloworld"
9 God.watch do |w|
10   w.name = "local-3000"
11   w.interval = 5.seconds
12   w.start = "mongrel_rails start -P ./log/mongrel.pid -c #{RAILS_ROOT} -d"
13   w.stop = "mongrel_rails stop -P ./log/mongrel.pid -c #{RAILS_ROOT}"
14   w.pid_file = File.join(RAILS_ROOT, "log/mongrel.pid")
15   
16   # clean pid files before start if necessary
17   w.behavior(:clean_pid_file)
18   
19   # determine the state on startup
20   w.transition(:init, { true => :up, false => :start }) do |on|
21     on.condition(:process_running) do |c|
22       c.running = true
23     end
24   end
25   
26   # determine when process has finished starting
27   w.transition([:start, :restart], :up) do |on|
28     on.condition(:process_running) do |c|
29       c.running = true
30     end
31     
32     # failsafe
33     on.condition(:tries) do |c|
34       c.times = 3
35       c.transition = :start
36     end
37   end
39   # start if process is not running
40   w.transition(:up, :start) do |on|
41     on.condition(:process_exits)
42   end
43   
44   # restart if memory or cpu is too high
45   w.transition(:up, :restart) do |on|
46     on.condition(:memory_usage) do |c|
47       c.interval = 20
48       c.above = 50.megabytes
49       c.times = [3, 5]
50     end
51     
52     on.condition(:cpu_usage) do |c|
53       c.interval = 10
54       c.above = 10.percent
55       c.times = [3, 5]
56     end
57   end
58   
59   # lifecycle
60   w.lifecycle do |on|
61     on.condition(:flapping) do |c|
62       c.to_state = [:start, :restart]
63       c.times = 5
64       c.within = 1.minute
65       c.transition = :unmonitored
66       c.retry_in = 10.minutes
67       c.retry_times = 5
68       c.retry_within = 2.hours
69     end
70   end
71 end