Closed DavydenkovM closed 9 years ago
I think an easier way to achieve the random-delay behaviour here would be to write another BackoffStrategy
(which just needs to define #should_retry?(attempt, exception)
and #retry_delay(attempt, exception)
). If you do this I'd be happy to accept it as a PR to this gem, with a shortcut to use as random_retry
(as is done for the constant and variable strategies).
Many thanks for your response, I'll try to do my best
Disclaimer: I haven't even run this so may not work at all, but I think this would probably achieve what you want:
class ExponentialBackoffStrategy < ConstantBackoffStrategy
def retry_delay(attempt, _exception)
((attempt ** 4) + 15 + (rand(30) * (attempt + 1))).seconds
end
end
class MyJob < ActiveJob::Base
include ActiveJob::Retry
retry_with ExponentialBackoffStrategy.new(limit: max_retries, retryable_exceptions: ..., )
end
You'd probably also want a ExponentialOptionsValidator
to make sure people don't try to set the delays
key, but add some tests and should be golden. You'll probably also want a helper method like this one to be able to use the strategy as:
class MyJob < ActiveJob::Base
include ActiveJob::Retry
exponential_retry limit: 25
...
end
Excited for the PR ;) :dancer: :tada: :boom:
I have some classes, which could be useful for someone using this great gem!
For example, you would like your jobs to be not-retryable by default, but be retryable only if some
retry_options
provided.You could use the following modules/concerns to achieve this goal:
That's it. Using these concerns you can easily redefine behavior within
concrete job
using methodretry_options
with keys:max_retries
,:fatal_exceptions
,:retryable_exceptions
,:delays_formula
,:delays_array
(see concernCommonRetry
).But if you would like to use default behavior, you can just include concern
CommonJobOptions
withretry_options max_retries: 5
(seeDummyJob
)