lib/rocket_job/plugins/retry.rb in rocketjob-3.3.1 vs lib/rocket_job/plugins/retry.rb in rocketjob-3.3.2

- old
+ new

@@ -23,47 +23,51 @@ # # class MyJob < RocketJob::Job # include RocketJob::Plugins::Retry # # # Set the default retry_count - # self.max_retries = 3 + # self.retry_limit = 3 # # def perform # puts "DONE" # end # end # # # Queue the job for processing using the default cron_schedule specified above # MyJob.create! # # # Replace the default retry_count - # MyCronJob.create!(max_retries: 10) + # MyCronJob.create!(retry_limit: 10) # # # Disable retries for this job instance - # MyCronJob.create!(max_retries: 0) + # MyCronJob.create!(retry_limit: 0) # module Retry extend ActiveSupport::Concern included do after_fail :rocket_job_retry # Maximum number of times to retry this job # 25 is approximately 3 weeks of retries - field :max_retries, type: Integer, default: 25, class_attribute: true, user_editable: true, copy_on_restart: true + field :retry_limit, type: Integer, default: 25, class_attribute: true, user_editable: true, copy_on_restart: true # List of times when this job failed - field :failed_times, type: Array, default: [] + field :failed_at_list, type: Array, default: [] - validates_presence_of :max_retries + validates_presence_of :retry_limit end - # Returns [true|false] whether this job will be automatically retried on failure + # Returns [true|false] whether this job should be retried on failure. def rocket_job_retry_on_fail? - failure_count > max_retries + rocket_job_failure_count < retry_limit end + def rocket_job_failure_count + failed_at_list.size + end + private def rocket_job_retry # Failure count is incremented during before_fail return if expired? || !rocket_job_retry_on_fail? @@ -71,24 +75,24 @@ 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 - self.failed_times << now - new_record? ? retry : retry! + self.failed_at_list << now + new_record? ? self.retry : self.retry! end # Prevent exception from being cleared on retry def rocket_job_clear_exception self.completed_at = nil self.exception = nil unless rocket_job_retry_on_fail? self.worker_name = nil end # Returns [Time] when to retry this job at - # Same basic formula as Sidekiq and Delayed Job + # Same basic formula as Delayed Job def rocket_job_retry_seconds_to_delay - (failure_count ** 4) + 15 + (rand(30)*(failure_count+1)) + (rocket_job_failure_count ** 4) + 15 + (rand(30) * (rocket_job_failure_count + 1)) end end end end