Skip to content

Commit 832e7cd

Browse files
committed
Use processed_at for notification delivery
A notification is considered processed if we’ve initiated delivery. This is different than saying the delivery has completed; we may have skipped delivery or the email job may have failed. This is cheating a bit; it might be better to put "processed_at" after the actual email delivery but I’d probably end up building another wrapper job around individual email sends. I’m skipping that complexity for now. I should also consider adding an index for the notification query.
1 parent 29858b8 commit 832e7cd

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

app/jobs/notifications/event_job.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ class Notifications::EventJob < ApplicationJob
44
def perform(event)
55
# Enqueue individual deliveries
66
event.notifications.each do |notification|
7-
event.deliver_notification(notification)
7+
notification.transaction do
8+
event.deliver_notification(notification)
9+
notification.touch(:processed_at)
10+
end
811
end
912
end
1013
end

app/models/notification.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ class Notification < ApplicationRecord
55
scope :newest_first, -> { order(created_at: :desc) }
66

77
delegate :params, :record, to: :event
8+
9+
scope :processed, -> { where.not(processed_at: nil) }
810
end

app/notifiers/welcome_notifier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ def deliver_notification(notification)
1616
end
1717

1818
def deliver_to?(recipient)
19-
Notification.joins(:notification_event).where(recipient: recipient, notification_event: {type: self.class.name}).count < 2
19+
Notification.processed.joins(:notification_event).where(recipient: recipient, notification_event: {type: self.class.name}).count < 1
2020
end
2121
end

0 commit comments

Comments
 (0)