Class: Delayed::Worker
- Inherits:
-
Object
- Object
- Delayed::Worker
- Defined in:
- lib/delayed/worker.rb
Constant Summary collapse
- DEFAULT_SLEEP_DELAY =
5
- DEFAULT_MAX_ATTEMPTS =
25
- DEFAULT_MAX_RUN_TIME =
4.hours
- DEFAULT_DEFAULT_PRIORITY =
0
- DEFAULT_DELAY_JOBS =
true
- DEFAULT_QUEUES =
[]
- DEFAULT_READ_AHEAD =
5
Instance Attribute Summary collapse
-
#name_prefix ⇒ Object
name_prefix is ignored if name is set directly.
Class Method Summary collapse
- .after_fork ⇒ Object
- .backend=(backend) ⇒ Object
- .before_fork ⇒ Object
- .guess_backend ⇒ Object
- .lifecycle ⇒ Object
- .reset ⇒ Object
Instance Method Summary collapse
- #failed(job) ⇒ Object
-
#initialize(options = {}) ⇒ Worker
constructor
A new instance of Worker.
- #max_attempts(job) ⇒ Object
-
#name ⇒ Object
Every worker has a unique name which by default is the pid of the process.
-
#name=(val) ⇒ Object
Sets the name of the worker.
-
#reschedule(job, time = nil) ⇒ Object
Reschedule the job in the future (when a job fails).
- #run(job) ⇒ Object
- #say(text, level = Logger::INFO) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #stop? ⇒ Boolean
-
#work_off(num = 100) ⇒ Object
Do num jobs and return stats on success/failure.
Constructor Details
#initialize(options = {}) ⇒ Worker
Returns a new instance of Worker.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/delayed/worker.rb', line 98 def initialize(={}) @quiet = .has_key?(:quiet) ? [:quiet] : true self.class.min_priority = [:min_priority] if .has_key?(:min_priority) self.class.max_priority = [:max_priority] if .has_key?(:max_priority) self.class.sleep_delay = [:sleep_delay] if .has_key?(:sleep_delay) self.class.read_ahead = [:read_ahead] if .has_key?(:read_ahead) self.class.queues = [:queues] if .has_key?(:queues) self.plugins.each { |klass| klass.new } end |
Instance Attribute Details
#name_prefix ⇒ Object
name_prefix is ignored if name is set directly
29 30 31 |
# File 'lib/delayed/worker.rb', line 29 def name_prefix @name_prefix end |
Class Method Details
.after_fork ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/delayed/worker.rb', line 81 def self.after_fork # Re-open file handles @files_to_reopen.each do |file| begin file.reopen file.path, "a+" file.sync = true rescue ::Exception end end backend.after_fork end |
.backend=(backend) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/delayed/worker.rb', line 56 def self.backend=(backend) if backend.is_a? Symbol require "delayed/serialization/#{backend}" require "delayed/backend/#{backend}" backend = "Delayed::Backend::#{backend.to_s.classify}::Job".constantize end @@backend = backend silence_warnings { ::Delayed.const_set(:Job, backend) } end |
.before_fork ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/delayed/worker.rb', line 70 def self.before_fork unless @files_to_reopen @files_to_reopen = [] ObjectSpace.each_object(File) do |file| @files_to_reopen << file unless file.closed? end end backend.before_fork end |
.guess_backend ⇒ Object
66 67 68 |
# File 'lib/delayed/worker.rb', line 66 def self.guess_backend warn "[DEPRECATION] guess_backend is deprecated. Please remove it from your code." end |
.lifecycle ⇒ Object
94 95 96 |
# File 'lib/delayed/worker.rb', line 94 def self.lifecycle @lifecycle ||= Delayed::Lifecycle.new end |
.reset ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/delayed/worker.rb', line 31 def self.reset self.sleep_delay = DEFAULT_SLEEP_DELAY self.max_attempts = DEFAULT_MAX_ATTEMPTS self.max_run_time = DEFAULT_MAX_RUN_TIME self.default_priority = DEFAULT_DEFAULT_PRIORITY self.delay_jobs = DEFAULT_DELAY_JOBS self.queues = DEFAULT_QUEUES self.read_ahead = DEFAULT_READ_AHEAD end |
Instance Method Details
#failed(job) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/delayed/worker.rb', line 212 def failed(job) self.class.lifecycle.run_callbacks(:failure, self, job) do job.hook(:failure) self.class.destroy_failed_jobs ? job.destroy : job.fail! end end |
#max_attempts(job) ⇒ Object
225 226 227 |
# File 'lib/delayed/worker.rb', line 225 def max_attempts(job) job.max_attempts || self.class.max_attempts end |
#name ⇒ Object
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.
113 114 115 116 |
# File 'lib/delayed/worker.rb', line 113 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
120 121 122 |
# File 'lib/delayed/worker.rb', line 120 def name=(val) @name = val end |
#reschedule(job, time = nil) ⇒ Object
Reschedule the job in the future (when a job fails). Uses an exponential scale depending on the number of failed attempts.
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/delayed/worker.rb', line 200 def reschedule(job, time = nil) if (job.attempts += 1) < max_attempts(job) time ||= job.reschedule_at job.run_at = time job.unlock job.save! else say "PERMANENTLY removing #{job.name} because of #{job.attempts} consecutive failures.", Logger::INFO failed(job) end end |
#run(job) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/delayed/worker.rb', line 183 def run(job) runtime = Benchmark.realtime do Timeout.timeout(self.class.max_run_time.to_i) { job.invoke_job } job.destroy end say "#{job.name} completed after %.4f" % runtime return true # did work rescue DeserializationError => error job.last_error = "#{error.}\n#{error.backtrace.join("\n")}" failed(job) rescue Exception => error self.class.lifecycle.run_callbacks(:error, self, job){ handle_failed_job(job, error) } return false # work failed end |
#say(text, level = Logger::INFO) ⇒ Object
219 220 221 222 223 |
# File 'lib/delayed/worker.rb', line 219 def say(text, level = Logger::INFO) text = "[Worker(#{name})] #{text}" puts text unless @quiet logger.add level, "#{Time.now.strftime('%FT%T%z')}: #{text}" if logger end |
#start ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/delayed/worker.rb', line 124 def start trap('TERM') { say 'Exiting...'; stop } trap('INT') { say 'Exiting...'; stop } say "Starting job worker" self.class.lifecycle.run_callbacks(:execute, self) do loop do self.class.lifecycle.run_callbacks(:loop, self) do result = nil realtime = Benchmark.realtime do result = work_off end count = result.sum break if stop? if count.zero? sleep(self.class.sleep_delay) else say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last] end end break if stop? end end end |
#stop ⇒ Object
155 156 157 |
# File 'lib/delayed/worker.rb', line 155 def stop @exit = true end |
#stop? ⇒ Boolean
159 160 161 |
# File 'lib/delayed/worker.rb', line 159 def stop? !!@exit end |
#work_off(num = 100) ⇒ Object
Do num jobs and return stats on success/failure. Exit early if interrupted.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/delayed/worker.rb', line 165 def work_off(num = 100) success, failure = 0, 0 num.times do case reserve_and_run_one_job when true success += 1 when false failure += 1 else break # leave if no work could be done end break if stop? # leave if we're exiting end return [success, failure] end |