make jobs have a base class, which will verify the activerecord connection before trying the job

This commit is contained in:
maxwell 2011-01-19 14:10:42 -08:00
parent 934f56b553
commit e7a065a521
36 changed files with 111 additions and 110 deletions

View file

@ -23,7 +23,7 @@ class InvitationsController < Devise::InvitationsController
good_emails, bad_emails = emails.partition{|e| e.try(:match, Devise.email_regexp)}
good_emails.each{|e| Resque.enqueue(Jobs::InviteUser, current_user.id, e, aspect, message)}
good_emails.each{|e| Resque.enqueue(Job::InviteUser, current_user.id, e, aspect, message)}
if bad_emails.any?
flash[:error] = I18n.t('invitations.create.sent') + good_emails.join(', ') + " "+ I18n.t('invitations.create.rejected') + bad_emails.join(', ')

View file

@ -133,6 +133,6 @@ class PeopleController < ApplicationController
private
def webfinger(account, opts = {})
Resque.enqueue(Jobs::SocketWebfinger, current_user.id, account, opts)
Resque.enqueue(Job::SocketWebfinger, current_user.id, account, opts)
end
end

View file

@ -57,7 +57,7 @@ class PublicsController < ApplicationController
end
@user = person.owner
Resque.enqueue(Jobs::ReceiveSalmon, @user.id, CGI::unescape(params[:xml]))
Resque.enqueue(Job::ReceiveSalmon, @user.id, CGI::unescape(params[:xml]))
render :nothing => true, :status => 200
end

View file

@ -59,6 +59,4 @@ module AspectsHelper
end
(link_to str, '#', :id => 'expand_publisher').html_safe
end
end

13
app/models/jobs/base.rb Normal file
View file

@ -0,0 +1,13 @@
module Job
class Base
extend ResqueJobLogging
def self.perform(*args)
ActiveRecord::Base.verify_active_connections!
self.perform_delegate(*args)
end
def self.perform_delegate(*args) # override this
end
end
end

View file

