Class: God::Task
- Inherits:
-
Object
- Object
- God::Task
- Defined in:
- lib/god/task.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#autostart ⇒ Object
writeonly
Sets the attribute autostart.
-
#behaviors ⇒ Object
api.
-
#directory ⇒ Object
api.
-
#driver ⇒ Object
Returns the value of attribute driver.
-
#group ⇒ Object
Returns the value of attribute group.
-
#initial_state ⇒ Object
Returns the value of attribute initial_state.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#metrics ⇒ Object
api.
-
#name ⇒ Object
Returns the value of attribute name.
-
#state ⇒ Object
api.
-
#valid_states ⇒ Object
Returns the value of attribute valid_states.
Instance Method Summary collapse
-
#action(a, c = nil) ⇒ Object
Perform the given action
a
is the action Symbolc
is the Condition. -
#attach(condition) ⇒ Object
Events.
- #autostart? ⇒ Boolean
-
#canonical_hash_form(to) ⇒ Object
Advanced mode.
-
#dest_desc(metric, condition) ⇒ Object
Format the destination specification for use in debug logging
metric
is the Metriccondition
is the Condition. - #detach(condition) ⇒ Object
-
#handle_event(condition) ⇒ Object
Asynchronously evaluate and handle the given event condition.
-
#handle_poll(condition) ⇒ Object
Evaluate and handle the given poll condition.
-
#initialize ⇒ Task
constructor
A new instance of Task.
- #lifecycle {|m| ... } ⇒ Object
-
#log_line(watch, metric, condition, result) ⇒ Object
Log info about the condition and return the list of messages logged
watch
is the Watchmetric
is the Metriccondition
is the Conditionresult
is the Boolean result of the condition test evaluation. -
#method_missing(sym, *args) ⇒ Object
Actions.
-
#monitor ⇒ Object
Enable monitoring.
-
#move(to_state) ⇒ Object
Move to the givent state
to_state
is the Symbol representing the state to move to. -
#notify(condition, message) ⇒ Object
Notify all recipeients of the given condition with the specified message
condition
is the Conditionmessage
is the String message to send. - #prepare ⇒ Object
-
#register! ⇒ Object
Registration.
- #signal(sig) ⇒ Object
-
#transition(start_states, end_states) ⇒ Object
Define a transition handler which consists of a set of conditions.
-
#trigger(condition) ⇒ Object
Notify the Driver that an EventCondition has triggered.
-
#trigger?(metric, result) ⇒ Boolean
Determine whether a trigger happened
metric
is the Metricresult
is the result from the condition’s test. -
#unmonitor ⇒ Object
Disable monitoring.
- #unregister! ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ Task
Returns a new instance of Task.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/god/task.rb', line 12 def initialize @autostart ||= true # initial state is unmonitored self.state = :unmonitored # the list of behaviors self.behaviors = [] # the list of conditions for each action self.metrics = {nil => [], :unmonitored => [], :stop => []} # the condition -> metric lookup self.directory = {} # driver self.driver = Driver.new(self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
Actions
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/god/task.rb', line 217 def method_missing(sym, *args) unless (sym.to_s =~ /=$/) super end base = sym.to_s.chop.intern unless self.valid_states.include?(base) super end self.class.send(:attr_accessor, base) self.send(sym, *args) end |
Instance Attribute Details
#autostart=(value) ⇒ Object (writeonly)
Sets the attribute autostart
6 7 8 |
# File 'lib/god/task.rb', line 6 def autostart=(value) @autostart = value end |
#behaviors ⇒ Object
api
10 11 12 |
# File 'lib/god/task.rb', line 10 def behaviors @behaviors end |
#directory ⇒ Object
api
10 11 12 |
# File 'lib/god/task.rb', line 10 def directory @directory end |
#driver ⇒ Object
Returns the value of attribute driver.
4 5 6 |
# File 'lib/god/task.rb', line 4 def driver @driver end |
#group ⇒ Object
Returns the value of attribute group.
4 5 6 |
# File 'lib/god/task.rb', line 4 def group @group end |
#initial_state ⇒ Object
Returns the value of attribute initial_state.
4 5 6 |
# File 'lib/god/task.rb', line 4 def initial_state @initial_state end |
#interval ⇒ Object
Returns the value of attribute interval.
4 5 6 |
# File 'lib/god/task.rb', line 4 def interval @interval end |
#metrics ⇒ Object
api
10 11 12 |
# File 'lib/god/task.rb', line 10 def metrics @metrics end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/god/task.rb', line 4 def name @name end |
#state ⇒ Object
api
10 11 12 |
# File 'lib/god/task.rb', line 10 def state @state end |
#valid_states ⇒ Object
Returns the value of attribute valid_states.
4 5 6 |
# File 'lib/god/task.rb', line 4 def valid_states @valid_states end |
Instance Method Details
#action(a, c = nil) ⇒ Object
Perform the given action
+a+ is the action Symbol
+c+ is the Condition
Returns Task (self)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/god/task.rb', line 237 def action(a, c = nil) if !self.driver.in_driver_context? # called from outside Driver # send an async message to Driver self.driver.(:action, [a, c]) else # called from within Driver if self.respond_to?(a) command = self.send(a) case command when String msg = "#{self.name} #{a}: #{command}" applog(self, :info, msg) system(command) when Proc msg = "#{self.name} #{a}: lambda" applog(self, :info, msg) command.call else raise NotImplementedError end end end end |
#attach(condition) ⇒ Object
Events
273 274 275 276 277 278 279 280 |
# File 'lib/god/task.rb', line 273 def attach(condition) case condition when PollCondition self.driver.schedule(condition, 0) when EventCondition, TriggerCondition condition.register end end |
#autostart? ⇒ Boolean
7 |
# File 'lib/god/task.rb', line 7 def autostart?; @autostart; end |
#canonical_hash_form(to) ⇒ Object
Advanced mode
67 68 69 |
# File 'lib/god/task.rb', line 67 def canonical_hash_form(to) to.instance_of?(Symbol) ? {true => to} : to end |
#dest_desc(metric, condition) ⇒ Object
Format the destination specification for use in debug logging
+metric+ is the Metric
+condition+ is the Condition
Returns String
451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/god/task.rb', line 451 def dest_desc(metric, condition) if condition.transition {true => condition.transition}.inspect else if metric.destination metric.destination.inspect else 'none' end end end |
#detach(condition) ⇒ Object
282 283 284 285 286 287 288 289 |
# File 'lib/god/task.rb', line 282 def detach(condition) case condition when PollCondition condition.reset when EventCondition, TriggerCondition condition.deregister end end |
#handle_event(condition) ⇒ Object
Asynchronously evaluate and handle the given event condition. Handles logging notifications, and moving to the new state if necessary
+condition+ is the Condition to handle
Returns nothing
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/god/task.rb', line 375 def handle_event(condition) # lookup metric metric = self.directory[condition] # log = self.log_line(self, metric, condition, true) # notify if condition.notify self.notify(condition, .last) end # get the destination dest = if condition.transition # condition override condition.transition else # regular metric.destination && metric.destination[true] end if dest self.move(dest) end end |
#handle_poll(condition) ⇒ Object
Evaluate and handle the given poll condition. Handles logging notifications, and moving to the new state if necessary
+condition+ is the Condition to handle
Returns nothing
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/god/task.rb', line 316 def handle_poll(condition) # lookup metric metric = self.directory[condition] # run the test begin result = condition.test rescue Object => e cname = condition.class.to_s.split('::').last = format("Unhandled exception in %s condition - (%s): %s\n%s", cname, e.class, e., e.backtrace.join("\n")) applog(self, :error, ) result = false end # log = self.log_line(self, metric, condition, result) # notify if result && condition.notify self.notify(condition, .last) end # after-condition condition.after # get the destination dest = if result && condition.transition # condition override condition.transition else # regular metric.destination && metric.destination[result] end # transition or reschedule if dest # transition begin self.move(dest) rescue EventRegistrationFailedError msg = self.name + ' Event registration failed, moving back to previous state' applog(self, :info, msg) dest = self.state retry end else # reschedule self.driver.schedule(condition) end end |
#lifecycle {|m| ... } ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/god/task.rb', line 106 def lifecycle # create a new metric to hold the watch and conditions m = Metric.new(self) # let the config file define some conditions on the metric yield(m) # populate the condition -> metric directory m.conditions.each do |c| self.directory[c] = m end # record the metric self.metrics[nil] << m end |
#log_line(watch, metric, condition, result) ⇒ Object
Log info about the condition and return the list of messages logged
+watch+ is the Watch
+metric+ is the Metric
+condition+ is the Condition
+result+ is the Boolean result of the condition test evaluation
Returns String[]
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/god/task.rb', line 418 def log_line(watch, metric, condition, result) status = if self.trigger?(metric, result) "[trigger]" else "[ok]" end = [] # log info if available if condition.info Array(condition.info).each do |condition_info| << "#{watch.name} #{status} #{condition_info} (#{condition.base_name})" applog(watch, :info, .last) end else << "#{watch.name} #{status} (#{condition.base_name})" applog(watch, :info, .last) end # log = watch.name + ' ' + condition.base_name + " [#{result}] " + self.dest_desc(metric, condition) applog(watch, :debug, ) end |
#monitor ⇒ Object
Enable monitoring
Returns nothing
131 132 133 |
# File 'lib/god/task.rb', line 131 def monitor self.move(self.initial_state) end |
#move(to_state) ⇒ Object
Move to the givent state
+to_state+ is the Symbol representing the state to move to
Returns Task (self)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/god/task.rb', line 146 def move(to_state) if !self.driver.in_driver_context? # called from outside Driver # send an async message to Driver self.driver.(:move, [to_state]) else # called from within Driver # record original info orig_to_state = to_state from_state = self.state # log msg = "#{self.name} move '#{from_state}' to '#{to_state}'" applog(self, :info, msg) # cleanup from current state self.driver.clear_events self.metrics[from_state].each { |m| m.disable } if to_state == :unmonitored self.metrics[nil].each { |m| m.disable } end # perform action self.action(to_state) # enable simple mode if [:start, :restart].include?(to_state) && self.metrics[to_state].empty? to_state = :up end # move to new state self.metrics[to_state].each { |m| m.enable } # if no from state, enable lifecycle metric if from_state == :unmonitored self.metrics[nil].each { |m| m.enable } end # set state self.state = to_state # broadcast to interested TriggerConditions Trigger.broadcast(self, :state_change, [from_state, orig_to_state]) # log msg = "#{self.name} moved '#{from_state}' to '#{to_state}'" applog(self, :info, msg) end self end |
#notify(condition, message) ⇒ Object
Notify all recipeients of the given condition with the specified message
+condition+ is the Condition
+message+ is the String message to send
Returns nothing
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/god/task.rb', line 468 def notify(condition, ) spec = Contact.normalize(condition.notify) unmatched = [] # resolve contacts resolved_contacts = spec[:contacts].inject([]) do |acc, contact_name_or_group| cons = Array(God.contacts[contact_name_or_group] || God.contact_groups[contact_name_or_group]) unmatched << contact_name_or_group if cons.empty? acc += cons acc end # warn about unmatched contacts unless unmatched.empty? msg = "#{condition.watch.name} no matching contacts for '#{unmatched.join(", ")}'" applog(condition.watch, :warn, msg) end # notify each contact resolved_contacts.each do |c| host = `hostname`.chomp rescue 'none' begin c.notify(, Time.now, spec[:priority], spec[:category], host) msg = "#{condition.watch.name} #{c.info ? c.info : "notification sent for contact: #{c.name}"} (#{c.base_name})" applog(condition.watch, :info, msg % []) rescue Exception => e applog(condition.watch, :error, "#{e.} #{e.backtrace}") msg = "#{condition.watch.name} Failed to deliver notification for contact: #{c.name} (#{c.base_name})" applog(condition.watch, :error, msg % []) end end end |
#prepare ⇒ Object
31 32 33 34 35 |
# File 'lib/god/task.rb', line 31 def prepare self.valid_states.each do |state| self.metrics[state] ||= [] end end |
#register! ⇒ Object
Registration
297 298 299 |
# File 'lib/god/task.rb', line 297 def register! # override if necessary end |
#signal(sig) ⇒ Object
207 208 209 |
# File 'lib/god/task.rb', line 207 def signal(sig) # noop end |
#transition(start_states, end_states) ⇒ Object
Define a transition handler which consists of a set of conditions
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/god/task.rb', line 72 def transition(start_states, end_states) # convert end_states into canonical hash form canonical_end_states = canonical_hash_form(end_states) Array(start_states).each do |start_state| # validate start state unless self.valid_states.include?(start_state) abort "Invalid state :#{start_state}. Must be one of the symbols #{self.valid_states.map{|x| ":#{x}"}.join(', ')}" end # create a new metric to hold the watch, end states, and conditions m = Metric.new(self, canonical_end_states) if block_given? # let the config file define some conditions on the metric yield(m) else # add an :always condition if no block m.condition(:always) do |c| c.what = true end end # populate the condition -> metric directory m.conditions.each do |c| self.directory[c] = m end # record the metric self.metrics[start_state] ||= [] self.metrics[start_state] << m end end |
#trigger(condition) ⇒ Object
Notify the Driver that an EventCondition has triggered
Returns nothing
203 204 205 |
# File 'lib/god/task.rb', line 203 def trigger(condition) self.driver.(:handle_event, [condition]) end |
#trigger?(metric, result) ⇒ Boolean
Determine whether a trigger happened
+metric+ is the Metric
+result+ is the result from the condition's test
Returns Boolean
407 408 409 |
# File 'lib/god/task.rb', line 407 def trigger?(metric, result) metric.destination && metric.destination[result] end |
#unmonitor ⇒ Object
Disable monitoring
Returns nothing
138 139 140 |
# File 'lib/god/task.rb', line 138 def unmonitor self.move(:unmonitored) end |
#unregister! ⇒ Object
301 302 303 |
# File 'lib/god/task.rb', line 301 def unregister! driver.shutdown end |
#valid? ⇒ Boolean
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/god/task.rb', line 37 def valid? valid = true # a name must be specified if self.name.nil? valid = false applog(self, :error, "No name was specified") end # valid_states must be specified if self.valid_states.nil? valid = false applog(self, :error, "No valid_states array was specified") end # valid_states must be specified if self.initial_state.nil? valid = false applog(self, :error, "No initial_state was specified") end valid end |