5 attr_accessor :name, :cwd, :start, :stop, :restart, :grace
8 attr_accessor :behaviors, :conditions
12 # no grace period by default
15 # keep track of which action each condition belongs to
20 # the list of conditions for each action
21 self.conditions = {:start => [],
28 b = Behavior.generate(kind)
29 rescue NoSuchBehaviorError => e
33 # send to block so config can set attributes
34 yield(b) if block_given?
36 # abort if the Behavior is invalid, the Behavior will have printed
37 # out its own error messages by now
57 # Instantiate a Condition of type +kind+ and pass it into the optional
58 # block. Attributes of the condition must be set in the config file
60 # must be in a _if block
62 abort "Watch#condition can only be called from inside a start_if block"
65 # create the condition
67 c = Condition.generate(kind)
68 rescue NoSuchConditionError => e
72 # send to block so config can set attributes
73 yield(c) if block_given?
75 # call prepare on the condition
78 # abort if the Condition is invalid, the Condition will have printed
79 # out its own error messages by now
84 self.conditions[@action] << c
88 [:start, :restart].each do |cmd|
89 self.conditions[cmd].each do |c|
91 puts self.name + ' ' + c.class.name + ' [ok]'
93 puts self.name + ' ' + c.class.name + ' [fail]'
108 Dir.chdir(self.cwd) do
109 call_action(c, :start, self.start)
115 Dir.chdir(self.cwd) do
116 call_action(c, :restart, self.restart)
119 self.action(:stop, c)
120 self.action(:start, c)
125 Dir.chdir(self.cwd) do
126 call_action(c, :stop, self.stop)
132 def call_action(condition, action, command)
134 (self.behaviors + [condition]).each { |b| b.send("before_#{action}") }
137 if command.kind_of?(String)
146 (self.behaviors + [condition]).each { |b| b.send("after_#{action}") }