implemented state based lifecycle
[god.git] / test / configs / test.rb
blob0b9cfb2bb800d2a03ec54b6596bb08b59e9b8e72
1 if $0 == __FILE__
2   require File.join(File.dirname(__FILE__), *%w[.. .. lib god])
3 end
5 RAILS_ROOT = "/Users/tom/dev/powerset/speechverb"
7 God.meddle do |god|
8   god.watch do |w|
9     w.name = "local-3000"
10     w.interval = 5 # seconds
11     w.start = "mongrel_rails start -P ./log/mongrel.pid -c #{RAILS_ROOT} -p 3001 -d"
12     w.stop = "mongrel_rails stop -P ./log/mongrel.pid -c #{RAILS_ROOT}"
13     w.grace = 5
14     
15     pid_file = File.join(RAILS_ROOT, "log/mongrel.pid")
16     
17     # clean pid files before start if necessary
18     w.behavior(:clean_pid_file) do |b|
19       b.pid_file = pid_file
20     end
21     
22     # determine the state on startup
23     w.transition(:init, { true => :up, false => :start }) do |on|
24       on.condition(:process_running) do |c|
25         c.running = true
26         c.pid_file = pid_file
27       end
28     end
29     
30     # determine when process has finished starting
31     w.transition([:start, :restart], :up) do |on|
32       on.condition(:always) do |c|
33         c.what = true
34       end
35     end
36   
37     # start if process is not running
38     w.transition(:up, :start) do |on|
39       # on.condition(:process_exits) do |c|
40       #   c.pid_file = pid_file
41       # end
42       
43       on.condition(:process_running) do |c|
44         c.running = false
45         c.pid_file = pid_file
46       end
47     end
48     
49     # restart if memory or cpu is too high
50     w.transition(:up, :restart) do |on|
51       on.condition(:memory_usage) do |c|
52         c.interval = 20
53         c.pid_file = pid_file
54         c.above = (50 * 1024) # 50mb
55         c.times = [3, 5]
56       end
57       
58       on.condition(:cpu_usage) do |c|
59         c.interval = 10
60         c.pid_file = pid_file
61         c.above = 10 # percent
62         c.times = [3, 5]
63       end
64     end
65   end
66   
67   # clear old session files
68   # god.watch do |w|
69   #   w.name = "local-session-cleanup"
70   #   w.start = lambda do
71   #     Dir["#{RAILS_ROOT}/tmp/sessions/ruby_sess.*"].select do |f|
72   #       File.mtime(f) < Time.now - (7 * 24 * 60 * 60)
73   #     end.each { |f| File.delete(f) }
74   #   end
75   #   
76   #   w.start_if do |start|
77   #     start.condition(:always)
78   #   end
79   # end
80 end