Sha256: 2206f8997f4db0345c619bc9677e65ee0bf451b0720eb0dd37ce62f45702c522

Contents?: true

Size: 1.16 KB

Versions: 19

Compression:

Stored size: 1.16 KB

Contents

require "ldclient-rb/util"

require "concurrent/atomics"

module LaunchDarkly
  module Impl
    class RepeatingTask
      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

      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

      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
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
launchdarkly-server-sdk-8.8.2 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.8.1 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.8.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.7.1 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.7.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.6.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.5.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.4.2 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.4.1 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.4.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.3.1 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-7.3.3 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.3.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.2.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.1.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-7.3.2 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-8.0.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-7.3.0 lib/ldclient-rb/impl/repeating_task.rb
launchdarkly-server-sdk-7.2.0 lib/ldclient-rb/impl/repeating_task.rb