Sha256: 89da31a4175a7361dd7b6eb3e09c0bf615801bf2c7e97cd24270da7685142de5

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

require 'securerandom'
require_relative 'logging'

class Belated
  # 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
  class JobWrapper
    include Comparable
    include Logging
    attr_accessor :retries, :max_retries, :id, :job, :at, :completed, :proc_klass

    def initialize(job:, max_retries: 5, at: nil)
      self.retries = 0
      self.max_retries = max_retries
      self.id = SecureRandom.uuid
      self.job = job
      self.at = at
      self.completed = false
      self.proc_klass = job.instance_of?(Proc)
    end

    def <=>(other)
      at <=> other.at
    end

    # rubocop:disable Lint/RescueException
    def perform
      resp = if job.respond_to?(:call)
               job.call
             else
               job.perform
             end
      self.completed = true
      resp
    rescue Exception => e
      case e.class
      when Interrupt, SignalException
        raise e
      else
        retry_job
        "Error while executing job, #{e.inspect}. Retry #{retries} of #{max_retries}"
      end
    end
    # rubocop:enable Lint/RescueException

    def retry_job
      self.retries += 1
      return if retries > max_retries

      self.at = Time.now + (retries.next**4)
      log "Job #{id} failed, retrying at #{at}"
      Belated.job_list.push(self)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
belated-0.6.7 lib/belated/job_wrapper.rb
belated-0.6.6 lib/belated/job_wrapper.rb
belated-0.6.5 lib/belated/job_wrapper.rb