class Belated::JobWrapper

JobWrapper is a wrapper for a job. It is responsible for

Attributes

active_job[RW]
at[RW]
completed[RW]
error[RW]
id[RW]
job[RW]
max_retries[RW]
proc_klass[RW]
retries[RW]

Public Class Methods

new(job:, max_retries: 5, at: nil, active_job: false) click to toggle source
# File lib/belated/job_wrapper.rb, line 19
def initialize(job:, max_retries: 5, at: nil, active_job: false)
  raise 'JobError' unless job.respond_to?(:call) || job.respond_to?(:perform)

  self.retries = 0
  self.max_retries = max_retries
  self.id = job.respond_to?(:job_id) ? job.job_id : SecureRandom.uuid
  self.job = job
  self.at = at
  self.proc_klass = job.instance_of?(Proc)
  self.active_job = active_job
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/belated/job_wrapper.rb, line 31
def <=>(other)
  at <=> (other&.at || other&.scheduled_at)
end
execute() click to toggle source

rubocop:enable Lint/RescueException

# File lib/belated/job_wrapper.rb, line 51
def execute
  if job.respond_to?(:call)
    job.call
  elsif job.respond_to?(:arguments)
    job.perform(*job.arguments)
  else
    job.perform
  end
end
perform() click to toggle source

rubocop:disable Lint/RescueException

# File lib/belated/job_wrapper.rb, line 36
def perform
  resp = execute
  self.completed = true
  resp
rescue Exception => e
  case e.class
  when Interrupt, SignalException, NoMethodError
    raise e
  else
    retry_job(e)
    "Error while executing job #{job.inspect}, #{e.inspect}. Retry #{retries} of #{max_retries}"
  end
end
retry_job(error) click to toggle source
# File lib/belated/job_wrapper.rb, line 61
def retry_job(error)
  self.retries += 1
  if retries > max_retries
    self.error = error
    return
  end

  seconds_to_retry = $TESTING ? 0.05 : retries.next**4
  self.at = (Time.now + seconds_to_retry).to_f
  log "Job #{id} failed, retrying at #{at}"
  Belated.job_list.push(self)
end