Class: Cuetip::Worker

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/cuetip/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, id, queues) ⇒ Worker

Returns a new instance of Worker.



13
14
15
16
17
# File 'lib/cuetip/worker.rb', line 13

def initialize(group, id, queues)
  @group = group
  @id = id
  @queues = queues
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/cuetip/worker.rb', line 7

def status
  @status
end

Instance Method Details

#request_exit!Object



19
20
21
22
# File 'lib/cuetip/worker.rb', line 19

def request_exit!
  @exit_requested = true
  interrupt_sleep
end

#runObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/cuetip/worker.rb', line 24

def run
  set_status('idle')
  loop do
    unless run_once
      interruptible_sleep(Cuetip.config.polling_interval + rand)
    end

    break if @exit_requested
  end
end

#run_onceObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cuetip/worker.rb', line 35

def run_once
  set_status('polling')
  run_callbacks :poll do
    queued_job = silence do
      if @queues.any?
        scope = Cuetip::Models::QueuedJob.from_queues(@queues)
      else
        scope = Cuetip::Models::QueuedJob
      end
      scope.find_and_lock
    end

    if queued_job
      set_status("executing #{queued_job.job.id}")
      run_callbacks :execute do
        queued_job.job.execute
      end
      set_status('idle')
      true
    else
      set_status('idle')
      false
    end
  end
end