Skip to content

A platform for community discussion. Free, open, simple.

PERF: Only log the first skipped email when user exceeds daily limit.

Guo Xiang Tanba6f11c521b6
diff --git a/app/jobs/regular/notify_mailing_list_subscribers.rb b/app/jobs/regular/notify_mailing_list_subscribers.rb
index a75603b172e..d6f0484cef1 100644
--- a/app/jobs/regular/notify_mailing_list_subscribers.rb
+++ b/app/jobs/regular/notify_mailing_list_subscribers.rb
@@ -80,13 +80,23 @@ module Jobs
end
def skip(to_address, user_id, post_id, reason_type)
- SkippedEmailLog.create!(
+ attributes = {
email_type: 'mailing_list',
to_address: to_address,
user_id: user_id,
post_id: post_id,
reason_type: reason_type
- )
+ }
+
+ if reason_type == SkippedEmailLog.reason_types[:exceeded_emails_limit]
+ exists = SkippedEmailLog.exists?({
+ created_at: (Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
+ }.merge(attributes))
+
+ return if exists
+ end
+
+ SkippedEmailLog.create!(attributes)
end
end
end
diff --git a/spec/jobs/notify_mailing_list_subscribers_spec.rb b/spec/jobs/notify_mailing_list_subscribers_spec.rb
index ec42df7bd4b..7bb5b3265f4 100644
--- a/spec/jobs/notify_mailing_list_subscribers_spec.rb
+++ b/spec/jobs/notify_mailing_list_subscribers_spec.rb
@@ -126,8 +126,15 @@ describe Jobs::NotifyMailingListSubscribers do
mailing_list_user.email_logs.create(email_type: 'foobar', to_address: mailing_list_user.email)
}
- Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
- UserNotifications.expects(:mailing_list_notify).with(mailing_list_user, post).never
+ expect do
+ UserNotifications.expects(:mailing_list_notify)
+ .with(mailing_list_user, post)
+ .never
+
+ 2.times do
+ Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
+ end
+ end.to change { SkippedEmailLog.count }.by(1)
expect(SkippedEmailLog.exists?(
email_type: "mailing_list",