Sha256: 55d141401eec34d48192f66eb182128ae5523ec3cb5907aa4e3aa26d1b98a682

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

require 'thread'
require 'copycopter_client/cache'

module CopycopterClient
  # Starts a background thread that continually resynchronizes with the remote
  # server using the given {Cache} after a set delay.
  class Poller
    # @param options [Hash]
    # @option options [Logger] :logger where errors should be logged
    # @option options [Fixnum] :polling_delay how long to wait in between requests
    def initialize(cache, options)
      @cache         = cache
      @polling_delay = options[:polling_delay]
      @logger        = options[:logger]
      @stop          = false
    end

    def start
      Thread.new { poll } or logger.error("Couldn't start poller thread")
    end

    def stop
      @stop = true
    end

    private

    attr_reader :cache, :logger, :polling_delay

    def poll
      until @stop
        cache.sync
        logger.flush if logger.respond_to?(:flush)
        delay
      end
    rescue InvalidApiKey => error
      logger.error(error.message)
    end

    def delay
      sleep(polling_delay)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
copycopter_client-2.0.1 lib/copycopter_client/poller.rb
copycopter_client-2.0.0 lib/copycopter_client/poller.rb
copycopter_client-1.1.2 lib/copycopter_client/poller.rb
copycopter_client-1.1.1 lib/copycopter_client/poller.rb
copycopter_client-1.1.0 lib/copycopter_client/poller.rb
copycopter_client-1.0.4 lib/copycopter_client/poller.rb
copycopter_client-1.0.3 lib/copycopter_client/poller.rb