Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Fix reminder frequency not getting sent daily when early by a few seconds/minutes #496

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/jobs/scheduled/enqueue_reminders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def allowed_group_ids
Group.assign_allowed_groups.pluck(:id).join(",")
end

REMINDER_BUFFER_MINUTES = 10

def user_ids
global_frequency = SiteSetting.remind_assigns_frequency
frequency =
Expand All @@ -46,7 +48,7 @@ def user_ids
AND #{frequency} > 0
AND (
last_reminder.value IS NULL OR
last_reminder.value::TIMESTAMP <= CURRENT_TIMESTAMP - ('1 MINUTE'::INTERVAL * #{frequency})
last_reminder.value::TIMESTAMP <= CURRENT_TIMESTAMP - ('1 MINUTE'::INTERVAL * #{frequency}) + ('1 MINUTE'::INTERVAL * #{REMINDER_BUFFER_MINUTES})
)
AND assignments.updated_at::TIMESTAMP <= CURRENT_TIMESTAMP - ('1 MINUTE'::INTERVAL * #{frequency})
AND assignments.assigned_to_type = 'User'
Expand Down
87 changes: 51 additions & 36 deletions spec/jobs/scheduled/enqueue_reminders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
require "rails_helper"

RSpec.describe Jobs::EnqueueReminders do
let(:assign_allowed_group) { Group.find_by(name: "staff") }
let(:user) { Fabricate(:user, groups: [assign_allowed_group]) }
fab!(:assign_allowed_group) { Fabricate(:group) }
fab!(:user) { Fabricate(:user, groups: [assign_allowed_group]) }

before do
SiteSetting.remind_assigns_frequency = RemindAssignsFrequencySiteSettings::MONTHLY_MINUTES
SiteSetting.assign_enabled = true
SiteSetting.assign_allowed_on_groups = "#{assign_allowed_group.id}"
end

describe "#execute" do
Expand Down Expand Up @@ -36,55 +37,69 @@
assert_reminders_enqueued(0)
end

it "does not enqueue a reminder if it's too soon" do
user.upsert_custom_fields(PendingAssignsReminder::REMINDED_AT => 2.days.ago)
assign_multiple_tasks_to(user)
it "doesn't count assigns from deleted topics" do
deleted_post = Fabricate(:post)
assign_one_task_to(user, post: deleted_post)
(PendingAssignsReminder::REMINDER_THRESHOLD - 1).times { assign_one_task_to(user) }

deleted_post.topic.trash!

assert_reminders_enqueued(0)
end

it "enqueues a reminder if the user was reminded more than a month ago" do
user.upsert_custom_fields(PendingAssignsReminder::REMINDED_AT => 31.days.ago)
assign_multiple_tasks_to(user)
describe "assignment frequency" do
it "enqueues a reminder if the user reminder frequency is 1 day and the last reminded at is almost 1 day" do
user.custom_fields[
PendingAssignsReminder::REMINDERS_FREQUENCY
] = RemindAssignsFrequencySiteSettings::DAILY_MINUTES
user.custom_fields[PendingAssignsReminder::REMINDED_AT] = 1.days.ago
user.save

assert_reminders_enqueued(1)
end
assign_multiple_tasks_to(user, assigned_on: 1.day.ago - 1.minute)

it "does not enqueue reminders if the remind frequency is set to never" do
SiteSetting.remind_assigns_frequency = 0
assign_multiple_tasks_to(user)
assert_reminders_enqueued(1)
end
Comment on lines +51 to +61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-obvious, but this is the new test for the buffer.


assert_reminders_enqueued(0)
end
it "does not enqueue a reminder if it's too soon" do
user.upsert_custom_fields(PendingAssignsReminder::REMINDED_AT => 1.days.ago)
assign_multiple_tasks_to(user)

it "does not enqueue reminders if the topic was just assigned to the user" do
just_assigned = DateTime.now
assign_multiple_tasks_to(user, assigned_on: just_assigned)
assert_reminders_enqueued(0)
end

assert_reminders_enqueued(0)
end
it "enqueues a reminder if the user was reminded more than a month ago" do
user.upsert_custom_fields(PendingAssignsReminder::REMINDED_AT => 31.days.ago)
assign_multiple_tasks_to(user)

it "enqueues a reminder when the user overrides the global frequency" do
SiteSetting.remind_assigns_frequency = 0
user.custom_fields.merge!(
PendingAssignsReminder::REMINDERS_FREQUENCY =>
RemindAssignsFrequencySiteSettings::DAILY_MINUTES,
)
user.save_custom_fields
assert_reminders_enqueued(1)
end

assign_multiple_tasks_to(user)
it "does not enqueue reminders if the remind frequency is set to never" do
SiteSetting.remind_assigns_frequency = 0
assign_multiple_tasks_to(user)

assert_reminders_enqueued(1)
end
assert_reminders_enqueued(0)
end

it "doesn't count assigns from deleted topics" do
deleted_post = Fabricate(:post)
assign_one_task_to(user, post: deleted_post)
(PendingAssignsReminder::REMINDER_THRESHOLD - 1).times { assign_one_task_to(user) }
it "does not enqueue reminders if the topic was just assigned to the user" do
just_assigned = DateTime.now
assign_multiple_tasks_to(user, assigned_on: just_assigned)

deleted_post.topic.trash!
assert_reminders_enqueued(0)
end

assert_reminders_enqueued(0)
it "enqueues a reminder when the user overrides the global frequency" do
SiteSetting.remind_assigns_frequency = 0
user.custom_fields.merge!(
PendingAssignsReminder::REMINDERS_FREQUENCY =>
RemindAssignsFrequencySiteSettings::DAILY_MINUTES,
)
user.save_custom_fields

assign_multiple_tasks_to(user)

assert_reminders_enqueued(1)
end
end

def assert_reminders_enqueued(expected_amount)
Expand Down