Skip to content

Instantly share code, notes, and snippets.

@softr8
Created May 9, 2014 19:46
Show Gist options
  • Save softr8/2bd96fc296505616adff to your computer and use it in GitHub Desktop.
Save softr8/2bd96fc296505616adff to your computer and use it in GitHub Desktop.
Auto retry 3 times a resque job without any dependency
#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