Commit dde55d70a3fd97357cb226ef0f94afeb7b3ee9af

Authored by Victor Costa
1 parent bd06936c

Avoid loop in person notification job

app/models/person_notifier.rb
... ... @@ -59,8 +59,12 @@ class PersonNotifier
59 59 end
60 60  
61 61 def failure(job)
62   - person = Person.find(person_id)
63   - person.notifier.dispatch_notification_mail
  62 + begin
  63 + person = Person.find(person_id)
  64 + person.notifier.dispatch_notification_mail
  65 + rescue
  66 + Rails.logger.error "PersonNotifier::NotifyJob: Cannot recover from failure"
  67 + end
64 68 end
65 69  
66 70 end
... ...
test/unit/person_notifier_test.rb
... ... @@ -225,6 +225,18 @@ class PersonNotifierTest < ActiveSupport::TestCase
225 225 assert !jobs.select {|j| !j.failed? && j.last_error.nil? }.empty?
226 226 end
227 227  
  228 + should 'do not raise errors in NotifyJob failure to avoid loop' do
  229 + Delayed::Worker.max_attempts = 1
  230 + Delayed::Job.enqueue(PersonNotifier::NotifyJob.new(@member.id))
  231 +
  232 + PersonNotifier.any_instance.stubs(:notify).raises('error')
  233 + PersonNotifier.any_instance.stubs(:dispatch_notification_mail).raises('error')
  234 +
  235 + process_delayed_job_queue
  236 + jobs = PersonNotifier::NotifyJob.find(@member.id)
  237 + assert jobs.select {|j| !j.failed? && j.last_error.nil? }.empty?
  238 + end
  239 +
228 240 private
229 241  
230 242 def notify
... ...