Skip to content

Instantly share code, notes, and snippets.

@kunalmodi
Created June 16, 2012 00:03
Show Gist options
  • Save kunalmodi/2939288 to your computer and use it in GitHub Desktop.
Save kunalmodi/2939288 to your computer and use it in GitHub Desktop.
Retry Typhoeus
module RetryableTyphoeus
require 'typhoeus'
include Typhoeus
DEFAULT_RETRIES = 1
class Request < Typhoeus::Request
def original_on_complete=(proc)
@original_on_complete = proc
end
def original_on_complete
@original_on_complete
end
def get_on_complete
@on_complete
end
def retries=(retries)
@retries = retries
end
def retries
@retries ||= 0
end
end
class Hydra
def initialize(params = {})
@hydra = Typhoeus::Hydra.new(params)
end
def hydra
@hydra
end
def queue_with_retry(request, opts = {})
request.retries = (opts[:retries] || DEFAULT_RETRIES).to_i
request.original_on_complete ||= request.get_on_complete
request.on_complete do |response|
if response.success? || response.request.retries <= 0
request.original_on_complete.call(response)
else
response.request.retries -= 1
queue response.request
end
end
queue request
end
def queue(request)
hydra.queue request
end
def run
hydra.run
end
end
end
# Usage:
# hydra = RetryableTyphoeus::Hydra.new
# request = RetryableTyphoeus::Request.new(...)
# request.on_complete{|resp| puts resp}
# hydra.queue_with_retry request
# hydra.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment