Class: LaunchDarkly::Impl::RepeatingTask
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::RepeatingTask
- Defined in:
- lib/ldclient-rb/impl/repeating_task.rb
Overview
Instance Attribute Summary collapse
- #name ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(interval, start_delay, task, logger, name) ⇒ RepeatingTask
constructor
A new instance of RepeatingTask.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(interval, start_delay, task, logger, name) ⇒ RepeatingTask
Returns a new instance of RepeatingTask.
10 11 12 13 14 15 16 17 18 |
# File 'lib/ldclient-rb/impl/repeating_task.rb', line 10 def initialize(interval, start_delay, task, logger, name) @interval = interval @start_delay = start_delay @task = task @logger = logger @stopped = Concurrent::AtomicBoolean.new(false) @worker = nil @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
8 9 10 |
# File 'lib/ldclient-rb/impl/repeating_task.rb', line 8 def name @name end |
Instance Method Details
#start ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ldclient-rb/impl/repeating_task.rb', line 20 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 @worker.name = @name end |
#stop ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/ldclient-rb/impl/repeating_task.rb', line 41 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 |