Class: Cognizant::Process::ConditionDelegate
- Inherits:
-
Object
- Object
- Cognizant::Process::ConditionDelegate
- Defined in:
- lib/cognizant/process/condition_delegate.rb
Defined Under Namespace
Classes: HistoryValue
Constant Summary collapse
- EMPTY_ARRAY =
No need to recreate one every tick.
[].freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #clear_history! ⇒ Object
- #failed_check? ⇒ Boolean
-
#initialize(name, options = {}, &block) ⇒ ConditionDelegate
constructor
A new instance of ConditionDelegate.
- #run(pid, tick_number = Time.now.to_i) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, options = {}, &block) ⇒ ConditionDelegate
Returns a new instance of ConditionDelegate.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cognizant/process/condition_delegate.rb', line 14 def initialize(name, = {}, &block) @name = name @every = .delete(:every).to_i @times = .delete(:times) || 1 @times = [@times, @times] unless @times.is_a?(Array) # handles :times => 5 @times.map(&:to_i) @do = .has_key?(:do) ? [.delete(:do)] : [:restart] @do = [block] if block clear_history! @condition = Cognizant::Process::Conditions[@name].new() end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/cognizant/process/condition_delegate.rb', line 12 def name @name end |
Instance Method Details
#clear_history! ⇒ Object
43 44 45 |
# File 'lib/cognizant/process/condition_delegate.rb', line 43 def clear_history! @history = Util::RotationalArray.new(@times.last) end |
#failed_check? ⇒ Boolean
47 48 49 |
# File 'lib/cognizant/process/condition_delegate.rb', line 47 def failed_check? @history.count { |v| v and v.critical } >= @times.first end |
#run(pid, tick_number = Time.now.to_i) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cognizant/process/condition_delegate.rb', line 30 def run(pid, tick_number = Time.now.to_i) if @last_ran_at.nil? || (@last_ran_at + @every) <= tick_number @last_ran_at = tick_number value = @condition.run(pid) @history << HistoryValue.new(@condition.format_value(value), @condition.check(value)) # puts self.to_s return @do if failed_check? end EMPTY_ARRAY end |
#to_s ⇒ Object
51 52 53 54 |
# File 'lib/cognizant/process/condition_delegate.rb', line 51 def to_s data = @history.collect { |v| v and "#{v.value}#{'*' unless v.critical}" }.join(", ") "#{@name}: [#{data}]\n" end |