Class: EQ::Working::Worker

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Logging
Defined in:
lib/eq-working/worker.rb

Instance Method Summary collapse

Methods included from Logging

#debug, #info, #log_error

Constructor Details

#initialize(autostart = true) ⇒ Worker

Returns a new instance of Worker.



6
7
8
9
# File 'lib/eq-working/worker.rb', line 6

def initialize autostart=true
  # start working async
  process_jobs! if autostart
end

Instance Method Details

#look_for_a_jobObject



33
34
35
# File 'lib/eq-working/worker.rb', line 33

def look_for_a_job
  EQ.queue.reserve if EQ.queue && EQ.queue.alive?
end

#process(job) ⇒ TrueClass, FalseClass

Returns true when job is done and deleted.

Parameters:

Returns:

  • (TrueClass, FalseClass)

    true when job is done and deleted



39
40
41
42
43
# File 'lib/eq-working/worker.rb', line 39

def process job
  debug "processing #{job.inspect}"
  job.perform
  EQ.queue.pop! job.id
end

#process_jobsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/eq-working/worker.rb', line 11

def process_jobs
  sleep EQ.config.worker_delay

  # TODO check if this is really what we want here, does it stop gracefully?
  while Celluloid::Actor.current.alive?
    if job = look_for_a_job
      debug "got #{job.inspect}"
      
      # this should happen in sync mode, because we don't want to pick
      # too much jobs
      process job
    else
      # currently no job
      sleep 0.05
    end
  end
rescue Celluloid::DeadActorError
  log_error 'dead'
  sleep 0.05
  retry
end