Forked from necojackarc/active_job_retry_controlable.rb
Last active
February 5, 2020 09:46
-
-
Save edbond/6520ff254dc00c24f5e8a295b121f29f to your computer and use it in GitHub Desktop.
To enable ActiveJob to control retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveJobRetryControlable | |
extend ActiveSupport::Concern | |
DEFAULT_RETRY_LIMIT = 5 | |
class_methods do | |
def retry_limit(retry_limit) | |
@retry_limit = retry_limit | |
end | |
def load_retry_limit | |
@retry_limit || DEFAULT_RETRY_LIMIT | |
end | |
end | |
included do | |
attr_reader :attempt_number | |
def attempt_number | |
@attempt_number || 0 | |
end | |
def serialize | |
super.merge("attempt_number" => attempt_number + 1) | |
end | |
def deserialize(job_data) | |
super | |
@attempt_number = (job_data["attempt_number"] || 1) | |
end | |
private | |
def retry_limit | |
self.class.load_retry_limit | |
end | |
def retry_limit_exceeded? | |
self.class.load_retry_limit | |
attempt_number > retry_limit | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment