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.



58
59
60
61
# File 'lib/kurchatov/monitor.rb', line 58

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

Instance Attribute Details

#tasksObject

Returns the value of attribute tasks.



55
56
57
# File 'lib/kurchatov/monitor.rb', line 55

def tasks
  @tasks
end

Instance Method Details

#<<(plugin) ⇒ Object



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

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

#inspectObject



84
85
86
87
88
89
90
91
92
# File 'lib/kurchatov/monitor.rb', line 84

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kurchatov/monitor.rb', line 68

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