Skip to content

Instantly share code, notes, and snippets.

@jtoy
Forked from igrigorik/em-http-retry.rb
Created August 26, 2011 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtoy/1174121 to your computer and use it in GitHub Desktop.
Save jtoy/1174121 to your computer and use it in GitHub Desktop.
require 'em-http'
EM.run do
def dispatch(opts)
opts
puts opts
h = EM::HttpRequest.new(opts[:uri]).get
h.callback { |rep| opts[:cb].call(rep) if opts[:cb] }
h.errback do |rep|
#how do i write this code in errback if I am passing my own errback handling code from the initial dispatch
opts[:retries] < 3 ? dispatch(opts.merge({:retries =>(opts[:retries] +1)})) : opts[:eb].call(rep)
end
end
cb = Proc.new{ |rep| [:success, rep.response]; EM.stop }
eb = Proc.new{ |rep| puts "original errback" ; EM.stop}
dispatch(:uri => 'http://badhos1212.com/',:cb => cb,:eb => eb,:retries => 0)
end
require 'em-http'
EM.run do
def dispatch(uri, retries = 0)
h = EM::HttpRequest.new(uri).get
h.callback { |rep| [:success, rep.response]; EM.stop }
h.errback do |rep|
p [:fail, rep]
retries < 3 ? dispatch(uri, retries+1) : EM.stop
end
end
dispatch('http://badhos1212.com/')
end
require 'em-http'
EM.run do
def dispatch(uri, retries = 0)
h = EM::HttpRequest.new(uri).get
h.callback { |rep| [:success, rep.response]; EM.stop }
h.errback do |rep|
p [:fail, rep]
retries < 3 ? dispatch(uri, retries+1) : EM.stop
end
end
dispatch('http://badhos1212.com/')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment