Commit 005b165e authored by danielgrippi's avatar danielgrippi

defer dispatching to help app processes not bloat in memory

parent c23e6d9a
web: bundle exec unicorn -c config/unicorn.rb -p $PORT
redis: redis-server
catchall_worker: env QUEUE=* bundle exec rake resque:work
slow_worker: env QUEUES=socket_webfinger,photos,http_service,receive_local,mail,receive,receive_salmon,http,delete_account bundle exec rake resque:work
priority_worker: env QUEUES=socket_webfinger,photos,http_service,mail,delete_account bundle exec rake resque:work
slow_worker: env QUEUES=socket_webfinger,photos,http_service,dispatch,receive_local,mail,receive,receive_salmon,http,delete_account,* bundle exec rake resque:work
priority_worker: env QUEUES=socket_webfinger,photos,http_service,dispatch,mail,delete_account bundle exec rake resque:work
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module Jobs
class DeferredDispatch < Base
@queue = :dispatch
def self.perform(user_id, object_class_name, object_id, opts)
user = User.find(user_id)
object = object_class_name.constantize.find(object_id)
if opts[:additional_subscribers].present?
opts[:additional_subscribers] = Person.where(:id => opts[:additional_subscribers])
end
Postzord::Dispatcher.build(user, object, opts).post
end
end
end
......@@ -142,7 +142,11 @@ class StatusMessage < Post
XML
end
def after_dispatch sender
def after_dispatch(sender)
self.update_and_dispatch_attached_photos(sender)
end
def update_and_dispatch_attached_photos(sender)
unless self.photos.empty?
self.photos.update_all(:pending => false, :public => self.public)
for photo in self.photos
......
......@@ -228,7 +228,7 @@ class User < ActiveRecord::Base
end
######## Posting ########
def build_post(class_name, opts = {})
def build_post(class_name, opts={})
opts[:author] = self.person
opts[:diaspora_handle] = opts[:author].diaspora_handle
......@@ -236,15 +236,15 @@ class User < ActiveRecord::Base
model_class.diaspora_initialize(opts)
end
def dispatch_post(post, opts = {})
mailman = Postzord::Dispatcher.build(self, post, opts)
mailman.post
def dispatch_post(post, opts={})
Postzord::Dispatcher.defer_build_and_post(self, post, opts)
end
def update_post(post, post_hash = {})
def update_post(post, post_hash={})
if self.owns? post
puts 'ownin'
post.update_attributes(post_hash)
Postzord::Dispatcher.build(self, post).post
self.dispatch_post(post)
end
end
......
......@@ -35,6 +35,12 @@ class Postzord::Dispatcher
end
end
def self.defer_build_and_post(user, object, opts={})
opts[:additional_subscribers] ||= []
opts[:additional_subscribers] = [*opts[:additional_subscribers]].map(&:id)
Resque.enqueue(Jobs::DeferredDispatch, user.id, object.class.to_s, object.id, opts)
end
# @param object [Object]
# @return [Boolean]
def self.object_should_be_processed_as_public?(object)
......
......@@ -125,19 +125,24 @@ describe StatusMessagesController do
@hash = status_message_hash
@hash[:photos] = [@photo1.id.to_s, @photo2.id.to_s]
end
it "will post a photo without text" do
@hash.delete :text
post :create, @hash
response.should be_redirect
end
it "attaches all referenced photos" do
post :create, @hash
assigns[:status_message].photos.map(&:id).should =~ [@photo1, @photo2].map(&:id)
end
it "sets the pending bit of referenced photos" do
post :create, @hash
@photo1.reload.pending.should be_false
@photo2.reload.pending.should be_false
fantasy_resque do
post :create, @hash
@photo1.reload.pending.should be_false
@photo2.reload.pending.should be_false
end
end
end
end
......
......@@ -283,10 +283,10 @@ describe Postzord::Dispatcher do
it 'does not push to hub for non-public posts' do
@sm = Factory(:status_message)
mailman = Postzord::Dispatcher.build(alice, @sm)
mailman = Postzord::Dispatcher.build(alice, @sm, :url => "http://joindiaspora.com/p/123")
mailman.should_not_receive(:deliver_to_hub)
mailman.post(:url => "http://joindiaspora.com/p/123")
mailman.post
end
it 'only pushes to specified services' do
......@@ -294,20 +294,20 @@ describe Postzord::Dispatcher do
alice.services << @s1
@s2 = Factory(:service, :user_id => alice.id)
alice.services << @s2
mailman = Postzord::Dispatcher.build(alice, Factory(:status_message))
mailman = Postzord::Dispatcher.build(alice, Factory(:status_message), :url => "http://joindiaspora.com/p/123", :services => [@s1])
Resque.stub!(:enqueue).with(Jobs::PublishToHub, anything)
Resque.stub!(:enqueue).with(Jobs::HttpMulti, anything, anything, anything)
Resque.should_receive(:enqueue).with(Jobs::PostToService, @s1.id, anything, anything)
mailman.post(:url => "http://joindiaspora.com/p/123", :services => [@s1])
mailman.post
end
it 'does not push to services if none are specified' do
mailman = Postzord::Dispatcher.build(alice, Factory(:status_message))
mailman = Postzord::Dispatcher.build(alice, Factory(:status_message), :url => "http://joindiaspora.com/p/123")
Resque.stub!(:enqueue).with(Jobs::PublishToHub, anything)
Resque.should_not_receive(:enqueue).with(Jobs::PostToService, anything, anything, anything)
mailman.post(:url => "http://joindiaspora.com/p/123")
mailman.post
end
end
......
......@@ -580,11 +580,9 @@ describe User do
end
describe '#update_post' do
it 'sends a notification to aspects' do
m = mock()
m.should_receive(:post)
Postzord::Dispatcher.should_receive(:build).and_return(m)
it 'should dispatch post' do
photo = alice.build_post(:photo, :user_file => uploaded_photo, :text => "hello", :to => alice.aspects.first.id)
alice.should_receive(:dispatch_post).with(photo)
alice.update_post(photo, :text => 'hellp')
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment