Module: Delayed::Backend::Base

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ON_HOLD_BLOCKER =
"blocker job"
ON_HOLD_LOCKED_BY =
"on hold"
ON_HOLD_COUNT =
50

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

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)


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

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

#expired?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/delayed/backend/base.rb', line 222

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

#failed?Boolean Also known as: failed

Returns:

  • (Boolean)


217
218
219
# File 'lib/delayed/backend/base.rb', line 217

def failed?
  failed_at
end

#full_nameObject



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

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

#hold!Object



355
356
357
358
359
360
# File 'lib/delayed/backend/base.rb', line 355

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

#inferred_max_attemptsObject



226
227
228
# File 'lib/delayed/backend/base.rb', line 226

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

#initialize_defaultsObject



418
419
420
421
# File 'lib/delayed/backend/base.rb', line 418

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.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/delayed/backend/base.rb', line 309

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
      unless Rails.env.test?
        if Rails.version < "6.1"
          ::ActiveRecord::Base.clear_active_connections!
        else
          ::ActiveRecord::Base.clear_active_connections!(nil)
        end
      end
    end
  end
end

#locked?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'lib/delayed/backend/base.rb', line 337

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

#nameObject



276
277
278
279
280
281
282
283
284
285
# File 'lib/delayed/backend/base.rb', line 276

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)


371
372
373
# File 'lib/delayed/backend/base.rb', line 371

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

#payload_objectObject



272
273
274
# File 'lib/delayed/backend/base.rb', line 272

def payload_object
  @payload_object ||= deserialize(self["handler"])
end

#payload_object=(object) ⇒ Object



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

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



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/delayed/backend/base.rb', line 253

def permanent_failure(error)
  begin
    # notify the payload_object of a permanent failure
    invoke_payload_object_cb(:on_permanent_failure, error)
  rescue
    # don't allow a failed deserialization to prevent destroying the job
  end

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

  if destroy_self
    destroy
  else
    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.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/delayed/backend/base.rb', line 232

def reschedule(error = nil, time = nil)
  begin
    return_code = invoke_payload_object_cb(:on_failure, error)
  rescue
    # don't allow a failed deserialization to prevent rescheduling
  end

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

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

#reschedule_atObject



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/delayed/backend/base.rb', line 341

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



362
363
364
365
366
367
368
369
# File 'lib/delayed/backend/base.rb', line 362

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

#unlockObject

Unlock this job (note: not saved to DB)



332
333
334
335
# File 'lib/delayed/backend/base.rb', line 332

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