Class: God::Watch
- Extended by:
- Forwardable
- Defined in:
- lib/god/watch.rb
Constant Summary collapse
- VALID_STATES =
[:init, :up, :start, :restart]
- INITIAL_STATE =
:init
Instance Attribute Summary collapse
-
#grace ⇒ Object
config.
-
#restart_grace ⇒ Object
config.
-
#start_grace ⇒ Object
config.
-
#stop_grace ⇒ Object
config.
Attributes inherited from Task
#autostart, #behaviors, #directory, #driver, #group, #initial_state, #interval, #metrics, #name, #state, #valid_states
Instance Method Summary collapse
-
#action(a, c = nil) ⇒ Object
Actions.
-
#behavior(kind) {|b| ... } ⇒ Object
Behavior.
- #call_action(condition, action) ⇒ Object
-
#initialize ⇒ Watch
constructor
A new instance of Watch.
-
#monitor ⇒ Object
Enable monitoring.
-
#register! ⇒ Object
Registration.
- #restart_if ⇒ Object
-
#start_if ⇒ Object
Simple mode.
- #stop_if ⇒ Object
- #unregister! ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from Task
#attach, #autostart?, #canonical_hash_form, #dest_desc, #detach, #handle_event, #handle_poll, #lifecycle, #log_line, #method_missing, #move, #notify, #prepare, #signal, #transition, #trigger, #trigger?, #unmonitor
Constructor Details
#initialize ⇒ Watch
Returns a new instance of Watch.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/god/watch.rb', line 19 def initialize super @process = God::Process.new # valid states self.valid_states = VALID_STATES self.initial_state = INITIAL_STATE # no grace period by default self.grace = self.start_grace = self.stop_grace = self.restart_grace = 0 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class God::Task
Instance Attribute Details
#grace ⇒ Object
config
11 12 13 |
# File 'lib/god/watch.rb', line 11 def grace @grace end |
#restart_grace ⇒ Object
config
11 12 13 |
# File 'lib/god/watch.rb', line 11 def restart_grace @restart_grace end |
#start_grace ⇒ Object
config
11 12 13 |
# File 'lib/god/watch.rb', line 11 def start_grace @start_grace end |
#stop_grace ⇒ Object
config
11 12 13 |
# File 'lib/god/watch.rb', line 11 def stop_grace @stop_grace end |
Instance Method Details
#action(a, c = nil) ⇒ Object
Actions
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/god/watch.rb', line 106 def action(a, c = nil) if Thread.current != self.driver.thread # called from outside Driver # send an async message to Driver self.driver.(:action, [a, c]) else # called from within Driver case a when :start call_action(c, :start) sleep(self.start_grace + self.grace) when :restart if self.restart call_action(c, :restart) else action(:stop, c) action(:start, c) end sleep(self.restart_grace + self.grace) when :stop call_action(c, :stop) sleep(self.stop_grace + self.grace) end end self end |
#behavior(kind) {|b| ... } ⇒ Object
Behavior
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/god/watch.rb', line 42 def behavior(kind) # create the behavior begin b = Behavior.generate(kind, self) rescue NoSuchBehaviorError => e abort e. end # send to block so config can set attributes yield(b) if block_given? # abort if the Behavior is invalid, the Behavior will have printed # out its own error messages by now abort unless b.valid? self.behaviors << b end |
#call_action(condition, action) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/god/watch.rb', line 136 def call_action(condition, action) # before before_items = self.behaviors before_items += [condition] if condition before_items.each do |b| info = b.send("before_#{action}") if info msg = "#{self.name} before_#{action}: #{info} (#{b.base_name})" applog(self, :info, msg) end end # log if self.send(action) msg = "#{self.name} #{action}: #{self.send(action).to_s}" applog(self, :info, msg) end @process.call_action(action) # after after_items = self.behaviors after_items += [condition] if condition after_items.each do |b| info = b.send("after_#{action}") if info msg = "#{self.name} after_#{action}: #{info} (#{b.base_name})" applog(self, :info, msg) end end end |
#monitor ⇒ Object
Enable monitoring
91 92 93 94 95 96 97 98 |
# File 'lib/god/watch.rb', line 91 def monitor # start monitoring at the first available of the init or up states if !self.metrics[:init].empty? self.move(:init) else self.move(:up) end end |
#register! ⇒ Object
Registration
174 175 176 |
# File 'lib/god/watch.rb', line 174 def register! God.registry.add(@process) end |
#restart_if ⇒ Object
72 73 74 75 76 |
# File 'lib/god/watch.rb', line 72 def restart_if self.transition(:up, :restart) do |on| yield(on) end end |
#start_if ⇒ Object
Simple mode
66 67 68 69 70 |
# File 'lib/god/watch.rb', line 66 def start_if self.transition(:up, :start) do |on| yield(on) end end |
#stop_if ⇒ Object
78 79 80 81 82 |
# File 'lib/god/watch.rb', line 78 def stop_if self.transition(:up, :stop) do |on| yield(on) end end |
#unregister! ⇒ Object
178 179 180 |
# File 'lib/god/watch.rb', line 178 def unregister! God.registry.remove(@process) end |
#valid? ⇒ Boolean
32 33 34 |
# File 'lib/god/watch.rb', line 32 def valid? super && @process.valid? end |