Module: Delayed::Backend::Base

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ParseObjectFromYaml =
/\!ruby\/\w+\:([^\s]+)/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/delayed/backend/base.rb', line 4

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#failed?Boolean Also known as: failed

Returns:

  • (Boolean)


58
59
60
# File 'lib/delayed/backend/base.rb', line 58

def failed?
  failed_at
end

#hook(name, *args) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/delayed/backend/base.rb', line 102

def hook(name, *args)
  if payload_object.respond_to?(name)
    method = payload_object.method(name)
    method.arity == 0 ? method.call : method.call(self, *args)
  end
rescue DeserializationError
  # do nothing
end

#invoke_jobObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/delayed/backend/base.rb', line 85

def invoke_job
  hook :before
  payload_object.perform
  hook :success
rescue Exception => e
  hook :error, e
  raise e
ensure
  hook :after
end

#max_attemptsObject



117
118
119
# File 'lib/delayed/backend/base.rb', line 117

def max_attempts
  payload_object.max_attempts if payload_object.respond_to?(:max_attempts)
end

#nameObject



65
66
67
68
69
70
71
# File 'lib/delayed/backend/base.rb', line 65

def name
  @name ||= payload_object.respond_to?(:display_name) ?
              payload_object.display_name :
              payload_object.class.name
rescue DeserializationError
  ParseObjectFromYaml.match(handler)[1]
end

#payload_objectObject



78
79
80
81
82
83
# File 'lib/delayed/backend/base.rb', line 78

def payload_object
  @payload_object ||= YAML.load(self.handler)
rescue TypeError, LoadError, NameError, ArgumentError => e
  raise DeserializationError,
    "Job failed to load: #{e.message}. Handler: #{handler.inspect}"
end

#payload_object=(object) ⇒ Object



73
74
75
76
# File 'lib/delayed/backend/base.rb', line 73

def payload_object=(object)
  @payload_object = object
  self.handler = object.to_yaml
end

#reschedule_atObject



111
112
113
114
115
# File 'lib/delayed/backend/base.rb', line 111

def reschedule_at
  payload_object.respond_to?(:reschedule_at) ?
    payload_object.reschedule_at(self.class.db_time_now, attempts) :
    self.class.db_time_now + (attempts ** 4) + 5
end

#unlockObject

Unlock this job (note: not saved to DB)



97
98
99
100
# File 'lib/delayed/backend/base.rb', line 97

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