Merge branch 'fork_exec' of git://208.76.47.25
[god.git] / examples / events.god
blobc89775db6caa4d8216f2f7d4039a387c7bdfe808
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/helloworld"
9 God.meddle do |god|
10   god.watch do |w|
11     w.name = "local-3000"
12     w.interval = 5 # seconds
13     w.start = "mongrel_rails start -P ./log/mongrel.pid -c #{RAILS_ROOT} -d"
14     w.stop = "mongrel_rails stop -P ./log/mongrel.pid -c #{RAILS_ROOT}"
15     
16     pid_file = File.join(RAILS_ROOT, "log/mongrel.pid")
17     
18     # clean pid files before start if necessary
19     w.behavior(:clean_pid_file) do |b|
20       b.pid_file = pid_file
21     end
22     
23     # determine the state on startup
24     w.transition(:init, { true => :up, false => :start }) do |on|
25       on.condition(:process_running) do |c|
26         c.running = true
27         c.pid_file = pid_file
28       end
29     end
30     
31     # determine when process has finished starting
32     w.transition([:start, :restart], :up) do |on|
33       on.condition(:process_running) do |c|
34         c.running = true
35         c.pid_file = pid_file
36       end
37     end
38   
39     # start if process is not running
40     w.transition(:up, :start) do |on|
41       on.condition(:process_exits) do |c|
42         c.pid_file = pid_file
43       end
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.pid_file = pid_file
51         c.above = (50 * 1024) # 50mb
52         c.times = [3, 5]
53       end
54       
55       on.condition(:cpu_usage) do |c|
56         c.interval = 10
57         c.pid_file = pid_file
58         c.above = 10 # percent
59         c.times = [3, 5]
60       end
61     end
62   end
63 end