From 55729b442f1fea0923834e2be3b9be8efcdbf9e3 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Thu, 14 Oct 2010 15:32:36 -0300 Subject: [PATCH] Avoiding some 'delayed job' errors --- lib/create_thumbnails_job.rb | 1 + lib/get_email_contacts_job.rb | 4 ++-- lib/invitation_job.rb | 2 +- lib/notify_activity_to_profiles_job.rb | 1 + test/unit/create_thumbnails_job_test.rb | 10 ++++++++++ test/unit/get_email_contacts_job_test.rb | 20 ++++++++++++++++++++ test/unit/invitation_job_test.rb | 10 ++++++++++ test/unit/notify_activity_to_profiles_job_test.rb | 13 +++++++++++++ 8 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/create_thumbnails_job.rb b/lib/create_thumbnails_job.rb index 4c64537..b9dc034 100644 --- a/lib/create_thumbnails_job.rb +++ b/lib/create_thumbnails_job.rb @@ -1,5 +1,6 @@ class CreateThumbnailsJob < Struct.new(:class_name, :file_id) def perform + return unless class_name.constantize.exists?(file_id) Article.disable_ferret # acts_as_ferret sucks file = class_name.constantize.find(file_id) file.create_thumbnails diff --git a/lib/get_email_contacts_job.rb b/lib/get_email_contacts_job.rb index 92a8199..f911fb4 100644 --- a/lib/get_email_contacts_job.rb +++ b/lib/get_email_contacts_job.rb @@ -3,9 +3,9 @@ class GetEmailContactsJob < Struct.new(:import_from, :login, :password, :contact begin Invitation.get_contacts(import_from, login, password, contact_list_id) rescue Contacts::AuthenticationError => ex - ContactList.find(contact_list_id).register_auth_error + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_auth_error rescue Exception => ex - ContactList.find(contact_list_id).register_error + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).register_error end end end diff --git a/lib/invitation_job.rb b/lib/invitation_job.rb index a084453..c42fbb8 100644 --- a/lib/invitation_job.rb +++ b/lib/invitation_job.rb @@ -4,7 +4,7 @@ class InvitationJob < Struct.new(:person_id, :contacts_to_invite, :message, :pro person = Person.find(person_id) profile = Profile.find(profile_id) Invitation.invite(person, contacts_to_invite, message, profile) - ContactList.find(contact_list_id).destroy + ContactList.exists?(contact_list_id) && ContactList.find(contact_list_id).destroy end end end diff --git a/lib/notify_activity_to_profiles_job.rb b/lib/notify_activity_to_profiles_job.rb index 577fcec..1409161 100644 --- a/lib/notify_activity_to_profiles_job.rb +++ b/lib/notify_activity_to_profiles_job.rb @@ -9,6 +9,7 @@ class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id) 'leave_community', ] def perform + return unless ActionTracker::Record.exists?(tracked_action_id) tracked_action = ActionTracker::Record.find(tracked_action_id) target = tracked_action.target if target.is_a?(Community) && NOTIFY_ONLY_COMMUNITY.include?(tracked_action.verb) diff --git a/test/unit/create_thumbnails_job_test.rb b/test/unit/create_thumbnails_job_test.rb index 8842222..4ea1934 100644 --- a/test/unit/create_thumbnails_job_test.rb +++ b/test/unit/create_thumbnails_job_test.rb @@ -24,4 +24,14 @@ class CreateThumbnailsJobTest < ActiveSupport::TestCase assert file.thumbnails_processed end + should 'not create thumbnails from deleted files' do + person = create_user('test_user').person + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => person) + job = CreateThumbnailsJob.new(file.class.name, file.id) + file.destroy + assert_nothing_raised do + job.perform + end + end + end diff --git a/test/unit/get_email_contacts_job_test.rb b/test/unit/get_email_contacts_job_test.rb index 6c833b8..202cb8b 100644 --- a/test/unit/get_email_contacts_job_test.rb +++ b/test/unit/get_email_contacts_job_test.rb @@ -24,4 +24,24 @@ class GetEmailContactsJobTest < ActiveSupport::TestCase assert_equal 'There was an error while authenticating. Did you enter correct login and password?', ContactList.find(contact_list).error_fetching end + should 'not try to register error if has no contact list' do + contact_list = ContactList.create + Invitation.expects(:get_contacts).with('from-email', 'mylogin', 'mypassword', contact_list.id).raises(Exception.new("crash")) + job = GetEmailContactsJob.new('from-email', 'mylogin', 'mypassword', contact_list.id) + contact_list.destroy + assert_nothing_raised do + job.perform + end + end + + should 'not try to register auth error if has no contact list' do + contact_list = ContactList.create + Invitation.expects(:get_contacts).with('from-email', 'mylogin', 'wrongpassword', contact_list.id).raises(Contacts::AuthenticationError) + job = GetEmailContactsJob.new('from-email', 'mylogin', 'wrongpassword', contact_list.id) + contact_list.destroy + assert_nothing_raised do + job.perform + end + end + end diff --git a/test/unit/invitation_job_test.rb b/test/unit/invitation_job_test.rb index 91d1621..0d091b6 100644 --- a/test/unit/invitation_job_test.rb +++ b/test/unit/invitation_job_test.rb @@ -25,4 +25,14 @@ class InvitationJobTest < ActiveSupport::TestCase job.perform end + should 'skip contact list deletion if it not exists' do + contact_list = ContactList.create! + person = create_user('maluquete').person + job = InvitationJob.new(person.id, ['email1@example.com'], 'Hi!', person.id, contact_list.id) + contact_list.destroy + assert_nothing_raised do + job.perform + end + end + end diff --git a/test/unit/notify_activity_to_profiles_job_test.rb b/test/unit/notify_activity_to_profiles_job_test.rb index b1ebf4d..24de4a4 100644 --- a/test/unit/notify_activity_to_profiles_job_test.rb +++ b/test/unit/notify_activity_to_profiles_job_test.rb @@ -166,4 +166,17 @@ class NotifyActivityToProfilesJobTest < ActiveSupport::TestCase assert_equal [], NotifyActivityToProfilesJob::NOT_NOTIFY_COMMUNITY - not_notify_community_verbs end + should 'cancel notify when target no more exists' do + person = fast_create(Person) + friend = fast_create(Person) + fast_create(Friendship, :person_id => person.id, :friend_id => friend.id) + action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :verb => 'create_article') + ActionTrackerNotification.delete_all + job = NotifyActivityToProfilesJob.new(action_tracker.id) + person.destroy + job.perform + process_delayed_job_queue + assert_equal 0, ActionTrackerNotification.count + end + end -- libgit2 0.21.2