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

#stopObject



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

def stop
  @threads.each do |t|
    t.exit
    @log.debug("#{t.name} thread stopped")
  end
  @log.info("#{@threads.count} routine threads stopped")
end