Class: Belated::Worker

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/belated/worker.rb

Overview

The worker class that actually gets the jobs from the queue and calls them. Expects the jobs to be procs or classes that have a perform method.

Instance Method Summary collapse

Methods included from Logging

#error, #log, #logger, #logger=, #warn

Constructor Details

#initialize(number: 1) ⇒ Worker

Returns a new instance of Worker.



11
12
13
14
# File 'lib/belated/worker.rb', line 11

def initialize(number: 1)
  @number = number
  start_working
end

Instance Method Details

#start_workingObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/belated/worker.rb', line 16

def start_working
  loop do
    log "Worker #{@number} fetching jobs!"
    next unless (job = Belated.fetch_job)

    break if job.is_a?(Symbol)

    log "Worker #{@number} got job: #{job.inspect}"
    log job.perform
    history_insert(job) unless job.proc_klass || !job.completed
  rescue DRb::DRbConnError, Errno::ECONNREFUSED, RangeError => e
    log e
  end
end