Module: Delayed::Backend::Base

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ParseObjectFromYaml =

rubocop:disable ConstantName

%r{\!ruby/\w+\:([^\s]+)}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorObject

Returns the value of attribute error


50
51
52
# File 'lib/delayed/backend/base.rb', line 50

def error
  @error
end

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

#destroy_failed_jobs?Boolean

Returns:

  • (Boolean)

132
133
134
135
136
# File 'lib/delayed/backend/base.rb', line 132

def destroy_failed_jobs?
  payload_object.respond_to?(:destroy_failed_jobs?) ? payload_object.destroy_failed_jobs? : Delayed::Worker.destroy_failed_jobs
rescue DeserializationError
  Delayed::Worker.destroy_failed_jobs
end

#fail!Object


138
139
140
# File 'lib/delayed/backend/base.rb', line 138

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

#failed?Boolean Also known as: failed

Returns:

  • (Boolean)

56
57
58
# File 'lib/delayed/backend/base.rb', line 56

def failed?
  !!failed_at
end

#hook(name, *args) ⇒ Object


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

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


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/delayed/backend/base.rb', line 80

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


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

#max_run_timeObject


121
122
123
124
125
126
127
128
129
130
# File 'lib/delayed/backend/base.rb', line 121

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


63
64
65
66
67
# File 'lib/delayed/backend/base.rb', line 63

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


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

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


69
70
71
72
# File 'lib/delayed/backend/base.rb', line 69

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

#reschedule_atObject


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

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)


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

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