class Zold::Metronome

Metronome

Public Class Methods

new(log = Log::NULL) click to toggle source
# File lib/zold/metronome.rb, line 37
def initialize(log = Log::NULL)
  @log = log
  @routines = []
  @threads = ThreadPool.new('metronome', log: log)
  @failures = {}
end

Public Instance Methods

add(routine) click to toggle source
# File lib/zold/metronome.rb, line 54
def add(routine)
  @routines << routine
  @log.info("Added #{routine.class.name} to the metronome")
end
start() { |self| ... } click to toggle source
# File lib/zold/metronome.rb, line 59
    def start
      @routines.each_with_index do |r, idx|
        @threads.add do
          step = 0
          Endless.new("#{r.class.name}-#{idx}", log: @log).run do
            Thread.current.thread_variable_set(:start, Time.now)
            step += 1
            begin
              r.exec(step)
              @log.debug("Routine #{r.class.name} ##{step} done \
in #{Age.new(Thread.current.thread_variable_get(:start))}")
            rescue StandardError => e
              @failures[r.class.name] = Time.now.utc.iso8601 + "\n" + Backtrace.new(e).to_s
              @log.error("Routine #{r.class.name} ##{step} failed \
in #{Age.new(Thread.current.thread_variable_get(:start))}")
              raise e
            end
            sleep(1)
          end
        end
      end
      begin
        yield(self)
      ensure
        @threads.kill
      end
    end
to_text() click to toggle source
# File lib/zold/metronome.rb, line 44
def to_text
  [
    Time.now.utc.iso8601,
    'Current threads:',
    @threads.to_s,
    'Failures:',
    @failures.map { |r, f| "#{r}\n#{f}\n" }
  ].flatten.join("\n\n")
end