class CopyTunerClient::Poller

server using the given {Cache} after a set delay.
Starts a background thread that continually resynchronizes with the remote

def initialize(cache, options)

Options Hash: (**options)
  • :polling_delay (Fixnum) -- how long to wait in between requests
  • :logger (Logger) -- where errors should be logged

Parameters:
  • options (Hash) --
def initialize(cache, options)
  @cache         = cache
  @polling_delay = options[:polling_delay]
  @logger        = options[:logger]
  @command_queue = CopyTunerClient::QueueWithTimeout.new
  @mutex         = Mutex.new
  @thread        = nil
end

def poll

def poll
  loop do
    cache.sync
    logger.flush if logger.respond_to?(:flush)
    begin
      command = @command_queue.pop_with_timeout(polling_delay)
      break if command == :stop
    rescue ThreadError
      # timeout
    end
  end
  @logger.info 'stop poller thread'
rescue InvalidApiKey => error
  logger.error(error.message)
end

def start

def start
  @mutex.synchronize do
    if @thread.nil?
      @logger.info 'start poller thread'
      @thread = Thread.new { poll } or logger.error("Couldn't start poller thread")
    end
  end
end

def start_sync

def start_sync
  @command_queue.uniq_push(:sync)
end

def stop

def stop
  @mutex.synchronize do
    @command_queue.uniq_push(:stop)
    @thread.join if @thread
    @thread = nil
  end
end

def wait_for_download

def wait_for_download
  @cache.wait_for_download
end