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.



31
32
33
34
# File 'lib/zold/metronome.rb', line 31

def initialize(log = Log::Quiet.new)
  @log = log
  @threads = []
end

Instance Method Details

#add(routine) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zold/metronome.rb', line 36

def add(routine)
  @threads << Thread.start do
    Thread.current.name = routine.class.name
    step = 0
    loop do
      start = Time.now
      VerboseThread.new(@log).run(true) do
        routine.exec(step)
      end
      sleep(1)
      step += 1
      @log.debug("Routine #{routine.class.name} ##{step} done in #{((Time.now - start) / 60).round(2)}s")
    end
  end
  @log.info("Added #{routine.class.name} to the metronome")
end

#stopObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/zold/metronome.rb', line 53

def stop
  @log.info("Terminating the metronome with #{@threads.count} threads...")
  start = Time.now
  @threads.each do |t|
    tstart = Time.now
    t.exit
    @log.info("Thread #{t.name} terminated in #{((Time.now - tstart) / 60).round(2)}s")
  end
  @log.info("Metronome stopped in #{((Time.now - start) / 60).round(2)}s")
end