Module: Delayed::Backend::Base

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

#fail!Object



168
169
170
# File 'lib/delayed/backend/base.rb', line 168

def fail!
  update_attributes(:failed_at => self.class.db_time_now)
end

#failed?Boolean Also known as: failed

Returns:

  • (Boolean)


100
101
102
# File 'lib/delayed/backend/base.rb', line 100

def failed?
  !!failed_at
end

#hook(name, *args) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/delayed/backend/base.rb', line 149

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/delayed/backend/base.rb', line 127

def invoke_job
  Delayed::Worker.lifecycle.run_callbacks(:invoke_job, self) do
    begin
      hook :before
      payload_object.perform
      hook :success
    rescue Exception => e
      hook :error, e
      raise e
    ensure
      hook :after
    end
  end
end

#max_attemptsObject



164
165
166
# File 'lib/delayed/backend/base.rb', line 164

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

#nameObject



107
108
109
110
111
112
113
# File 'lib/delayed/backend/base.rb', line 107

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



120
121
122
123
124
125
# File 'lib/delayed/backend/base.rb', line 120

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



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

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

#reschedule_atObject



158
159
160
161
162
# File 'lib/delayed/backend/base.rb', line 158

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)



143
144
145
146
147
# File 'lib/delayed/backend/base.rb', line 143

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