add files
This commit is contained in:
parent
8f6922bcf9
commit
c9dd2d5b50
2 changed files with 77 additions and 0 deletions
30
app/models/jobs/http_multi.rb
Normal file
30
app/models/jobs/http_multi.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
module Jobs
|
||||||
|
class HttpMulti
|
||||||
|
@queue = :http
|
||||||
|
|
||||||
|
MAX_RETRIES = 3
|
||||||
|
OPTS = {:max_redirects => 3, :timeout => 5000, :method => :post}
|
||||||
|
|
||||||
|
def self.perform(urls, xml, retry_count=0)
|
||||||
|
failed_requests = []
|
||||||
|
|
||||||
|
hydra = Typhoeus::Hydra.new
|
||||||
|
urls.each do |url|
|
||||||
|
request = Typhoeus::Request.new(url, OPTS.merge(:xml => xml))
|
||||||
|
|
||||||
|
request.on_complete do |response|
|
||||||
|
unless response.success?
|
||||||
|
failed_requests << url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
hydra.queue request
|
||||||
|
end
|
||||||
|
hydra.run
|
||||||
|
|
||||||
|
unless failed_requests.empty? || retry_count >= MAX_RETRIES
|
||||||
|
Resque.enqueue(Jobs::HttpMulti, failed_requests, xml, retry_count+=1 )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
47
spec/models/jobs/http_multi_spec.rb
Normal file
47
spec/models/jobs/http_multi_spec.rb
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Jobs::HttpMulti do
|
||||||
|
before do
|
||||||
|
@urls = ['example.org/things/on/fire', 'http://google.com/']
|
||||||
|
@body = '<xml>California</xml>'
|
||||||
|
|
||||||
|
@hydra = Typhoeus::Hydra.new
|
||||||
|
@response = Typhoeus::Response.new(:code => 200, :headers => "", :body => "", :time => 0.2)
|
||||||
|
@failed_response = Typhoeus::Response.new(:code => 504, :headers => "", :body => "", :time => 0.2)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'POSTs to more than one URL' do
|
||||||
|
@urls.each do |url|
|
||||||
|
@hydra.stub(:post, url).and_return(@response)
|
||||||
|
end
|
||||||
|
|
||||||
|
@hydra.should_receive(:queue).twice
|
||||||
|
@hydra.should_receive(:run).once
|
||||||
|
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||||
|
|
||||||
|
Jobs::HttpMulti.perform(@urls, @body)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'retries' do
|
||||||
|
url = @urls[0]
|
||||||
|
|
||||||
|
@hydra.stub(:post, url).and_return(@failed_response)
|
||||||
|
|
||||||
|
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||||
|
|
||||||
|
Resque.should_receive(:enqueue).with(Jobs::HttpMulti, [url], @body, 1).once
|
||||||
|
Jobs::HttpMulti.perform([url], @body)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'max retries' do
|
||||||
|
url = @urls[0]
|
||||||
|
|
||||||
|
@hydra.stub(:post, url).and_return(@failed_response)
|
||||||
|
|
||||||
|
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||||
|
|
||||||
|
Resque.should_not_receive(:enqueue)
|
||||||
|
Jobs::HttpMulti.perform([url], @body, 3)
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue