Class: Resque::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/hirefire/workers/resque/worker.rb

Instance Method Summary collapse

Instance Method Details

#work(interval = 5.0, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hirefire/workers/resque/worker.rb', line 9

def work(interval = 5.0, &block)
  interval = Float(interval)
  $0 = "resque: Starting"
  startup

  loop do
    break if shutdown?
    ::Resque::Job.environment.hire

    if not @paused and job = reserve
      log "got: #{job.inspect}"
      run_hook :before_fork, job
      working_on job

      if @child = fork
        rand # Reseeding
        procline "Forked #{@child} at #{Time.now.to_i}"
        Process.wait
      else
        procline "Processing #{job.queue} since #{Time.now.to_i}"
        perform(job, &block)
        exit! unless @cant_fork
      end

      done_working
      @child = nil
    else

      ##
      # HireFire Hook
      # After the last job in the queue finishes processing, Resque::Job.jobs will return 0.
      # This means that there aren't any more jobs to process for any of the workers.
      # If this is the case it'll command the current environment to fire all the hired workers
      # and then immediately break out of this infinite loop.
      if (::Resque::Job.jobs + ::Resque::Job.working) == 0
        break if ::Resque::Job.environment.fire
      end

      sleep(interval)

    end
  end

ensure
  unregister_worker
end