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.
- #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, #transition, #trigger, #trigger?, #unmonitor
Constructor Details
#initialize ⇒ Watch
Returns a new instance of Watch.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/god/watch.rb', line 18 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/god/watch.rb', line 99 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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/god/watch.rb', line 41 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/god/watch.rb', line 129 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
84 85 86 87 88 89 90 91 |
# File 'lib/god/watch.rb', line 84 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
167 168 169 |
# File 'lib/god/watch.rb', line 167 def register! God.registry.add(@process) end |
#restart_if ⇒ Object
71 72 73 74 75 |
# File 'lib/god/watch.rb', line 71 def restart_if self.transition(:up, :restart) do |on| yield(on) end end |
#start_if ⇒ Object
Simple mode
65 66 67 68 69 |
# File 'lib/god/watch.rb', line 65 def start_if self.transition(:up, :start) do |on| yield(on) end end |
#unregister! ⇒ Object
171 172 173 |
# File 'lib/god/watch.rb', line 171 def unregister! God.registry.remove(@process) end |
#valid? ⇒ Boolean
31 32 33 |
# File 'lib/god/watch.rb', line 31 def valid? super && @process.valid? end |