Class: Delayed::Worker

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

Constant Summary collapse

@@sleep_delay =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Worker

Returns a new instance of Worker.



35
36
37
38
39
# File 'lib/delayed/worker.rb', line 35

def initialize(options={})
  @quiet = options[:quiet]
  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

#name_prefixObject

name_prefix is ignored if name is set directly



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

def name_prefix
  @name_prefix
end

Instance Method Details

#job_max_run_timeObject



17
18
19
# File 'lib/delayed/worker.rb', line 17

def job_max_run_time
  Delayed::Job.max_run_time
end

#nameObject

Every worker has a unique name which by default is the pid of the process. There are some advantages to overriding this with something which survives worker retarts: Workers can safely resume working on tasks which are locked by themselves. The worker will assume that it crashed before.



24
25
26
27
# File 'lib/delayed/worker.rb', line 24

def name
  return @name unless @name.nil?
  "#{@name_prefix}host:#{Socket.gethostname} pid:#{Process.pid}" rescue "#{@name_prefix}pid:#{Process.pid}"
end

#name=(val) ⇒ Object

Sets the name of the worker. Setting the name to nil will reset the default worker name



31
32
33
# File 'lib/delayed/worker.rb', line 31

def name=(val)
  @name = val
end

#say(text) ⇒ Object



71
72
73
74
# File 'lib/delayed/worker.rb', line 71

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

#startObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/delayed/worker.rb', line 41

def start
  say "*** Starting job worker #{Delayed::Job.worker_name}"

  trap('TERM') { say 'Exiting...'; $exit = true }
  trap('INT')  { say 'Exiting...'; $exit = true }

  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_delay)
    else
      say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
    end

    break if $exit
  end

ensure
  Delayed::Job.clear_locks!
end