Class: Zold::Metronome
- Inherits:
-
Object
- Object
- Zold::Metronome
- Defined in:
- lib/zold/metronome.rb
Overview
Metronome
Instance Method Summary collapse
- #add(routine) ⇒ Object
-
#initialize(log = Log::Quiet.new) ⇒ Metronome
constructor
A new instance of Metronome.
- #start ⇒ Object
- #to_text ⇒ Object
Constructor Details
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 |
#start ⇒ Object
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_text ⇒ Object
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 |