Module: Delayed::Backend::Base

Included in:
ActiveRecord::Job, ActiveRecord::Job::Failed, Redis::Job, Redis::Job::Failed
Defined in:
lib/delayed/backend/base.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ON_HOLD_LOCKED_BY =
'on hold'
ON_HOLD_COUNT =
50

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


18
19
20
21
22
# File 'lib/delayed/backend/base.rb', line 18

def self.included(base)
  base.extend ClassMethods
  base.default_priority = Delayed::NORMAL_PRIORITY
  base.before_save :initialize_defaults
end

Instance Method Details

#batch?Boolean

Returns:

  • (Boolean)

284
285
286
# File 'lib/delayed/backend/base.rb', line 284

def batch?
  payload_object.is_a?(Delayed::Batch::PerformableBatch)
end

#expired?Boolean

Returns:

  • (Boolean)

181
182
183
# File 'lib/delayed/backend/base.rb', line 181

def expired?
  expires_at && (self.class.db_time_now >= expires_at)
end

#failed?Boolean Also known as: failed

Returns:

  • (Boolean)

176
177
178
# File 'lib/delayed/backend/base.rb', line 176

def failed?
  failed_at
end

#full_nameObject


250
251
252
253
254
255
256
257
# File 'lib/delayed/backend/base.rb', line 250

def full_name
  obj = payload_object rescue nil
  if obj && obj.respond_to?(:full_name)
    obj.full_name
  else
    name
  end
end

#hold!Object


311
312
313
314
315
316
# File 'lib/delayed/backend/base.rb', line 311

def hold!
  self.locked_by = ON_HOLD_LOCKED_BY
  self.locked_at = self.class.db_time_now
  self.attempts = ON_HOLD_COUNT
  self.save!
end

#inferred_max_attemptsObject


185
186
187
# File 'lib/delayed/backend/base.rb', line 185

def inferred_max_attempts
  self.max_attempts || Delayed::Settings.max_attempts
end

#initialize_defaultsObject


367
368
369
370
# File 'lib/delayed/backend/base.rb', line 367

def initialize_defaults
  self.queue ||= Delayed::Settings.queue
  self.run_at ||= self.class.db_time_now
end

#invoke_jobObject

Moved into its own method so that new_relic can trace it.


272
273
274
275
276
277
278
279
280
281
282
# File 'lib/delayed/backend/base.rb', line 272

def invoke_job
  Delayed::Worker.lifecycle.run_callbacks(:invoke_job, self) do
    Delayed::Job.in_delayed_job = true
    begin
      payload_object.perform
    ensure
      Delayed::Job.in_delayed_job = false
      ::ActiveRecord::Base.clear_active_connections! unless Rails.env.test?
    end
  end
end

#locked?Boolean

Returns:

  • (Boolean)

294
295
296
# File 'lib/delayed/backend/base.rb', line 294

def locked?
  !!(self.locked_at || self.locked_by)
end

#nameObject


239
240
241
242
243
244
245
246
247
248
# File 'lib/delayed/backend/base.rb', line 239

def name
  @name ||= begin
    payload = payload_object
    if payload.respond_to?(:display_name)
      payload.display_name
    else
      payload.class.name
    end
  end
end

#on_hold?Boolean

Returns:

  • (Boolean)

327
328
329
# File 'lib/delayed/backend/base.rb', line 327

def on_hold?
  self.locked_by == 'on hold' && self.locked_at && self.attempts == ON_HOLD_COUNT
end

#payload_objectObject


235
236
237
# File 'lib/delayed/backend/base.rb', line 235

def payload_object
  @payload_object ||= deserialize(self['handler'].untaint)
end

#payload_object=(object) ⇒ Object


259
260
261
262
263
264
265
266
267
268
269
# File 'lib/delayed/backend/base.rb', line 259

def payload_object=(object)
  @payload_object = object
  self['handler'] = object.to_yaml
  self['tag'] = if object.respond_to?(:tag)
    object.tag
  elsif object.is_a?(Module)
    "#{object}.perform"
  else
    "#{object.class}#perform"
  end
end

#permanent_failure(error) ⇒ Object


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/delayed/backend/base.rb', line 213

def permanent_failure(error)
  begin
    # notify the payload_object of a permanent failure
    obj = payload_object
    obj.on_permanent_failure(error) if obj && obj.respond_to?(:on_permanent_failure)
  rescue
    # don't allow a failed deserialization to prevent destroying the job
  end

  # optionally destroy the object
  destroy_self = true
  if Delayed::Worker.on_max_failures
    destroy_self = Delayed::Worker.on_max_failures.call(self, error)
  end

  if destroy_self
    self.destroy
  else
    self.fail!
  end
end

#reschedule(error = nil, time = nil) ⇒ Object

Reschedule the job in the future (when a job fails). Uses an exponential scale depending on the number of failed attempts.


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/delayed/backend/base.rb', line 191

def reschedule(error = nil, time = nil)
  begin
    obj = payload_object
    return_code = obj.on_failure(error) if obj && obj.respond_to?(:on_failure)
  rescue
    # don't allow a failed deserialization to prevent rescheduling
  end

  self.attempts += 1 unless return_code == :unlock

  if self.attempts >= self.inferred_max_attempts
    permanent_failure error || "max attempts reached"
  elsif expired?
    permanent_failure error || "job has expired"
  else
    time ||= self.reschedule_at
    self.run_at = time
    self.unlock
    self.save!
  end
end

#reschedule_atObject


298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/delayed/backend/base.rb', line 298

def reschedule_at
  new_time = self.class.db_time_now + (attempts ** 4) + 5
  begin
    if payload_object.respond_to?(:reschedule_at)
      new_time = payload_object.reschedule_at(
                                  self.class.db_time_now, attempts)
    end
  rescue
    # TODO: just swallow errors from reschedule_at ?
  end
  new_time
end

#unhold!Object


318
319
320
321
322
323
324
325
# File 'lib/delayed/backend/base.rb', line 318

def unhold!
  self.locked_by = nil
  self.locked_at = nil
  self.attempts = 0
  self.run_at = [self.class.db_time_now, self.run_at].max
  self.failed_at = nil
  self.save!
end

#unlockObject

Unlock this job (note: not saved to DB)


289
290
291
292
# File 'lib/delayed/backend/base.rb', line 289

def unlock
  self.locked_at    = nil
  self.locked_by    = nil
end