Class: Zold::Metronome

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/metronome.rb

Overview

Metronome

Instance Method Summary collapse

Constructor Details

#initialize(log = Log::Quiet.new) ⇒ Metronome

Returns a new instance of Metronome.



35
36
37
38
39
40
41
# File 'lib/zold/metronome.rb', line 35

def initialize(log = Log::Quiet.new)
  @log = log
  @routines = []
  @threads = []
  @starts = {}
  @failures = {}
end

Instance Method Details

#add(routine) ⇒ Object



59
60
61
62
# File 'lib/zold/metronome.rb', line 59

def add(routine)
  @routines << routine
  @log.info("Added #{routine.class.name} to the metronome")
end

#startObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zold/metronome.rb', line 64

def start
  alive = true
  @routines.each_with_index do |r, idx|
    @threads << Thread.start do
      Thread.current.abort_on_exception = true
      Thread.current.name = "#{r.class.name}-#{idx}"
      step = 0
      while alive
        @starts[Thread.current] = Time.now
        begin
          r.exec(step)
          @log.info("Routine #{r.class.name} ##{step} done in #{Age.new(@starts[Thread.current])}")
        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(@starts[Thread.current])}")
          @log.error(Backtrace.new(e).to_s)
        end
        step += 1
        sleep(1)
      end
    end
  end
  begin
    yield(self)
  ensure
    alive = false
    @log.info("Stopping the metronome with #{@threads.count} threads: #{@threads.map(&:name).join(', ')}")
    start = Time.now
    @threads.each do |t|
      tstart = Time.now
      if t.join(60)
        @log.info("Thread #{t.name} finished in #{Age.new(tstart)}")
      else
        t.exit
        @log.info("Thread #{t.name} killed in #{Age.new(tstart)}")
      end
    end
    @log.info("Metronome stopped in #{Age.new(start)}, #{@failures.count} failures")
  end
end

#to_textObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zold/metronome.rb', line 43

def to_text
  [
    Time.now.utc.iso8601,
    'Current threads:',
    @threads.map do |t|
      [
        "#{t.name}: status=#{t.status}; alive=#{t.alive?}",
        "Most recent start: #{Age.new(@starts[t])} ago",
        t.backtrace.nil? ? 'NO BACKTRACE' : "  #{t.backtrace.join("\n  ")}"
      ].join("\n")
    end,
    'Failures:',
    @failures.map { |r, f| "#{r}\n#{f}\n" }
  ].flatten.join("\n\n")
end