Class: Delayed::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/delayed/worker.rb

Constant Summary collapse

SLEEP =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Worker

Returns a new instance of Worker.



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

def initialize(options={})
  @quiet = options[:quiet]
  @idle_after = options[:idle_after] || 60
  Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority)
  Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority)
end

Instance Attribute Details

#idle_afterObject (readonly)

Returns the value of attribute idle_after.



5
6
7
# File 'lib/delayed/worker.rb', line 5

def idle_after
  @idle_after
end

#next_idleObject (readonly)

Returns the value of attribute next_idle.



5
6
7
# File 'lib/delayed/worker.rb', line 5

def next_idle
  @next_idle
end

Instance Method Details

#say(text) ⇒ Object



58
59
60
61
# File 'lib/delayed/worker.rb', line 58

def say(text)
  puts text unless @quiet
  logger.info text if logger
end

#startObject



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
55
56
# File 'lib/delayed/worker.rb', line 20

def start
  say "*** Starting job worker #{Delayed::Job.worker_name}"
  
  trap('TERM') { say 'Exiting...'; $exit = true }
  trap('INT')  { say 'Exiting...'; $exit = true }
  
  @next_idle = Time.new + @idle_after
  
  loop do
    result = nil

    realtime = Benchmark.realtime do
      result = Delayed::Job.work_off
    end

    count = result.sum

    break if $exit

    if count.zero?
      sleep(SLEEP)
    else
      @next_idle = Time.new + @idle_after
      say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
    end
    
    if Time.new > @next_idle
      @next_idle = Time.new + @idle_after
      on_idle
    end
    
    break if $exit
  end

ensure
  Delayed::Job.clear_locks!
end