typhoeus should now be in. there is a new dependancy on curl and the curl dev headers, so install

This commit is contained in:
maxwell 2010-12-06 17:40:44 -08:00 committed by Raphael Sofaer
parent c9dd2d5b50
commit 638311125f
4 changed files with 72 additions and 20 deletions

View file

@ -5,16 +5,26 @@ module Jobs
MAX_RETRIES = 3
OPTS = {:max_redirects => 3, :timeout => 5000, :method => :post}
def self.perform(urls, xml, retry_count=0)
failed_requests = []
def self.perform(user_id, object_type, object_id, person_ids, retry_count=0)
user = User.find(user_id)
people = Person.all(:id.in => person_ids)
object = object_type.constantize.find(object_id)
salmon = user.salmon(object)
failed_request_people = []
hydra = Typhoeus::Hydra.new
urls.each do |url|
people.each do |person|
url = person.receive_url
xml = salmon.xml_for(person)
request = Typhoeus::Request.new(url, OPTS.merge(:xml => xml))
request.on_complete do |response|
unless response.success?
failed_requests << url
failed_request_people << person.id
end
end
@ -22,8 +32,8 @@ module Jobs
end
hydra.run
unless failed_requests.empty? || retry_count >= MAX_RETRIES
Resque.enqueue(Jobs::HttpMulti, failed_requests, xml, retry_count+=1 )
unless failed_request_people.empty? || retry_count >= MAX_RETRIES
Resque.enqueue(Jobs::HttpMulti, user_id, object_type, object_id, failed_request_people, retry_count+=1 )
end
end
end

View file

@ -136,6 +136,12 @@ class User < ActiveRecord::Base
end
end
def salmon(post)
created_salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
created_salmon
end
######## Commenting ########
def build_comment(text, options = {})
comment = Comment.new(:person_id => self.person.id,

View file

@ -18,6 +18,10 @@ execute "rvm deps" do
command "yum install -y bzip2"
end
execute "curl deps for Typheous" do
command "yum install -y curl.x86_64 curl-devel.x86_64"
end
def harden_ruby(ruby_string)
Dir.glob("/usr/local/rvm/wrappers/#{ruby_string}/*").each do |file|
link "/usr/local/bin/#{file.split('/').last}" do

View file

@ -1,47 +1,79 @@
require 'spec_helper'
describe Jobs::HttpMulti do
let!(:user){ make_user }
let!(:aspect){ user.aspects.create(:name => "awesome")}
before do
@urls = ['example.org/things/on/fire', 'http://google.com/']
@body = '<xml>California</xml>'
@people = [Factory(:person), Factory(:person)]
@post = user.build_post(:status_message, :message => "hey", :to => [aspect])
@post.save
@post_type = @post.class.to_s
@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)
it 'POSTs to more than one person' do
@people.each do |person|
@hydra.stub(:post, person.receive_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)
people_ids = @people.map{ |p| p.id }
Jobs::HttpMulti.perform(user.id, @post_type, @post.id, people_ids)
end
it 'retries' do
url = @urls[0]
person = @people[0]
@hydra.stub(:post, url).and_return(@failed_response)
@hydra.stub(:post, person.receive_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)
Resque.should_receive(:enqueue).with(Jobs::HttpMulti, user.id, @post_type, @post.id, [person.id], 1).once
Jobs::HttpMulti.perform(user.id, @post_type, @post.id, [person.id])
end
it 'max retries' do
url = @urls[0]
person = @people[0]
@hydra.stub(:post, url).and_return(@failed_response)
@hydra.stub(:post, person.receive_url).and_return(@failed_response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
Resque.should_not_receive(:enqueue)
Jobs::HttpMulti.perform([url], @body, 3)
Jobs::HttpMulti.perform(user.id, @post_type, @post.id, [person.id], 3)
end
it 'generates salmon from user' do
person = @people[0]
@hydra.stub(:post, person.receive_url).and_return(@response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
user.should_receive(:salmon).with(@post).and_return(user.salmon(@post))
Jobs::HttpMulti.perform(user.id, @post_type, @post.id, [person.id])
end
it 'generates encrypted xml for people' do
person = @people[0]
@hydra.stub(:post, person.receive_url).and_return(@response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
salmon = user.salmon(@post)
user.stub(:salmon).and_return(salmon)
salmon.should_receive(:xml_for).and_return(salmon.xml_for(@post))
Jobs::HttpMulti.perform(user.id, @post_type, @post.id, [person.id])
end
end