Skip to content

Instantly share code, notes, and snippets.

@necojackarc
Last active June 30, 2021 13:20
Show Gist options
  • Save necojackarc/2eb388bfae26c3228c9affe592a0def6 to your computer and use it in GitHub Desktop.
Save necojackarc/2eb388bfae26c3228c9affe592a0def6 to your computer and use it in GitHub Desktop.
To enable ActiveJob to control retry
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