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
34 # send to block so config can set attributes
35 yield(b) if block_given?
37 # exit if the Behavior is invalid, the Behavior will have printed
38 # out its own error messages by now
58 # Instantiate a Condition of type +kind+ and pass it into the optional
59 # block. Attributes of the condition must be set in the config file
61 # must be in a _if block
63 puts "Watch#condition can only be called from inside a start_if block"
67 # create the condition
69 c = Condition.generate(kind)
70 rescue NoSuchConditionError => e
75 # send to block so config can set attributes
76 yield(c) if block_given?
78 # call prepare on the condition
81 # exit if the Condition is invalid, the Condition will have printed
82 # out its own error messages by now
87 self.conditions[@action] << c
91 [:start, :restart].each do |cmd|
92 self.conditions[cmd].each do |c|
94 puts self.name + ' ' + c.class.name + ' [ok]'
96 puts self.name + ' ' + c.class.name + ' [fail]'
111 Dir.chdir(self.cwd) do
112 call_action(c, :start, self.start)
118 Dir.chdir(self.cwd) do
119 call_action(c, :restart, self.restart)
122 self.action(:stop, c)
123 self.action(:start, c)
128 Dir.chdir(self.cwd) do
129 call_action(c, :stop, self.stop)
135 def call_action(condition, action, command)
137 (self.behaviors + [condition]).each { |b| b.send("before_#{action}") }
140 if command.kind_of?(String)
149 (self.behaviors + [condition]).each { |b| b.send("after_#{action}") }