@ -3,13 +3,12 @@
# the COPYRIGHT file.
module Jobs
class HttpPost
extend ResqueJobLogging
module Job
class HttpPost < Base
@queue = :http
NUM_TRIES = 3
def self.perform(url, body, tries_remaining = NUM_TRIES)
def self.perform_delegate(url, body, tries_remaining = NUM_TRIES)
begin
body = CGI::escape(body)
RestClient.post(url, :xml => body){ |response, request, result, &block|

View file

@ -3,11 +3,10 @@
# the COPYRIGHT file.
module Jobs
class InviteUser
extend ResqueJobLogging
module Job
class InviteUser < Base
@queue = :mail
def self.perform(sender_id, email, aspect_id, invite_message)
def self.perform_delegate(sender_id, email, aspect_id, invite_message)
user = User.find(sender_id)
user.invite_user(email, aspect_id, invite_message)
end

View file

@ -1,8 +1,7 @@
module Jobs
class MailAlsoCommented
extend ResqueJobLogging
module Job
class MailAlsoCommented < Base
@queue = :mail
def self.perform(recipient_id, sender_id, comment_id)
def self.perform_delegate(recipient_id, sender_id, comment_id)
Notifier.also_commented(recipient_id, sender_id, comment_id).deliver
end
end

View file

@ -1,8 +1,7 @@
module Jobs
class MailCommentOnPost
extend ResqueJobLogging
module Job
class MailCommentOnPost < Base
@queue = :mail
def self.perform(recipient_id, sender_id, comment_id)
def self.perform_delegate(recipient_id, sender_id, comment_id)
Notifier.comment_on_post(recipient_id, sender_id, comment_id).deliver
end
end

View file

@ -3,11 +3,10 @@
# the COPYRIGHT file.
module Jobs
class MailRequestAcceptance
extend ResqueJobLogging
module Job
class MailRequestAcceptance < Base
@queue = :mail
def self.perform(recipient_id, sender_id)
def self.perform_delegate(recipient_id, sender_id)
Notifier.request_accepted(recipient_id, sender_id).deliver
end
end

View file

@ -3,11 +3,10 @@
# the COPYRIGHT file.
module Jobs
class MailRequestReceived
extend ResqueJobLogging
module Job
class MailRequestReceived < Base
@queue = :mail
def self.perform(recipient_id, sender_id)
def self.perform_delegate(recipient_id, sender_id)
Notifier.new_request(recipient_id, sender_id).deliver
end
end

View file

@ -2,14 +2,13 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module Jobs
class NotifyLocalUsers
extend ResqueJobLogging
module Job
class NotifyLocalUsers < Base
@queue = :receive_local
require File.join(Rails.root, 'app/models/notification')
def self.perform(user_id, object_klass, object_id, person_id)
def self.perform_delegate(user_id, object_klass, object_id, person_id)
user = User.find_by_id(user_id)
object = object_klass.constantize.find_by_id(object_id)
person = Person.find_by_id(person_id)

View file

@ -2,12 +2,11 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
#
module Jobs
class PostToService
extend ResqueJobLogging
module Job
class PostToService < Base
@queue = :http_service
def self.perform(service_id, post_id, url)
def self.perform_delegate(service_id, post_id, url)
service = Service.find_by_id(service_id)
post = Post.find_by_id(post_id)
service.post(post, url)

View file

@ -3,11 +3,10 @@
# the COPYRIGHT file.
module Jobs
class PostToServices
extend ResqueJobLogging
module Job
class PostToServices < Base
@queue = :http_service
def self.perform(user_id, post_id, url)
def self.perform_delegate(user_id, post_id, url)
user = User.find_by_id(user_id)
post = Post.find_by_id(post_id)
user.services.each do |s|

View file

@ -2,12 +2,11 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module Jobs
class PublishToHub
extend ResqueJobLogging
module Job
class PublishToHub < Base
@queue = :http_service
def self.perform(sender_public_url)
def self.perform_delegate(sender_public_url)
require File.join(Rails.root, 'lib/pubsubhubbub')
Pubsubhubbub.new(AppConfig[:pubsub_server]).publish(sender_public_url)
end

View file

@ -3,9 +3,9 @@
# the COPYRIGHT file.
module Jobs
class Receive
extend ResqueJobLogging
module Job
class Receive < Base
@queue = :receive
def self.perform(user_id, xml, salmon_author_id)
user = User.find(user_id)

View file

@ -3,13 +3,12 @@
# the COPYRIGHT file.
module Jobs
class ReceiveLocal
module Job
class ReceiveLocal < Base
require File.join(Rails.root, 'lib/postzord/receiver')
extend ResqueJobLogging
@queue = :receive_local
def self.perform(user_id, person_id, object_type, object_id)
def self.perform_delegate(user_id, person_id, object_type, object_id)
user = User.find(user_id)
person = Person.find(person_id)
object = object_type.constantize.where(:id => object_id).first

View file

@ -4,11 +4,11 @@
require File.join(Rails.root, 'lib/postzord/receiver')
module Jobs
class ReceiveSalmon
extend ResqueJobLogging
module Job
class ReceiveSalmon < Base
@queue = :receive_salmon
def self.perform(user_id, xml)
def self.perform_delegate(user_id, xml)
user = User.find(user_id)
zord = Postzord::Receiver.new(user, :salmon_xml => xml)
zord.perform

View file

@ -3,11 +3,12 @@
# the COPYRIGHT file.
module Jobs
class SocketWebfinger
extend ResqueJobLogging
module Job
class SocketWebfinger < Base
@queue = :socket_webfinger
def self.perform(user_id, account, opts={})
def self.perform_delegate(user_id, account, opts={})
finger = Webfinger.new(account)
begin
user = User.find_by_id(user_id)

View file

@ -31,13 +31,13 @@ class Notification < ActiveRecord::Base
def email_the_user
case self.action
when "new_request"
self.recipient.mail(Jobs::MailRequestReceived, self.recipient_id, self.actor_id)
self.recipient.mail(Job::MailRequestReceived, self.recipient_id, self.actor_id)
when "request_accepted"
self.recipient.mail(Jobs::MailRequestAcceptance, self.recipient_id, self.actor_id)
self.recipient.mail(Job::MailRequestAcceptance, self.recipient_id, self.actor_id)
when "comment_on_post"
self.recipient.mail(Jobs::MailCommentOnPost, self.recipient_id, self.actor_id, target.id)
self.recipient.mail(Job::MailCommentOnPost, self.recipient_id, self.actor_id, target.id)
when "also_commented"
self.recipient.mail(Jobs::MailAlsoCommented, self.recipient_id, self.actor_id, target.id)
self.recipient.mail(Job::MailAlsoCommented, self.recipient_id, self.actor_id, target.id)
end
end
end

View file

@ -41,20 +41,20 @@ class Postzord::Dispatch
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(Jobs::HttpPost, person.receive_url, enc_xml)
Resque.enqueue(Job::HttpPost, person.receive_url, enc_xml)
end
end
def deliver_to_local(people)
people.each do |person|
Rails.logger.info("event=push_to_local_person route=local sender=#{@sender_person.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{@object.class}")
Resque.enqueue(Jobs::Receive, person.owner_id, @xml, @sender_person.id)
Resque.enqueue(Job::Receive, person.owner_id, @xml, @sender_person.id)
end
end
def deliver_to_hub
Rails.logger.debug("event=post_to_service type=pubsub sender_handle=#{@sender.diaspora_handle}")
Resque.enqueue(Jobs::PublishToHub, @sender.public_url)
Resque.enqueue(Job::PublishToHub, @sender.public_url)
end
def deliver_to_services(url)
@ -62,7 +62,7 @@ class Postzord::Dispatch
deliver_to_hub
if @object.respond_to?(:message)
@sender.services.each do |service|
Resque.enqueue(Jobs::PostToService, service.id, @object.id, url)
Resque.enqueue(Job::PostToService, service.id, @object.id, url)
end
end
end
@ -74,7 +74,7 @@ class Postzord::Dispatch
if socket
@object.socket_to_user(user)
end
Resque.enqueue(Jobs::NotifyLocalUsers, user.id, @object.class.to_s, @object.id, @sender_person.id)
Resque.enqueue(Job::NotifyLocalUsers, user.id, @object.class.to_s, @object.id, @sender_person.id)
end
end
end

View file

@ -26,7 +26,7 @@ describe InvitationsController do
request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'
end
it 'calls the resque job Jobs::InviteUser' do
it 'calls the resque job Job::InviteUser' do
Resque.should_receive(:enqueue)
post :create, :user => @invite
end

View file

@ -170,7 +170,7 @@ describe PeopleController do
describe '#webfinger' do
it 'enqueues a webfinger job' do
Resque.should_receive(:enqueue).with(Jobs::SocketWebfinger, @user.id, @user.diaspora_handle, anything).once
Resque.should_receive(:enqueue).with(Job::SocketWebfinger, @user.id, @user.diaspora_handle, anything).once
get :retrieve_remote, :diaspora_handle => @user.diaspora_handle
end
end

View file

@ -21,7 +21,7 @@ describe PublicsController do
end
it 'enqueues a receive job' do
Resque.should_receive(:enqueue).with(Jobs::ReceiveSalmon, @user.id, xml).once
Resque.should_receive(:enqueue).with(Job::ReceiveSalmon, @user.id, xml).once
post :receive, "guid" => @user.person.guid.to_s, "xml" => xml
end
@ -34,7 +34,7 @@ describe PublicsController do
salmon_factory = Salmon::SalmonSlap.create(@user, xml2)
enc_xml = salmon_factory.xml_for(user2.person)
Resque.should_receive(:enqueue).with(Jobs::ReceiveSalmon, @user.id, enc_xml).once
Resque.should_receive(:enqueue).with(Job::ReceiveSalmon, @user.id, enc_xml).once
post :receive, "guid" => @user.person.guid.to_s, "xml" => CGI::escape(enc_xml)
end

View file

@ -101,7 +101,7 @@ describe Postzord::Dispatch do
end
it 'should queue an HttpPost job for each remote person' do
Resque.should_receive(:enqueue).with(Jobs::HttpPost, @user.person.receive_url, anything).once
Resque.should_receive(:enqueue).with(Job::HttpPost, @user.person.receive_url, anything).once
@mailman.send(:deliver_to_remote, @remote_people)
end
@ -117,7 +117,7 @@ describe Postzord::Dispatch do
local_people = []
local_people << @user.person
mailman = Postzord::Dispatch.new(@user, @sm)
Resque.should_receive(:enqueue).with(Jobs::Receive, @user.id, @xml, anything).once
Resque.should_receive(:enqueue).with(Job::Receive, @user.id, @xml, anything).once
mailman.send(:deliver_to_local, local_people)
end
end
@ -130,15 +130,15 @@ describe Postzord::Dispatch do
end
it 'calls post for each of the users services' do
Resque.stub!(:enqueue).with(Jobs::PublishToHub, anything)
Resque.should_receive(:enqueue).with(Jobs::PostToService, @service.id, anything, anything).once
Resque.stub!(:enqueue).with(Job::PublishToHub, anything)
Resque.should_receive(:enqueue).with(Job::PostToService, @service.id, anything, anything).once
@zord.instance_variable_get(:@sender).should_receive(:services).and_return([@service])
@zord.send(:deliver_to_services, nil)
end
it 'queues a job to notify the hub' do
Resque.stub!(:enqueue).with(Jobs::PostToService, anything, anything, anything)
Resque.should_receive(:enqueue).with(Jobs::PublishToHub, @user.public_url)
Resque.stub!(:enqueue).with(Job::PostToService, anything, anything, anything)
Resque.should_receive(:enqueue).with(Job::PublishToHub, @user.public_url)
@zord.send(:deliver_to_services, nil)
end
@ -166,9 +166,9 @@ describe Postzord::Dispatch do
z.send(:socket_and_notify_users, users)
end
it 'queues a Jobs::NotifyLocalUsers jobs' do
it 'queues a Job::NotifyLocalUsers jobs' do
@zord.instance_variable_get(:@object).should_receive(:socket_to_user).and_return(false)
Resque.should_receive(:enqueue).with(Jobs::NotifyLocalUsers, @local_user.id, @sm.class.to_s, @sm.id, anything)
Resque.should_receive(:enqueue).with(Job::NotifyLocalUsers, @local_user.id, @sm.class.to_s, @sm.id, anything)
@zord.send(:socket_and_notify_users, [@local_user])
end
end

View file

@ -1,6 +1,6 @@
require 'spec_helper'
describe Jobs::HttpPost do
describe Job::HttpPost do
before do
@url = 'example.org/things/on/fire'
@body = '<xml>California</xml>'
@ -8,11 +8,11 @@ describe Jobs::HttpPost do
end
it 'POSTs to a given URL' do
RestClient.should_receive(:post).with(@url, {:xml=>@escaped_body}).and_return(true)
Jobs::HttpPost.perform(@url, @body, 3)
Job::HttpPost.perform(@url, @body, 3)
end
it 'retries' do
RestClient.should_receive(:post).with(@url, {:xml=>@escaped_body}).and_raise(SocketError)
Resque.should_receive(:enqueue).with(Jobs::HttpPost, @url, @escaped_body, 1).once
Jobs::HttpPost.perform(@url, @body, 2)
Resque.should_receive(:enqueue).with(Job::HttpPost, @url, @escaped_body, 1).once
Job::HttpPost.perform(@url, @body, 2)
end
end

View file

@ -4,7 +4,7 @@
require 'spec_helper'
describe Jobs::NotifyLocalUsers do
describe Job::NotifyLocalUsers do
describe '#perfom' do
it 'should call Notification.notify on the object' do
user = alice
@ -12,7 +12,7 @@ describe Jobs::NotifyLocalUsers do
object = Factory :status_message
Notification.should_receive(:notify).with(instance_of(User), instance_of(StatusMessage), instance_of(Person))
Jobs::NotifyLocalUsers.perform(user.id, object.class.to_s, object.id, person.id)
Job::NotifyLocalUsers.perform(user.id, object.class.to_s, object.id, person.id)
end
end
end

View file

@ -1,6 +1,6 @@
require 'spec_helper'
describe Jobs::PostToService do
describe Job::PostToService do
it 'calls service#post with the given service' do
user = alice
aspect = user.aspects.create(:name => "yeah")
@ -10,6 +10,6 @@ describe Jobs::PostToService do
url = "foobar"
m.should_receive(:post).with(anything, url)
Service.stub!(:find_by_id).and_return(m)
Jobs::PostToService.perform("123", post.id.to_s, url)
Job::PostToService.perform("123", post.id.to_s, url)
end
end

View file

@ -1,6 +1,6 @@
require 'spec_helper'
describe Jobs::PostToServices do
describe Job::PostToServices do
it 'calls post to services from the given user with given post' do
user = alice
aspect = user.aspects.create(:name => "yeah")
@ -10,6 +10,6 @@ describe Jobs::PostToServices do
url = "foobar"
m.should_receive(:post).with(anything, url)
user.stub!(:services).and_return([m])
Jobs::PostToServices.perform(user.id.to_s, post.id.to_s, url)
Job::PostToServices.perform(user.id.to_s, post.id.to_s, url)
end
end

View file

@ -4,7 +4,7 @@
require 'spec_helper'
describe Jobs::PublishToHub do
describe Job::PublishToHub do
describe '.perform' do
it 'calls pubsubhubbub' do
url = "http://publiczone.com/"
@ -12,7 +12,7 @@ describe Jobs::PublishToHub do
m.should_receive(:publish).with(url)
Pubsubhubbub.should_receive(:new).with(AppConfig[:pubsub_server]).and_return(m)
Jobs::PublishToHub.perform(url)
Job::PublishToHub.perform(url)
end
end
end

View file

@ -1,6 +1,6 @@
require 'spec_helper'
describe Jobs::ReceiveLocal do
describe Job::ReceiveLocal do
before do
@user1 = alice
@person1 = @user1.person
@ -38,6 +38,6 @@ describe Jobs::ReceiveLocal do
m = mock()
m.should_receive(:receive_object)
Postzord::Receiver.should_receive(:new).and_return(m)
Jobs::ReceiveLocal.perform(@user1.id, @person2.id, @status_type, @status.id)
Job::ReceiveLocal.perform(@user1.id, @person2.id, @status_type, @status.id)
end
end

View file

@ -1,6 +1,6 @@
require 'spec_helper'
describe Jobs::ReceiveSalmon do
describe Job::ReceiveSalmon do
before do
@user = alice
@xml = '<xml></xml>'
@ -17,6 +17,6 @@ describe Jobs::ReceiveSalmon do
salmon_mock.should_receive(:perform)
Postzord::Receiver.should_receive(:new).and_return(salmon_mock)
Jobs::ReceiveSalmon.perform(@user.id, @xml)
Job::ReceiveSalmon.perform(@user.id, @xml)
end
end

View file

@ -1,6 +1,6 @@
require 'spec_helper'
describe Jobs::Receive do
describe Job::Receive do
before do
@user = alice
@person = Factory(:person)
@ -18,6 +18,6 @@ describe Jobs::Receive do
zord_mock = mock()
zord_mock.should_receive(:parse_and_receive).with(@xml)
Postzord::Receiver.should_receive(:new).with(@user, anything).and_return(zord_mock)
Jobs::Receive.perform(@user.id, @xml, @person.id)
Job::Receive.perform(@user.id, @xml, @person.id)
end
end

View file

@ -1,20 +1,20 @@
require 'spec_helper'
describe Jobs::SocketWebfinger do
describe Job::SocketWebfinger do
before do
@user = alice
@account = "tom@tom.joindiaspora.com"
end
it 'Makes a Webfinger object' do
Webfinger.should_receive(:new).with(@account)
Jobs::SocketWebfinger.perform(@user.id, @account)
Job::SocketWebfinger.perform(@user.id, @account)
end
it 'Queries the target account' do
finger = mock()
Webfinger.stub(:new).and_return(finger)
finger.should_receive(:fetch).and_return(Factory.create(:person))
Jobs::SocketWebfinger.perform(@user.id, @account)
Job::SocketWebfinger.perform(@user.id, @account)
end
it 'Sockets the resulting person on success' do
finger = mock()
@ -23,7 +23,7 @@ describe Jobs::SocketWebfinger do
finger.stub(:fetch).and_return(person)
person.should_receive(:socket_to_user).with(@user, {})
Jobs::SocketWebfinger.perform(@user.id, @account)
Job::SocketWebfinger.perform(@user.id, @account)
end
it 'Passes opts through on success' do
finger = mock()
@ -33,7 +33,7 @@ describe Jobs::SocketWebfinger do
opts = {:symbol => true}
person.should_receive(:socket_to_user).with(@user, opts)
Jobs::SocketWebfinger.perform(@user.id, @account, opts)
Job::SocketWebfinger.perform(@user.id, @account, opts)
end
it 'sockets failure message on failure' do
finger = mock()
@ -42,7 +42,7 @@ describe Jobs::SocketWebfinger do
opts = {:class => 'people', :status => 'fail', :query => @account, :response => I18n.t('people.webfinger.fail', :handle => @account )}.to_json
Diaspora::WebSocket.should_receive(:queue_to_user).with(@user.id, opts)
Jobs::SocketWebfinger.perform(@user.id, @account)
Job::SocketWebfinger.perform(@user.id, @account)
end
end

View file

@ -67,7 +67,7 @@ describe Diaspora::UserModules::Connecting do
end
it 'enqueues a mail job' do
Resque.should_receive(:enqueue).with(Jobs::MailRequestReceived, user.id, person.id)
Resque.should_receive(:enqueue).with(Job::MailRequestReceived, user.id, person.id)
zord = Postzord::Receiver.new(user, :object => @r, :person => person)
zord.receive_object
end
@ -87,7 +87,7 @@ describe Diaspora::UserModules::Connecting do
Request.where(:sender_id => user2.person.id, :recipient_id => user.person.id).should be_empty
end
it 'enqueues a mail job' do
Resque.should_receive(:enqueue).with(Jobs::MailRequestAcceptance, user.id, user2.person.id).once
Resque.should_receive(:enqueue).with(Job::MailRequestAcceptance, user.id, user2.person.id).once
zord = Postzord::Receiver.new(user, :object => @acceptance, :person => user2.person)
zord.receive_object
end

View file

@ -387,8 +387,8 @@ describe User do
user.save
user.reload
Resque.should_receive(:enqueue).with(Jobs::MailRequestReceived, user.id, 'contactrequestid').once
user.mail(Jobs::MailRequestReceived, user.id, 'contactrequestid')
Resque.should_receive(:enqueue).with(Job::MailRequestReceived, user.id, 'contactrequestid').once
user.mail(Job::MailRequestReceived, user.id, 'contactrequestid')
end
it 'does not enqueue a mail job' do
@ -397,7 +397,7 @@ describe User do
user.reload
Resque.should_not_receive(:enqueue)
user.mail(Jobs::MailRequestReceived, user.id, 'contactrequestid')
user.mail(Job::MailRequestReceived, user.id, 'contactrequestid')
end
end