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



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

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



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

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



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

def start_sync
  @command_queue.uniq_push(:sync)
end

#stopObject



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

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

#wait_for_downloadObject



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

def wait_for_download
  @cache.wait_for_download
end