Class: CopyTunerClient::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/copy_tuner_client/poller.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(cache, options) ⇒ Poller

Returns a new instance of Poller.

Parameters:

  • options (Hash)

Options Hash (options):

  • :logger (Logger)

    where errors should be logged

  • :polling_delay (Fixnum)

    how long to wait in between requests



12
13
14
15
16
17
18
19
# File 'lib/copy_tuner_client/poller.rb', line 12

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

Instance Method Details

#startObject



21
22
23
24
25
26
27
28
# File 'lib/copy_tuner_client/poller.rb', line 21

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

#start_syncObject



38
39
40
# File 'lib/copy_tuner_client/poller.rb', line 38

def start_sync
  @command_queue.uniq_push(:sync)
end

#stopObject



30
31
32
33
34
35
36
# File 'lib/copy_tuner_client/poller.rb', line 30

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

#wait_for_downloadObject



42
43
44
# File 'lib/copy_tuner_client/poller.rb', line 42

def wait_for_download
  @cache.wait_for_download
end