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.



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

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)


129
130
131
132
133
# File 'lib/delayed/backend/base.rb', line 129

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



135
136
137
138
# File 'lib/delayed/backend/base.rb', line 135

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



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

def hook(name, *args)
  if payload_object.respond_to?(name)
    method = payload_object.method(name)
    method.arity.zero? ? method.call : method.call(self, *args)
  end
rescue DeserializationError # rubocop:disable HandleExceptions
end

#invoke_jobObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/delayed/backend/base.rb', line 77

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



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

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

#max_run_timeObject



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

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



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

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



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

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



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

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

#reschedule_atObject



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

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)



93
94
95
96
# File 'lib/delayed/backend/base.rb', line 93

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