Class: Userlist::Push::Strategies::Threaded::Worker

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/userlist/push/strategies/threaded/worker.rb

Constant Summary collapse

MAX_WORKER_TIMEOUT =
30

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(queue, config = {}) ⇒ Worker

Returns a new instance of Worker.



10
11
12
13
14
15
# File 'lib/userlist/push/strategies/threaded/worker.rb', line 10

def initialize(queue, config = {})
  @queue = queue
  @config = Userlist.config.merge(config)
  @thread = Thread.new { run }
  @thread.abort_on_exception = true
end

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/userlist/push/strategies/threaded/worker.rb', line 17

def run
  logger.info 'Starting worker thread...'

  loop do
    begin
      method, *args = *queue.pop
      break if method == :stop

      client.public_send(method, *args)
    rescue StandardError => e
      logger.error "Failed to deliver payload: [#{e.class.name}] #{e.message}"
    end
  end

  logger.info "Worker thread exited with #{queue.size} tasks still in the queue..."
end

#stopObject



34
35
36
37
38
# File 'lib/userlist/push/strategies/threaded/worker.rb', line 34

def stop
  logger.info 'Stopping worker thread...'
  queue.push([:stop])
  thread.join(MAX_WORKER_TIMEOUT)
end