Last active
June 30, 2021 13:20
-
-
Save necojackarc/2eb388bfae26c3228c9affe592a0def6 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 | |
attr_reader :attempt_number | |
module ClassMethods | |
def retry_limit(retry_limit) | |
@retry_limit = retry_limit | |
end | |
def load_retry_limit | |
@retry_limit || DEFAULT_RETRY_LIMIT | |
end | |
end | |
def serialize | |
super.merge("attempt_number" => (@attempt_number || 0) + 1) | |
end | |
def deserialize(job_data) | |
super | |
@attempt_number = job_data["attempt_number"] | |
end | |
private | |
def retry_limit | |
self.class.load_retry_limit | |
end | |
def retry_limit_exceeded? | |
@attempt_number > retry_limit | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment