make self-daemonizing command synchronous
[god.git] / examples / events.god
blob94063989576efec97dfe643972bc97b71d533fb2
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   w.log = File.join(RAILS_ROOT, "log/commands.log")
16   
17   # clean pid files before start if necessary
18   w.behavior(:clean_pid_file)
19   
20   # determine the state on startup
21   w.transition(:init, { true => :up, false => :start }) do |on|
22     on.condition(:process_running) do |c|
23       c.running = true
24     end
25   end
26   
27   # determine when process has finished starting
28   w.transition([:start, :restart], :up) do |on|
29     on.condition(:process_running) do |c|
30       c.running = true
31     end
32     
33     # failsafe
34     on.condition(:tries) do |c|
35       c.times = 8
36       c.transition = :start
37     end
38   end
40   # start if process is not running
41   w.transition(:up, :start) do |on|
42     on.condition(:process_exits)
43   end
44   
45   # restart if memory or cpu is too high
46   w.transition(:up, :restart) do |on|
47     on.condition(:memory_usage) do |c|
48       c.interval = 20
49       c.above = 50.megabytes
50       c.times = [3, 5]
51     end
52     
53     on.condition(:cpu_usage) do |c|
54       c.interval = 10
55       c.above = 10.percent
56       c.times = 5
57     end
58     
59     on.condition(:http_response_code) do |c|
60       c.host = 'localhost'
61       c.port = '3000'
62       c.path = '/'
63       c.code_is_not = 201
64       c.timeout = 10.seconds
65       c.times = [3, 5]
66     end
67   end
68   
69   # lifecycle
70   w.lifecycle do |on|
71     on.condition(:flapping) do |c|
72       c.to_state = [:start, :restart]
73       c.times = 5
74       c.within = 1.minute
75       c.transition = :unmonitored
76       c.retry_in = 10.minutes
77       c.retry_times = 5
78       c.retry_within = 2.hours
79     end
80   end
81 end