Class: Belated::JobWrapper

Inherits:
Object
  • Object
show all
Includes:
Logging, Comparable
Defined in:
lib/belated/job_wrapper.rb

Overview

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

  • logging

  • error handling

  • job execution

  • job result handling

  • job result logging

  • job retries

  • job retry delay

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #logger, #logger=, #warn

Constructor Details

#initialize(job:, max_retries: 5, at: nil, active_job: false) ⇒ JobWrapper

Returns a new instance of JobWrapper.



19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#active_jobObject

Returns the value of attribute active_job.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def active_job
  @active_job
end

#atObject

Returns the value of attribute at.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def at
  @at
end

#completedObject

Returns the value of attribute completed.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def completed
  @completed
end

#errorObject

Returns the value of attribute error.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def error
  @error
end

#idObject

Returns the value of attribute id.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def id
  @id
end

#jobObject

Returns the value of attribute job.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def job
  @job
end

#max_retriesObject

Returns the value of attribute max_retries.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def max_retries
  @max_retries
end

#proc_klassObject

Returns the value of attribute proc_klass.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def proc_klass
  @proc_klass
end

#retriesObject

Returns the value of attribute retries.



16
17
18
# File 'lib/belated/job_wrapper.rb', line 16

def retries
  @retries
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  at <=> (other&.at || other&.scheduled_at)
end

#executeObject

rubocop:enable Lint/RescueException



51
52
53
54
55
56
57
58
59
# 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

#performObject

rubocop:disable Lint/RescueException



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# 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