Class: LaunchDarkly::Impl::RepeatingTask

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/repeating_task.rb

Overview

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(interval, start_delay, task, logger) ⇒ RepeatingTask

Returns a new instance of RepeatingTask.

Since:

  • 5.5.0



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

#startObject

Since:

  • 5.5.0



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

#stopObject

Since:

  • 5.5.0



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