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

https://meta.discourse.org/t/cleaning-up-e-mail-logs/39132
This commit is contained in:
Guo Xiang Tan 2018-08-08 16:25:00 +08:00
parent 1ea23b1eae
commit ba6f11c521
2 changed files with 21 additions and 4 deletions

View File

@ -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

View File

@ -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",