Sha256: 1344f495d6d38b6ac9c3e9610c42fd7690b184bf42e13ffc32feb8760318af27

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 KB

Contents

require "concurrent/atomics"
require "thread"

module LaunchDarkly
  class PollingProcessor
    
    def initialize(config, requestor)
      @config = config
      @requestor = requestor
      @initialized = Concurrent::AtomicBoolean.new(false)
      @started = Concurrent::AtomicBoolean.new(false)
    end

    def initialized?
      @initialized.value
    end

    def start
      return unless @started.make_true
      @config.logger.info("[LDClient] Initializing polling connection")
      create_worker
    end

    def poll
      flags = @requestor.request_all_flags
      if flags
        @config.feature_store.init(flags)
        if @initialized.make_true
          @config.logger.info("[LDClient] Polling connection initialized")
        end
      end
    end

    def create_worker
      Thread.new do
        @config.logger.debug("[LDClient] Starting polling worker")
        loop do
          begin
            started_at = Time.now
            poll
            delta = @config.poll_interval - (Time.now - started_at)
            if delta > 0
              sleep(delta)
            end
          rescue StandardError => exn
            @config.logger.error("[LDClient] Exception while polling: #{exn.inspect}")
           # TODO: log_exception(__method__.to_s, exn)
          end
        end
      end
    end


    private :poll, :create_worker
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ldclient-rb-2.2.5 lib/ldclient-rb/polling.rb
ldclient-rb-2.1.5 lib/ldclient-rb/polling.rb
ldclient-rb-2.1.4 lib/ldclient-rb/polling.rb
ldclient-rb-2.1.0 lib/ldclient-rb/polling.rb
ldclient-rb-2.0.6 lib/ldclient-rb/polling.rb
ldclient-rb-2.0.5 lib/ldclient-rb/polling.rb
ldclient-rb-2.0.3 lib/ldclient-rb/polling.rb
ldclient-rb-2.0.2 lib/ldclient-rb/polling.rb
ldclient-rb-2.0.1 lib/ldclient-rb/polling.rb