Skip to content

Instantly share code, notes, and snippets.

@gravis
Created February 9, 2010 20:24
Show Gist options
  • Save gravis/299623 to your computer and use it in GitHub Desktop.
Save gravis/299623 to your computer and use it in GitHub Desktop.
A resque job with retry on failure, dead simple. Needs resque-schedule plugin. the only caveat is that the first attempt is in the "events" queue, whereas retries are only visible in Delayed tab.
class GetEventResult
@queue = :events
def self.perform(event_id, attempt=0)
event = Event.find(event_id)
Rails.logger.info "Fetching results for event ##{event.id} (#{event.name})..."
begin
results = EventImporter.new(event.datetime.to_date).get_results(event)
rescue OpenURI::HTTPError
# let's reschedule the job if attempts < 30
if attempt < 30
Rails.logger.info "Results not ready fo event ##{event.id} (#{event.name}), re-scheduling (attempt ##{attempt}/30)..."
Resque.enqueue_at(Time.now+1.minute, self, event.id, attempt+1)
# You can also do exponential back off, thanks to bvandenbos :
# Resque.enqueue_at((1 * (attempt ** 1.7)).to_i.minutes.from_now, self, event.id, attempt+1)
else
raise "Max attempts reached"
end
else
event.process_results!(results)
Rails.logger.info "Results for event ##{event.id} (#{event.name}) : #{results}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment