Class: God::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/god/metric.rb

Overview

Metrics are responsible for holding watch conditions. An instance of Metric is yielded to blocks in the start_if, restart_if, stop_if, and transition methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watch, destination = nil) ⇒ Metric

Initialize a new Metric.

watch - The Watch. destination - The optional destination Hash in canonical hash form.



20
21
22
23
24
# File 'lib/god/metric.rb', line 20

def initialize(watch, destination = nil)
  self.watch = watch
  self.destination = destination
  self.conditions = []
end

Instance Attribute Details

#conditionsObject

The Array of Condition instances.



14
15
16
# File 'lib/god/metric.rb', line 14

def conditions
  @conditions
end

#destinationObject

The destination Hash in canonical hash form. Example: { true => :up, false => :restart}



11
12
13
# File 'lib/god/metric.rb', line 11

def destination
  @destination
end

#watchObject

The Watch.



7
8
9
# File 'lib/god/metric.rb', line 7

def watch
  @watch
end

Instance Method Details

#condition(kind) {|c| ... } ⇒ Object

Public: Instantiate the given Condition and pass it into the optional block. Attributes of the condition must be set in the config file.

kind - The Symbol name of the condition.

Returns nothing.

Yields:

  • (c)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/god/metric.rb', line 32

def condition(kind)
  # Create the condition.
  begin
    c = Condition.generate(kind, self.watch)
  rescue NoSuchConditionError => e
    abort e.message
  end

  # Send to block so config can set attributes.
  yield(c) if block_given?

  # Prepare the condition.
  c.prepare

  # Test generic and specific validity.
  unless Condition.valid?(c) && c.valid?
    abort "Exiting on invalid condition"
  end

  # Inherit interval from watch if no poll condition specific interval was
  # set.
  if c.kind_of?(PollCondition) && !c.interval
    if self.watch.interval
      c.interval = self.watch.interval
    else
      abort "No interval set for Condition '#{c.class.name}' in Watch " +
            "'#{self.watch.name}', and no default Watch interval from " +
            "which to inherit."
    end
  end

  # Add the condition to the list.
  self.conditions << c
end

#disableObject

Disable all of this Metric’s conditions. Poll conditions will be halted and event/trigger conditions will be deregistered.

Returns nothing.



81
82
83
84
85
# File 'lib/god/metric.rb', line 81

def disable
  self.conditions.each do |c|
    self.watch.detach(c)
  end
end

#enableObject

Enable all of this Metric’s conditions. Poll conditions will be scheduled and event/trigger conditions will be registered.

Returns nothing.



71
72
73
74
75
# File 'lib/god/metric.rb', line 71

def enable
  self.conditions.each do |c|
    self.watch.attach(c)
  end
end