Sha256: 7135208ba9db071848945ad27d58a4c8630de408e1e9075a6c3b96317e04921d

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require 'thread'
require 'copy_tuner_client/cache'

module CopyTunerClient
  # 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
      @logger.info 'start poller'
      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

3 entries across 3 versions & 1 rubygems

Version Path
copy_tuner_client-0.4.5 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.4.4 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.4.3 lib/copy_tuner_client/poller.rb