From 638311125fb16ccb71e6369f0b125f9fe5174850 Mon Sep 17 00:00:00 2001 From: maxwell Date: Mon, 6 Dec 2010 17:40:44 -0800 Subject: [PATCH] typhoeus should now be in. there is a new dependancy on curl and the curl dev headers, so install --- app/models/jobs/http_multi.rb | 22 +++++--- app/models/user.rb | 6 +++ chef/cookbooks/centos/recipes/bootstrap.rb | 4 ++ spec/models/jobs/http_multi_spec.rb | 60 +++++++++++++++++----- 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/app/models/jobs/http_multi.rb b/app/models/jobs/http_multi.rb index bece25032..15dc3c171 100644 --- a/app/models/jobs/http_multi.rb +++ b/app/models/jobs/http_multi.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 32944adab..61d9356ea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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, diff --git a/chef/cookbooks/centos/recipes/bootstrap.rb b/chef/cookbooks/centos/recipes/bootstrap.rb index b5c860876..b992a8f01 100644 --- a/chef/cookbooks/centos/recipes/bootstrap.rb +++ b/chef/cookbooks/centos/recipes/bootstrap.rb @@ -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 diff --git a/spec/models/jobs/http_multi_spec.rb b/spec/models/jobs/http_multi_spec.rb index 69737ae42..ec3a43ba0 100644 --- a/spec/models/jobs/http_multi_spec.rb +++ b/spec/models/jobs/http_multi_spec.rb @@ -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 = 'California' + @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