class Malevich::Monitor

Constants

CHECK_ALIVE

Attributes

tasks[R]

Public Class Methods

new() click to toggle source
# File lib/malevich/monitor.rb, line 11
def initialize
  @tasks = Array.new
end

Public Instance Methods

<<(obj) click to toggle source
# File lib/malevich/monitor.rb, line 19
def <<(obj)
  return false unless obj.respond_to?(:name) || obj.respond_to?(:run!)
  th = Thread.new do
    obj.run!
  end
  log :info, "Add '#{obj.class}(#{obj.name})'"
  @tasks << [th, obj]
end
plugins() click to toggle source
# File lib/malevich/monitor.rb, line 15
def plugins
  @tasks.select { |t| t[1].is_a?(Malevich::Plugin) }.map { |x| {x[1].name => x[1].settings.to_hash} }
end
run!() click to toggle source
# File lib/malevich/monitor.rb, line 34
def run!
  loop do
    @tasks.each_with_index do |task, i|
      next if task[0].alive?
      # start new thread
      log :error, "Thread for '#{task[1].class}(#{task[1].name})' is dead, start it"
      @tasks.delete_at(i)
      self << task[1]
    end
    log :debug, "Check alive #{@tasks.count} threads"
    ObjectSpace.garbage_collect if run_gc?
    sleep CHECK_ALIVE
  end
end
run_gc?() click to toggle source
# File lib/malevich/monitor.rb, line 28
def run_gc?
  @gc_counter ||= 0
  @gc_counter += 1
  @gc_counter > 100 ? @gc_counter = 0 : false
end