Sha256: 24c31bee80512c613b27b7a275f15bf51b4ecd868843b2cc2e98217db1674e47

Contents?: true

Size: 1009 Bytes

Versions: 5

Compression:

Stored size: 1009 Bytes

Contents

require 'securerandom'
require_relative 'logging'

class Belated
  class JobWrapper
    include Logging
    attr_accessor :retries, :max_retries, :id, :job, :at

    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
    end

    # rubocop:disable Lint/RescueException
    def perform
      if job.respond_to?(:call)
        job.call
      else
        job.perform
      end
    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.utc + (retries.next**4)
      log "Job #{id} failed, retrying at #{at}"
      Belated.job_list.push(self)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
belated-0.5.4 lib/belated/job_wrapper.rb
belated-0.5.3 lib/belated/job_wrapper.rb
belated-0.5.2 lib/belated/job_wrapper.rb
belated-0.5.1 lib/belated/job_wrapper.rb
belated-0.5.0 lib/belated/job_wrapper.rb