5 attr_accessor :name, :start, :stop, :restart, :interval, :grace
8 attr_accessor :behaviors, :conditions
14 def initialize(meddle)
17 # no grace period by default
20 # keep track of which action each condition belongs to
25 # the list of conditions for each action
26 self.conditions = {:start => [],
30 self.mutex = Mutex.new
36 b = Behavior.generate(kind)
37 rescue NoSuchBehaviorError => e
41 # send to block so config can set attributes
42 yield(b) if block_given?
44 # abort if the Behavior is invalid, the Behavior will have printed
45 # out its own error messages by now
63 # Instantiate a Condition of type +kind+ and pass it into the optional
64 # block. Attributes of the condition must be set in the config file
66 # must be in a _if block
68 abort "Watch#condition can only be called from inside a start_if block"
71 # create the condition
73 c = Condition.generate(kind)
74 rescue NoSuchConditionError => e
78 # send to block so config can set attributes
79 yield(c) if block_given?
81 # call prepare on the condition
84 # abort if the Condition is invalid, the Condition will have printed
85 # out its own error messages by now
90 # inherit interval from meddle if no poll condition specific interval was set
91 if c.kind_of?(PollCondition) && !c.interval
93 c.interval = self.interval
95 abort "No interval set for Condition '#{c.class.name}' in Watch '#{self.name}', and no default Watch interval from which to inherit"
99 self.conditions[@action] << c
102 # Schedule all poll conditions and register all condition events
104 [:start, :restart].each do |cmd|
105 self.conditions[cmd].each do |c|
106 @meddle.timer.register(self, c, cmd) if c.kind_of?(PollCondition)
107 c.register(self) if c.kind_of?(EventCondition)
116 call_action(c, :start, self.start)
121 call_action(c, :restart, self.restart)
129 call_action(c, :stop, self.stop)
134 def call_action(condition, action, command)
136 (self.behaviors + [condition]).each { |b| b.send("before_#{action}") }
139 if command.kind_of?(String)
148 (self.behaviors + [condition]).each { |b| b.send("after_#{action}") }