Module: Delayed::Backend::Base

Defined in:
lib/delayed/backend/base.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ParseObjectFromYaml =

rubocop:disable ConstantName

/\!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



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

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

#failed?Boolean Also known as: failed

Returns:

  • (Boolean)


67
68
69
# File 'lib/delayed/backend/base.rb', line 67

def failed?
  !!failed_at
end

#hook(name, *args) ⇒ Object



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

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 # rubocop:disable HandleExceptions
end

#invoke_jobObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/delayed/backend/base.rb', line 91

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

#max_attemptsObject



128
129
130
# File 'lib/delayed/backend/base.rb', line 128

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

#max_run_timeObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/delayed/backend/base.rb', line 132

def max_run_time
  return unless payload_object.respond_to?(:max_run_time)
  return unless (run_time = payload_object.max_run_time)

  if run_time > Delayed::Worker.max_run_time
    Delayed::Worker.max_run_time
  else
    run_time
  end
end

#nameObject



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

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



85
86
87
88
89
# File 'lib/delayed/backend/base.rb', line 85

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

#payload_object=(object) ⇒ Object



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

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

#reschedule_atObject



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

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

#unlockObject

Unlock this job (note: not saved to DB)



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

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