Created
May 9, 2014 19:46
-
-
Save softr8/2bd96fc296505616adff to your computer and use it in GitHub Desktop.
Auto retry 3 times a resque job without any dependency
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
#it retries only if Timeout is present | |
module HookTimeoutRetry | |
def on_failure_retry(e, *args) | |
key = "AutoRetry:#{self.name}:#{Digest::MD5.hexdigest(Resque.encode(args))}" | |
counter = Resque.redis.incr(key) | |
Resque.redis.expire(key, 180) | |
if [Timeout::Error, Redis::TimeoutError].include?(e.class) && counter <= 3 | |
Rails.logger.info "Performing #{self} caused an exception (#{e}). Retrying..." | |
Resque.enqueue self, *args | |
else | |
Resque.redis.del(key) | |
end | |
end | |
end | |
class MyWorker | |
extend HookTimoeutRetry | |
def self.perform | |
#external calls | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment