Class: Belated::JobWrapper
- Inherits:
-
Object
- Object
- Belated::JobWrapper
- 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
Direct Known Subclasses
Instance Attribute Summary collapse
-
#active_job ⇒ Object
Returns the value of attribute active_job.
-
#at ⇒ Object
Returns the value of attribute at.
-
#completed ⇒ Object
Returns the value of attribute completed.
-
#error ⇒ Object
Returns the value of attribute error.
-
#id ⇒ Object
Returns the value of attribute id.
-
#job ⇒ Object
Returns the value of attribute job.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#proc_klass ⇒ Object
Returns the value of attribute proc_klass.
-
#retries ⇒ Object
Returns the value of attribute retries.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#execute ⇒ Object
rubocop:enable Lint/RescueException.
-
#initialize(job:, max_retries: 5, at: nil, active_job: false) ⇒ JobWrapper
constructor
A new instance of JobWrapper.
-
#perform ⇒ Object
rubocop:disable Lint/RescueException.
- #retry_job(error) ⇒ Object
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_job ⇒ Object
Returns the value of attribute active_job.
16 17 18 |
# File 'lib/belated/job_wrapper.rb', line 16 def active_job @active_job end |
#at ⇒ Object
Returns the value of attribute at.
16 17 18 |
# File 'lib/belated/job_wrapper.rb', line 16 def at @at end |
#completed ⇒ Object
Returns the value of attribute completed.
16 17 18 |
# File 'lib/belated/job_wrapper.rb', line 16 def completed @completed end |
#error ⇒ Object
Returns the value of attribute error.
16 17 18 |
# File 'lib/belated/job_wrapper.rb', line 16 def error @error end |
#id ⇒ Object
Returns the value of attribute id.
16 17 18 |
# File 'lib/belated/job_wrapper.rb', line 16 def id @id end |
#job ⇒ Object
Returns the value of attribute job.
16 17 18 |
# File 'lib/belated/job_wrapper.rb', line 16 def job @job end |
#max_retries ⇒ Object
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_klass ⇒ Object
Returns the value of attribute proc_klass.
16 17 18 |
# File 'lib/belated/job_wrapper.rb', line 16 def proc_klass @proc_klass end |
#retries ⇒ Object
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 |
#execute ⇒ Object
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 |
#perform ⇒ Object
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 |