Skip to content

Instantly share code, notes, and snippets.

@quirkey
Created July 21, 2010 16:04
Show Gist options
  • Save quirkey/484682 to your computer and use it in GitHub Desktop.
Save quirkey/484682 to your computer and use it in GitHub Desktop.
# Resque::Reattempt
#
# The simplest auto-retry on failure (ala DJ) for Resque
#
# Just extend your job with the module and then instead of defining perform,
# define `perform_with_reattempt`:
#
# class MyReattemptJob
# extend Resque::Reattempt
#
# @queue = :myqueue
# @max_attempts = 15
#
# def self.perform_with_reattempt(myid)
# Myclass.dosomething(myid)
# end
#
# end
#
# If the job fails (throws an error) the error will be reraised to be handled by Resque
# failure then sent re-enqueued unless it has already been retried `@max_attempts` times.
module Resque
module Reattempt
def perform(*args)
last = args.last
if last && last.is_a?(Hash) && (attempt = last['attempt'])
args.pop
end
attempt ||= 0
begin
perform_with_reattempt(*args)
rescue => e
if attempt < (@max_attempts || 5)
args << {'attempt' => attempt + 1}
args.unshift(self)
Resque.enqueue(*args)
end
raise e
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment