-
-
Save freethinker/5433076 to your computer and use it in GitHub Desktop.
Retryable Typhoeus
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
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