Update dispatcher to use HttpMulti job
This commit is contained in:
parent
69a98a2f5a
commit
faf026dec6
5 changed files with 28 additions and 48 deletions
|
|
@ -1,16 +1,15 @@
|
|||
module Jobs
|
||||
class HttpMulti
|
||||
module Job
|
||||
class HttpMulti < Base
|
||||
@queue = :http
|
||||
|
||||
MAX_RETRIES = 3
|
||||
OPTS = {:max_redirects => 3, :timeout => 5000, :method => :post}
|
||||
|
||||
def self.perform(user_id, object_type, object_id, person_ids, retry_count=0)
|
||||
def self.perform_delegate(user_id, enc_object_xml, person_ids, retry_count=0)
|
||||
user = User.find(user_id)
|
||||
people = Person.all(:id.in => person_ids)
|
||||
people = Person.where(:id => person_ids)
|
||||
|
||||
object = object_type.constantize.find(object_id)
|
||||
salmon = user.salmon(object)
|
||||
salmon = Salmon::SalmonSlap.create(user, Base64.decode64(enc_object_xml))
|
||||
|
||||
failed_request_people = []
|
||||
|
||||
|
|
@ -33,7 +32,7 @@ module Jobs
|
|||
hydra.run
|
||||
|
||||
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 )
|
||||
Resque.enqueue(Job::HttpMulti, user_id, enc_object_xml, failed_request_people, retry_count+=1 )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -79,16 +79,11 @@ class User < ActiveRecord::Base
|
|||
if add_contact_to_aspect(contact, to_aspect)
|
||||
membership = contact ? contact.aspect_memberships.where(:aspect_id => from_aspect.id).first : nil
|
||||
return ( membership && membership.destroy )
|
||||
else
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def salmon(post)
|
||||
created_salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
|
||||
created_salmon
|
||||
end
|
||||
|
||||
def add_contact_to_aspect(contact, aspect)
|
||||
return true if contact.aspect_memberships.where(:aspect_id => aspect.id).count > 0
|
||||
contact.aspect_memberships.create!(:aspect => aspect)
|
||||
|
|
@ -136,7 +131,6 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
def salmon(post)
|
||||
created_salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
|
||||
created_salmon
|
||||
|
|
|
|||
|
|
@ -40,11 +40,7 @@ class Postzord::Dispatch
|
|||
protected
|
||||
|
||||
def deliver_to_remote(people)
|
||||
people.each do |person|
|
||||
enc_xml = salmon.xml_for(person)
|
||||
Rails.logger.info("event=deliver_to_remote route=remote sender=#{@sender.person.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{@object.class}")
|
||||
Resque.enqueue(Job::HttpPost, person.receive_url, enc_xml)
|
||||
end
|
||||
Resque.enqueue(Job::HttpMulti, @sender.id, Base64.encode64(@object.to_diaspora_xml), people.map{|p| p.id})
|
||||
end
|
||||
|
||||
def deliver_to_local(people)
|
||||
|
|
|
|||
|
|
@ -212,17 +212,24 @@ describe Postzord::Dispatch do
|
|||
@remote_people = []
|
||||
@remote_people << @user.person
|
||||
@mailman = Postzord::Dispatch.new(@user, @sm)
|
||||
@hydra = mock()
|
||||
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||
end
|
||||
|
||||
it 'should queue an HttpPost job for each remote person' do
|
||||
Resque.should_receive(:enqueue).with(Job::HttpPost, @user.person.receive_url, anything).once
|
||||
Resque.should_receive(:enqueue).with(Job::HttpMulti, @user.id, anything, @remote_people.map{|p| p.id}).once
|
||||
@mailman.send(:deliver_to_remote, @remote_people)
|
||||
end
|
||||
|
||||
it 'calls salmon_for each remote person' do
|
||||
salmon = @mailman.salmon
|
||||
salmon.should_receive(:xml_for).with(@user.person)
|
||||
@mailman.send(:deliver_to_remote, @remote_people)
|
||||
Salmon::SalmonSlap.stub(:create).and_return(salmon)
|
||||
salmon.should_receive(:xml_for).with(@user.person).times
|
||||
@hydra.stub!(:queue)
|
||||
@hydra.stub!(:run)
|
||||
fantasy_resque do
|
||||
@mailman.send(:deliver_to_remote, @remote_people)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,11 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Jobs::HttpMulti do
|
||||
|
||||
let!(:user){ make_user }
|
||||
let!(:aspect){ user.aspects.create(:name => "awesome")}
|
||||
describe Job::HttpMulti do
|
||||
|
||||
before do
|
||||
@people = [Factory(:person), Factory(:person)]
|
||||
@post = user.build_post(:status_message, :message => "hey", :to => [aspect])
|
||||
@post.save
|
||||
@post_xml = Base64.encode64("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH")
|
||||
|
||||
@post_type = @post.class.to_s
|
||||
|
||||
@hydra = Typhoeus::Hydra.new
|
||||
@response = Typhoeus::Response.new(:code => 200, :headers => "", :body => "", :time => 0.2)
|
||||
|
|
@ -27,7 +22,7 @@ describe Jobs::HttpMulti do
|
|||
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||
|
||||
people_ids = @people.map{ |p| p.id }
|
||||
Jobs::HttpMulti.perform(user.id, @post_type, @post.id, people_ids)
|
||||
Job::HttpMulti.perform(bob.id, @post_xml, people_ids)
|
||||
end
|
||||
|
||||
it 'retries' do
|
||||
|
|
@ -37,8 +32,8 @@ describe Jobs::HttpMulti do
|
|||
|
||||
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||
|
||||
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])
|
||||
Resque.should_receive(:enqueue).with(Job::HttpMulti, bob.id, @post_xml, [person.id], 1).once
|
||||
Job::HttpMulti.perform(bob.id, @post_xml, [person.id])
|
||||
end
|
||||
|
||||
it 'max retries' do
|
||||
|
|
@ -49,18 +44,7 @@ describe Jobs::HttpMulti do
|
|||
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||
|
||||
Resque.should_not_receive(:enqueue)
|
||||
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])
|
||||
Job::HttpMulti.perform(bob.id, @post_xml, [person.id], 3)
|
||||
end
|
||||
|
||||
it 'generates encrypted xml for people' do
|
||||
|
|
@ -70,10 +54,10 @@ describe Jobs::HttpMulti do
|
|||
|
||||
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))
|
||||
salmon = Salmon::SalmonSlap.create(bob, Base64.decode64(@post_xml))
|
||||
Salmon::SalmonSlap.stub(:create).and_return(salmon)
|
||||
salmon.should_receive(:xml_for).and_return("encrypted things")
|
||||
|
||||
Jobs::HttpMulti.perform(user.id, @post_type, @post.id, [person.id])
|
||||
Job::HttpMulti.perform(bob.id, @post_xml, [person.id])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue