Sha256: ecacbaa42a318c9d5fa6df97fe7f7ded4a8164ef86c524caf264db8742af67b0

Contents?: true

Size: 1.03 KB

Versions: 47

Compression:

Stored size: 1.03 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
      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

47 entries across 47 versions & 1 rubygems

Version Path
copy_tuner_client-0.4.2 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.4.1 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.4.0 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.3.5 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.3.4 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.3.3 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.3.2 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.3.1 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.3.0 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.2.4 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.2.3 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.2.2 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.2.1 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.2.0 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.1.1.beta19 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.1.1.beta18 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.1.1.beta17 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.1.1.beta16 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.1.1.beta15 lib/copy_tuner_client/poller.rb
copy_tuner_client-0.1.1.beta14 lib/copy_tuner_client/poller.rb