added MemoryUsage and factored out ProcessCondition
[god.git] / lib / god / watch.rb
blob42ba7ae8d45b4a936b483f478887ed2127e05b3b
1 module God
2   
3   class Watch < Base
4     # config
5     attr_accessor :name, :cwd, :start, :stop, :restart, :grace
6     
7     # api
8     attr_accessor :conditions
9     
10     # 
11     def initialize
12       # no grace period by default
13       self.grace = 0
14       
15       # keep track of which action each condition belongs to
16       @action = nil
17       
18       # the list of conditions for each action
19       self.conditions = {:start => [],
20                          :restart => []}
21     end
22     
23     def start_if
24       @action = :start
25       yield(self)
26       @action = nil
27     end
28     
29     def restart_if
30       @action = :restart
31       yield(self)
32       @action = nil
33     end
34     
35     # Instantiate a Condition of type +kind+ and pass it into the mandatory
36     # block. Attributes of the condition must be set in the config file
37     def condition(kind)
38       # must be in a _if block
39       unless @action
40         puts "Watch#condition can only be called from inside a start_if block"
41         exit
42       end
43       
44       # create the condition
45       begin
46         c = Condition.generate(kind)
47       rescue NoSuchConditionError => e
48         puts e.message
49         exit
50       end
51       
52       # send to block so config can set attributes
53       yield(c)
54       
55       # call prepare on the condition
56       c.prepare
57       
58       # exit if the Condition is invalid, the Condition will have printed
59       # out its own error messages by now
60       unless c.valid?
61         exit
62       end
63       
64       self.conditions[@action] << c
65     end
66     
67     def run
68       [:start, :restart].each do |cmd|
69         self.conditions[cmd].each do |c|
70           if c.test
71             puts self.name + ' ' + c.class.name + ' [ok]'
72           else
73             puts self.name + ' ' + c.class.name + ' [fail]'
74             c.after
75             action(cmd, c)
76             return
77           end
78         end
79       end
80     end
81     
82     def action(a, c)
83       case a
84       when :start
85         puts self.start
86         Dir.chdir(self.cwd) do
87           c.before_start
88           system(self.start)
89           c.after_start
90         end
91         sleep(self.grace)
92       when :restart
93         if self.restart
94           puts self.restart
95           Dir.chdir(self.cwd) do
96             c.before_restart
97             system(self.restart)
98             c.after_restart
99           end
100         else
101           self.action(:stop, c)
102           self.action(:start, c)
103         end
104         sleep(self.grace)
105       when :stop
106         puts self.stop
107         Dir.chdir(self.cwd) do
108           c.before_stop
109           system(self.stop)
110           c.after_stop
111         end
112         sleep(self.grace)
113       end      
114     end
115   end
116