Class: Kurchatov::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/kurchatov/monitor.rb

Defined Under Namespace

Classes: Task

Constant Summary collapse

CHECK_ALIVE_TIMEOUT =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stop = false) ⇒ Monitor

Returns a new instance of Monitor.



67
68
69
70
# File 'lib/kurchatov/monitor.rb', line 67

def initialize(stop = false)
  @stop_on_error = stop
  @tasks = Array.new
end

Instance Attribute Details

#tasksObject

Returns the value of attribute tasks.



64
65
66
# File 'lib/kurchatov/monitor.rb', line 64

def tasks
  @tasks
end

Instance Method Details

#<<(plugin) ⇒ Object



72
73
74
75
# File 'lib/kurchatov/monitor.rb', line 72

def <<(plugin)
  Log.debug("Add new plugin: #{plugin.inspect}")
  @tasks << Task.new(plugin)
end

#inspectObject



93
94
95
96
97
98
99
100
101
# File 'lib/kurchatov/monitor.rb', line 93

def inspect
  @tasks.map do |t|
    {
      "name" => t.name,
      "config" => t.config,
      "errors" => {"count" => t.count_errors, "last" => t.last_error, "time" => t.last_error_at}
    }
  end
end

#start!Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kurchatov/monitor.rb', line 77

def start!
  loop do
    @tasks.each do |task|
      if task.died? && @stop_on_error
        exit(Config[:ERROR_PLUGIN_REQ])
      end
      if task.stopped?
        task.stop!
        @tasks.delete(task)
      end
    end
    Log.debug("Check alive plugins [#{@tasks.count}]")
    sleep CHECK_ALIVE_TIMEOUT
  end
end