Class: LaunchDarkly::Impl::RepeatingTask
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::RepeatingTask
- Defined in:
- lib/ldclient-rb/impl/repeating_task.rb
Overview
Instance Method Summary collapse
-
#initialize(interval, start_delay, task, logger) ⇒ RepeatingTask
constructor
A new instance of RepeatingTask.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(interval, start_delay, task, logger) ⇒ RepeatingTask
Returns a new instance of RepeatingTask.
8 9 10 11 12 13 14 15 |
# File 'lib/ldclient-rb/impl/repeating_task.rb', line 8 def initialize(interval, start_delay, task, logger) @interval = interval @start_delay = start_delay @task = task @logger = logger @stopped = Concurrent::AtomicBoolean.new(false) @worker = nil end |
Instance Method Details
#start ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ldclient-rb/impl/repeating_task.rb', line 17 def start @worker = Thread.new do sleep(@start_delay) unless @start_delay.nil? || @start_delay == 0 until @stopped.value do started_at = Time.now begin @task.call rescue => e LaunchDarkly::Util.log_exception(@logger, "Uncaught exception from repeating task", e) end delta = @interval - (Time.now - started_at) if delta > 0 sleep(delta) end end end end |
#stop ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/ldclient-rb/impl/repeating_task.rb', line 36 def stop if @stopped.make_true if @worker && @worker.alive? && @worker != Thread.current @worker.run # causes the thread to wake up if it's currently in a sleep @worker.join end end end |