Class: Jaeger::RecurringExecutor
- Inherits:
-
Object
- Object
- Jaeger::RecurringExecutor
- Defined in:
- lib/jaeger/recurring_executor.rb
Overview
Executes a given block periodically. The block will be executed only once when interval is set to 0.
Instance Method Summary collapse
-
#initialize(interval:) ⇒ RecurringExecutor
constructor
A new instance of RecurringExecutor.
- #running? ⇒ Boolean
- #start(&block) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(interval:) ⇒ RecurringExecutor
Returns a new instance of RecurringExecutor.
7 8 9 |
# File 'lib/jaeger/recurring_executor.rb', line 7 def initialize(interval:) @interval = interval end |
Instance Method Details
#running? ⇒ Boolean
26 27 28 |
# File 'lib/jaeger/recurring_executor.rb', line 26 def running? @thread&.alive? end |
#start(&block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jaeger/recurring_executor.rb', line 11 def start(&block) raise 'Already running' if @thread @thread = Thread.new do if @interval <= 0 yield else loop do yield sleep @interval end end end end |
#stop ⇒ Object
30 31 32 33 |
# File 'lib/jaeger/recurring_executor.rb', line 30 def stop @thread.kill @thread = nil end |