Class: Delayed::JobWrapper

Inherits:
Object
  • Object
show all
Includes:
HookDeprecation
Defined in:
lib/delayed/job_wrapper.rb

Overview

rubocop:disable Betterment/ActiveJobPerformable

Defined Under Namespace

Modules: HookDeprecation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_or_data) ⇒ JobWrapper

Returns a new instance of JobWrapper.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/delayed/job_wrapper.rb', line 19

def initialize(job_or_data)
  # During enqueue the job instance is passed in directly, saves us deserializing
  # it to find out how to queue the job.
  # During load from the db, we get a data hash passed in so deserialize lazily.
  if job_or_data.is_a?(ActiveJob::Base)
    @job = job_or_data
    @job_data = job_or_data.serialize
  else
    @job_data = job_or_data
  end
end

Instance Attribute Details

#job_dataObject

Returns the value of attribute job_data.



15
16
17
# File 'lib/delayed/job_wrapper.rb', line 15

def job_data
  @job_data
end

Instance Method Details

#before(record) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/delayed/job_wrapper.rb', line 46

def before(record)
  # ActiveJob retries should use the row's current priority (it may have changed since enqueue):
  self.priority = record.priority.to_i if respond_to?(:priority=)
  # If a job is manually reset, we reset ActiveJob's execution log as well:
  reset_execution_log! if job_data.delete('terminated_at') && record.attempts.zero?
  super
end

#display_nameObject



31
32
33
# File 'lib/delayed/job_wrapper.rb', line 31

def display_name
  job_data['job_class']
end

#encode_with(coder) ⇒ Object



77
78
79
# File 'lib/delayed/job_wrapper.rb', line 77

def encode_with(coder)
  coder['job_data'] = @job_data
end

#error(record, error) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/delayed/job_wrapper.rb', line 54

def error(record, error)
  # The error escaped retry_on (if any), so ActiveJob considers the job terminated.
  job_data['terminated_at'] = record.class.db_time_now.utc.iso8601(9)
  record.payload_object = self # re-serialize the handler

  # If the error escaped ActiveJob's retry_on policy, we stop the Delayed::Job retries as well.
  @_aj_retry_terminated = rescue_handlers.any? { |name, _| error.is_a?(name.constantize) }

  super
end

#max_attemptsObject



65
66
67
68
69
# File 'lib/delayed/job_wrapper.rb', line 65

def max_attempts
  return 1 if @_aj_retry_terminated

  super if respond_to_missing?(:max_attempts)
end

#performObject



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

def perform
  ActiveJob::Callbacks.run_callbacks(:execute) do
    job.perform_now
  end
end

#respond_to?Boolean

If job failed to deserialize, we can't respond to delegated methods. Returning false here prevents instance method checks from blocking job cleanup. Rails 8.1+ raises ActiveJob::UnknownJobClassError (rails/rails#53770).

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/delayed/job_wrapper.rb', line 38

def respond_to?(*, **)
  super
rescue NameError => e
  raise if defined?(ActiveJob::UnknownJobClassError) && !e.is_a?(ActiveJob::UnknownJobClassError)

  false
end