module RocketJob::Plugins::Retry

Automatically retry the job on failure.

Example:

class MyJob < RocketJob::Job

include RocketJob::Plugins::Retry

# Set the maximum number of times a job should be retried before giving up.
self.retry_limit = 3

def perform
  puts "DONE"
end

end

# Queue the job for processing using the default cron_schedule specified above MyJob.create!

# Override the default retry_limit for a specific job instance. MyCronJob.create!(retry_limit: 10)

# Disable retries for this job instance. MyCronJob.create!(retry_limit: 0)

Public Instance Methods

rocket_job_failure_count() click to toggle source
# File lib/rocket_job/plugins/retry.rb, line 50
def rocket_job_failure_count
  failed_at_list.size
end
rocket_job_retry_on_fail?() click to toggle source

Returns [true|false] whether this job should be retried on failure.

# File lib/rocket_job/plugins/retry.rb, line 46
def rocket_job_retry_on_fail?
  rocket_job_failure_count < retry_limit
end

Private Instance Methods

rocket_job_clear_exception() click to toggle source

Prevent exception from being cleared on retry

# File lib/rocket_job/plugins/retry.rb, line 70
def rocket_job_clear_exception
  self.completed_at = nil
  self.exception    = nil unless rocket_job_retry_on_fail?
  self.worker_name  = nil
end
rocket_job_retry() click to toggle source
# File lib/rocket_job/plugins/retry.rb, line 56
def rocket_job_retry
  # Failure count is incremented during before_fail
  return if expired? || !rocket_job_retry_on_fail?

  delay_seconds = rocket_job_retry_seconds_to_delay
  logger.info "Job failed, automatically retrying in #{delay_seconds} seconds. Retry count: #{failure_count}"

  now         = Time.now
  self.run_at = now + delay_seconds
  failed_at_list << now
  new_record? ? self.retry : retry!
end
rocket_job_retry_seconds_to_delay() click to toggle source

Returns [Time] when to retry this job at Same basic formula as Delayed Job

# File lib/rocket_job/plugins/retry.rb, line 78
def rocket_job_retry_seconds_to_delay
  (rocket_job_failure_count**4) + 15 + (rand(30) * (rocket_job_failure_count + 1))
end