Skip note emails when the note was deleted before sending
What does this MR do and why?
When a comment is deleted after its notification mail jobs are queued but before they run, each ActionMailer::MailDeliveryJob raised ActiveRecord::RecordNotFound from Note.find in Emails::Notes#setup_note_mail. Sidekiq retried each job 3 times (retry: 3 for MailDeliveryJob), and each job ended in the dead set. One job is queued per recipient, so a single deleted comment on a project with many watchers could produce hundreds of failures and set off Sidekiq retry and SLI alerts for a case that can never recover.
This MR looks the note up with Note.find_by_id and skips delivery when the note is gone. Each note_*_email action (6 in CE, note_epic_email in EE) returns before calling mail, so ActionMailer delivers a NullMail and the job finishes without raising. This is the same find-and-return-early pattern other mailers already use for missing records (remote_mirrors.rb, groups.rb, reviews.rb, profile.rb).
The 3 existing Note.find stubs in spec/mailers/notify_spec.rb now stub Note.find_by_id, because those examples stub methods on that exact note instance.
Out of scope, and possible follow-ups:
User.find(recipient_id)in the same method fails the same way if the recipient is deleted first.Emails::ServiceDesk#service_desk_new_note_emailalso usesNote.find.
References
Screenshots or screen recordings
Not applicable: no UI change.
How to set up and validate locally
- Create a project with a few members whose notification level for it is Watch, and an issue in it.
- Keep the
mailersqueue from running right away (for example, stop Sidekiq). - Comment on the issue, so
NewNoteWorkerqueues oneNotify#note_issue_emailjob per watcher. - Delete the comment.
- Start Sidekiq again. Before this change, every job fails with
ActiveRecord::RecordNotFound: Couldn't find Note with 'id'=<id>and is retried. With this change, the jobs finish without sending an email and without a retry.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.