7 VALID_STATES = [:init, :up, :start, :restart]
10 attr_accessor :state, :interval, :group,
11 :grace, :start_grace, :stop_grace, :restart_grace
14 attr_writer :autostart
15 def autostart?; @autostart; end
18 def_delegators :@process, :name, :uid, :gid, :start, :stop, :restart,
19 :name=, :uid=, :gid=, :start=, :stop=, :restart=,
20 :pid_file, :pid_file=, :log, :log=, :alive?
23 attr_accessor :behaviors, :metrics
31 @process = God::Process.new
33 # no grace period by default
34 self.grace = self.start_grace = self.stop_grace = self.restart_grace = 0
36 # the list of behaviors
39 # the list of conditions for each action
40 self.metrics = {:init => [],
46 self.mutex = Mutex.new
53 ###########################################################################
57 ###########################################################################
62 b = Behavior.generate(kind, self)
63 rescue NoSuchBehaviorError => e
67 # send to block so config can set attributes
68 yield(b) if block_given?
70 # abort if the Behavior is invalid, the Behavior will have printed
71 # out its own error messages by now
77 ###########################################################################
81 ###########################################################################
83 # Define a transition handler which consists of a set of conditions
84 def transition(start_states, end_states)
85 # convert to into canonical hash form
86 canonical_end_states = canonical_hash_form(end_states)
88 # for each start state do
89 Array(start_states).each do |start_state|
90 # validate start state
91 unless VALID_STATES.include?(start_state)
92 abort "Invalid state :#{start_state}. Must be one of the symbols #{VALID_STATES.map{|x| ":#{x}"}.join(', ')}"
95 # create a new metric to hold the watch, end states, and conditions
96 m = Metric.new(self, canonical_end_states)
98 # let the config file define some conditions on the metric
102 self.metrics[start_state] << m
106 ###########################################################################
110 ###########################################################################
113 self.transition(:up, :start) do |on|
119 self.transition(:up, :restart) do |on|
124 ###########################################################################
128 ###########################################################################
132 # start monitoring at the first available of the init or up states
133 if !self.metrics[:init].empty?
145 # Move from one state to another
147 msg = "#{self.name} move '#{self.state}' to '#{to_state}'"
149 LOG.log(self, :info, msg)
151 # cleanup from current state
152 from_state = self.state
154 self.metrics[from_state].each { |m| m.disable }
158 self.action(to_state)
161 if [:start, :restart].include?(to_state) && self.metrics[to_state].empty?
167 self.metrics[to_state].each { |m| m.enable }
171 self.state = to_state
177 def action(a, c = nil)
180 msg = "#{self.name} start: #{self.start.to_s}"
182 LOG.log(self, :info, msg)
183 call_action(c, :start)
184 sleep(self.start_grace + self.grace)
187 msg = "#{self.name} restart: #{self.restart.to_s}"
189 LOG.log(self, :info, msg)
190 call_action(c, :restart)
195 sleep(self.restart_grace + self.grace)
198 msg = "#{self.name} stop: #{self.stop.to_s}"
200 LOG.log(self, :info, msg)
202 call_action(c, :stop)
203 sleep(self.stop_grace + self.grace)
207 def call_action(condition, action)
209 before_items = self.behaviors
210 before_items += [condition] if condition
211 before_items.each { |b| b.send("before_#{action}") }
213 @process.call_action(action)
216 after_items = self.behaviors
217 after_items += [condition] if condition
218 after_items.each { |b| b.send("after_#{action}") }
221 def canonical_hash_form(to)
222 to.instance_of?(Symbol) ? {true => to} : to
226 God.registry.add(@process)
230 God.registry.remove(@process)