diff --git a/app/models/person_notifier.rb b/app/models/person_notifier.rb index 590ffa9..ddab97f 100644 --- a/app/models/person_notifier.rb +++ b/app/models/person_notifier.rb @@ -36,7 +36,7 @@ class PersonNotifier class NotifyAllJob def self.exists? - Delayed::Job.where(:handler => "--- !ruby/object:PersonNotifier::NotifyAllJob {}\n").count > 0 + Delayed::Job.by_handler("--- !ruby/object:PersonNotifier::NotifyAllJob {}\n").count > 0 end def perform @@ -51,7 +51,7 @@ class PersonNotifier end def self.find(person_id) - Delayed::Job.where(:handler => "--- !ruby/struct:PersonNotifier::NotifyJob\nperson_id: #{person_id}\n") + Delayed::Job.by_handler("--- !ruby/struct:PersonNotifier::NotifyJob\nperson_id: #{person_id}\n") end def perform diff --git a/config/initializers/delayed_job_config.rb b/config/initializers/delayed_job_config.rb index 7f026b5..6aa3e45 100644 --- a/config/initializers/delayed_job_config.rb +++ b/config/initializers/delayed_job_config.rb @@ -2,6 +2,16 @@ require 'delayed_job' Delayed::Worker.backend = :active_record Delayed::Worker.max_attempts = 2 +class Delayed::Job + def self.handler_like(handler) + Delayed::Job.where("handler LIKE '%#{handler}%'") + end + + def self.by_handler(handler) + Delayed::Job.where(:handler => handler) + end +end + # TODO This is consuming ton of space on development with a postgres connection # error on the jobs. This must be verified before going into production. # Logging jobs backtraces diff --git a/test/functional/invite_controller_test.rb b/test/functional/invite_controller_test.rb index cb471a4..d1edcc1 100644 --- a/test/functional/invite_controller_test.rb +++ b/test/functional/invite_controller_test.rb @@ -231,7 +231,7 @@ class InviteControllerTest < ActionController::TestCase contact_list = ContactList.create post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: ", :contact_list => contact_list.id - job = Delayed::Job.where("handler LIKE '%InvitationJob%'").first + job = Delayed::Job.handler_like(InvitationJob.name).first assert_equal 'pt', job.payload_object.locale end diff --git a/test/unit/person_notifier_test.rb b/test/unit/person_notifier_test.rb index 4bbac79..8495e72 100644 --- a/test/unit/person_notifier_test.rb +++ b/test/unit/person_notifier_test.rb @@ -34,14 +34,14 @@ class PersonNotifierTest < ActiveSupport::TestCase should 'do not send mail if do not have notifications' do @community.add_member(@member) ActionTracker::Record.delete_all - assert_no_difference ActionMailer::Base.deliveries, :count do + assert_no_difference 'ActionMailer::Base.deliveries.count' do notify end end should 'do not send mail to people not joined to community' do Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article) - assert_no_difference ActionMailer::Base.deliveries, :count do + assert_no_difference 'ActionMailer::Base.deliveries.count' do notify end end @@ -60,7 +60,7 @@ class PersonNotifierTest < ActiveSupport::TestCase ActionTracker::Record.delete_all comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article ) @member.last_notification = DateTime.now + 1.day - assert_no_difference ActionMailer::Base.deliveries, :count do + assert_no_difference 'ActionMailer::Base.deliveries.count' do notify end end @@ -85,7 +85,7 @@ class PersonNotifierTest < ActiveSupport::TestCase @member.notification_time = 12 time = Time.now @member.notifier.schedule_next_notification_mail - job = Delayed::Job.where("handler like '%PersonNotifier::NotifyJob%'").last + job = Delayed::Job.handler_like(PersonNotifier::NotifyJob.name).last assert job.run_at >= time + @member.notification_time.hours assert job.run_at < time + (@member.notification_time+1).hours end @@ -93,14 +93,14 @@ class PersonNotifierTest < ActiveSupport::TestCase should 'do not schedule duplicated notification mail' do @member.notification_time = 12 @member.notifier.schedule_next_notification_mail - assert_no_difference 'Delayed::Job.count' do + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do @member.notifier.schedule_next_notification_mail end end should 'do not schedule next mail if notification time is zero' do @member.notification_time = 0 - assert_no_difference 'Delayed::Job.count' do + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do @member.notifier.schedule_next_notification_mail end end @@ -118,17 +118,17 @@ class PersonNotifierTest < ActiveSupport::TestCase should 'do not create duplicated job' do PersonNotifier.schedule_all_next_notification_mail - assert_no_difference 'Delayed::Job.count' do + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do PersonNotifier.schedule_all_next_notification_mail end end should 'schedule after update and set a valid notification time' do - assert_no_difference 'Delayed::Job.count' do + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do @member.notification_time = 0 @member.save! end - assert_difference 'Delayed::Job.count', 1 do + assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do @member.notification_time = 12 @member.save! end @@ -136,16 +136,16 @@ class PersonNotifierTest < ActiveSupport::TestCase should 'reschedule with changed notification time' do time = Time.now - assert_difference Delayed::Job, :count, 1 do + assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do @member.notification_time = 2 @member.save! end - assert_no_difference Delayed::Job, :count do + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do @member.notification_time = 12 @member.save! end @member.notifier.schedule_next_notification_mail - job = Delayed::Job.where("handler like '%PersonNotifier::NotifyJob%'").last + job = Delayed::Job.handler_like(PersonNotifier::NotifyJob.name).last assert job.run_at >= time + @member.notification_time.hours assert job.run_at < time + (@member.notification_time+1).hours end @@ -197,7 +197,7 @@ class PersonNotifierTest < ActiveSupport::TestCase end should 'perform create NotifyJob for all users with notification_time' do - assert_difference Delayed::Job, :count, 2 do + assert_difference 'job_count(PersonNotifier::NotifyJob)', 2 do Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new) process_delayed_job_queue end @@ -225,8 +225,14 @@ class PersonNotifierTest < ActiveSupport::TestCase assert !jobs.select {|j| !j.failed? && j.last_error.nil? }.empty? end + private + def notify @member.notifier.notify end + def job_count(job) + Delayed::Job.handler_like(job.name).count + end + end -- libgit2 0.21.2