Module: Delayed::Backend::Base

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ParseObjectFromYaml =

rubocop:disable Naming/ConstantName

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorObject

Returns the value of attribute error.



46
47
48
# File 'lib/delayed/backend/base.rb', line 46

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)


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

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



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

def fail!
  self.failed_at = self.class.db_time_now
  save!
end

#failed?Boolean Also known as: failed

Returns:

  • (Boolean)


53
54
55
# File 'lib/delayed/backend/base.rb', line 53

def failed?
  !!failed_at
end

#hook(name, *args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/delayed/backend/base.rb', line 106

def hook(name, *args)
  if payload_object.respond_to?(name)
    if payload_object.is_a?(Delayed::JobWrapper)
      return if name == :enqueue # this callback is not supported due to method naming conflicts.

      warn '[DEPRECATION] Job hook methods (`before`, `after`, `success`, etc) are deprecated. Use ActiveJob callbacks instead.'
    end

    method = payload_object.method(name)
    method.arity.zero? ? method.call : method.call(self, *args)
  end
rescue DeserializationError
end

#invoke_jobObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/delayed/backend/base.rb', line 87

def invoke_job
  Delayed::Worker.lifecycle.run_callbacks(:invoke_job, self) do
    hook :before
    payload_object.perform
    hook :success
  rescue Exception => e # rubocop:disable Lint/RescueException
    hook :error, e
    raise e
  ensure
    hook :after
  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

rubocop:disable Metrics/AbcSize



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

def name # rubocop:disable Metrics/AbcSize
  @name ||= payload_object.job_data['job_class'] if payload_object.respond_to?(:job_data)
  @name ||= payload_object.display_name if payload_object.respond_to?(:display_name)
  @name ||= payload_object.class.name
rescue DeserializationError
  ParseObjectFromYaml.match(handler)[1]
end

#payload_objectObject



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

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



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

def payload_object=(object)
  @payload_object = object
  self.handler = YAML.dump_dj(object)
end

#priorityObject



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

def priority
  Priority.new(super)
end

#priority=(value) ⇒ Object



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

def priority=(value)
  super(Priority.new(value))
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)



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

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