Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

handle having missed a slot for the reminder mails

ulferts853cd81bbb9b
diff --git a/app/models/user.rb b/app/models/user.rb
index fafd653866a..f3a440f0bd6 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -77,7 +77,7 @@ class User < Principal
:newest,
:notified_on_all,
:watcher_recipients,
- :having_reminder_mail_to_send_now
+ :having_reminder_mail_to_send
def self.create_blocked_scope(scope, blocked)
scope.where(blocked_condition(blocked))
diff --git a/app/models/users/scopes/having_reminder_mail_to_send_now.rb b/app/models/users/scopes/having_reminder_mail_to_send.rb
similarity index 69%
rename from app/models/users/scopes/having_reminder_mail_to_send_now.rb
rename to app/models/users/scopes/having_reminder_mail_to_send.rb
index f3866b9103f..14aab6442b0 100644
--- a/app/models/users/scopes/having_reminder_mail_to_send_now.rb
+++ b/app/models/users/scopes/having_reminder_mail_to_send.rb
@@ -29,7 +29,7 @@
#++
module Users::Scopes
- module HavingReminderMailToSendNow
+ module HavingReminderMailToSend
extend ActiveSupport::Concern
class_methods do
@@ -39,13 +39,17 @@ module Users::Scopes
# * The user has configured reminder mails to be sent now.
# This assumes that users only have full hours specified for the times they desire
# to receive a reminder mail at.
- def having_reminder_mail_to_send_now
+ # @param [DateTime] earliest_time The earliest time to consider as a matching slot. All full hours from that time
+ # to now are included.
+ # Only the time part is used and that is floored to the hour (e.g. 2021-05-03 10:34:12+02:00 -> 08:00:00).
+ # Needs to be before the current time.
+ def having_reminder_mail_to_send(earliest_time)
# Left outer join as not all user instances have preferences associated
# but we still want to select them.
recipient_candidates = User
.active
.left_joins(:preference)
- .joins(local_time_join)
+ .joins(local_time_join(earliest_time))
subscriber_ids = Notification
.unsent_reminders_before(recipient: recipient_candidates, time: Time.current)
@@ -55,14 +59,14 @@ module Users::Scopes
where(id: subscriber_ids)
end
- def local_time_join
+ def local_time_join(earliest_time)
# Joins the times local to the user preferences and then checks whether:
# * reminders are enabled
# * any of the configured reminder time is the local time
# If no time zone is present, utc is assumed.
# If no reminder settings are present, sending a reminder at 08:00 local time is assumed.
<<~SQL.squish
- JOIN (#{local_time_table}) AS local_times
+ JOIN (#{local_time_table(earliest_time)}) AS local_times
ON COALESCE(user_preferences.settings->>'time_zone', 'UTC') = local_times.zone
AND (
(
@@ -78,16 +82,31 @@ module Users::Scopes
SQL
end
- def local_time_table
- current_time = Time.current
+ def local_time_table(earliest_time)
+ times = hours_between_earliest_and_now(earliest_time)
- times_with_zones = ActiveSupport::TimeZone
- .all
- .map do |z|
- [current_time.in_time_zone(z).strftime('%H:00:00+00:00'), z.name.gsub("'", "''")]
- end
+ values_list = Arel::Nodes::ValuesList.new(times_for_zones(times))
- "SELECT * FROM #{arel_table.grouping(Arel::Nodes::ValuesList.new(times_with_zones)).as('t(time, zone)').to_sql}"
+ "SELECT * FROM #{arel_table.grouping(values_list).as('t(time, zone)').to_sql}"
+ end
+
+ def times_for_zones(times)
+ ActiveSupport::TimeZone
+ .all
+ .map do |z|
+ times.map do |time|
+ [time.in_time_zone(z).strftime('%H:00:00+00:00'), z.name.gsub("'", "''")]
+ end
+ end
+ .flatten(1)
+ end
+
+ def hours_between_earliest_and_now(earliest_time)
+ raise ArgumentError if Time.current - earliest_time < 0
+
+ (0..(((Time.current - earliest_time) / 1.hour).round)).map do |i|
+ (earliest_time + i.hours).utc
+ end
end
end
end
diff --git a/app/workers/notifications/schedule_reminder_mails_job.rb b/app/workers/notifications/schedule_reminder_mails_job.rb
index 21c0036cd73..6aea431fcdf 100644
--- a/app/workers/notifications/schedule_reminder_mails_job.rb
+++ b/app/workers/notifications/schedule_reminder_mails_job.rb
@@ -34,9 +34,13 @@ module Notifications
self.cron_expression = '0 * * * *'
def perform
- User.having_reminder_mail_to_send_now.pluck(:id).each do |subscriber_ids|
+ User.having_reminder_mail_to_send(run_at).pluck(:id).each do |subscriber_ids|
Mails::ReminderJob.perform_later(subscriber_ids)
end
end
+
+ def run_at
+ self.class.delayed_job.run_at
+ end
end
end
diff --git a/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
similarity index 84%
rename from spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb
rename to spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
index cfd8c28b6d0..fe41aa2895e 100644
--- a/spec/models/users/scopes/having_reminder_mail_to_send_now_spec.rb
+++ b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb
@@ -30,14 +30,15 @@
require 'spec_helper'
-describe User, '.with_reminder_mail_to_send_now', type: :job do
+describe User, '.with_reminder_mail_to_send', type: :job do
subject(:scope) do
- described_class.having_reminder_mail_to_send_now
+ described_class.having_reminder_mail_to_send(scope_time)
end
# As it is hard to mock PostgreSQL's "now()" method, in the specs here we need to adopt the slot time
# relative to the local time of the user that we want to hit.
let(:current_utc_time) { Time.current.getutc }
+ let(:scope_time) { Time.current }
let(:paris_user) do
FactoryBot.create(
@@ -114,6 +115,42 @@ describe User, '.with_reminder_mail_to_send_now', type: :job do
end
end
+ context 'for a user who has configured a slot between the earliest_time (in local time) and his current local time' do
+ let(:paris_user_daily_reminders) do
+ {
+ enabled: true,
+ times: [
+ hitting_reminder_slot_for("Paris", current_utc_time - 2.hours),
+ hitting_reminder_slot_for("Paris", current_utc_time + 3.hours)
+ ]
+ }
+ end
+ let(:scope_time) { Time.current - 2.hours }
+
+ it 'contains the user' do
+ expect(scope)
+ .to match_array([paris_user])
+ end
+ end
+
+ context 'for a user who has configured a slot before the earliest_time (in local time) and after his current local time' do
+ let(:paris_user_daily_reminders) do
+ {
+ enabled: true,
+ times: [
+ hitting_reminder_slot_for("Paris", current_utc_time - 3.hours),
+ hitting_reminder_slot_for("Paris", current_utc_time + 1.hour)
+ ]
+ }
+ end
+ let(:scope_time) { Time.current - 2.hours }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
context 'for a user whose local time is matching the configured time but without a notification' do
let(:notifications) do
nil
@@ -291,4 +328,13 @@ describe User, '.with_reminder_mail_to_send_now', type: :job do
.to be_empty
end
end
+
+ context 'when the provided scope_time is after the current time' do
+ let(:scope_time) { Time.current + 1.minute }
+
+ it 'raises an error' do
+ expect { scope }
+ .to raise_error ArgumentError
+ end
+ end
end
diff --git a/spec/workers/notifications/schedule_reminder_mails_job_spec.rb b/spec/workers/notifications/schedule_reminder_mails_job_spec.rb
index 7ebe7dff829..5d5cf8569f6 100644
--- a/spec/workers/notifications/schedule_reminder_mails_job_spec.rb
+++ b/spec/workers/notifications/schedule_reminder_mails_job_spec.rb
@@ -31,14 +31,28 @@
require 'spec_helper'
describe Notifications::ScheduleReminderMailsJob, type: :job do
- subject(:job) { described_class.perform_now }
+ subject(:job) { scheduled_job.invoke_job }
+
+ let(:scheduled_job) do
+ described_class.ensure_scheduled!
+
+ Delayed::Job.first
+ end
let(:ids) { [23, 42] }
+ let(:run_at) { scheduled_job.run_at }
before do
+ # We need to access the job as stored in the database to get at the run_at time persisted there
+ allow(ActiveJob::Base)
+ .to receive(:queue_adapter)
+ .and_return(ActiveJob::QueueAdapters::DelayedJobAdapter.new)
+
+ scheduled_job.update_column(:run_at, run_at)
+
scope = instance_double(ActiveRecord::Relation)
allow(User)
- .to receive(:having_reminder_mail_to_send_now)
+ .to receive(:having_reminder_mail_to_send)
.and_return(scope)
allow(scope)
@@ -48,20 +62,40 @@ describe Notifications::ScheduleReminderMailsJob, type: :job do
end
describe '#perform' do
- it 'schedules reminder jobs for every user with a reminder mails to be sent' do
- expect { subject }
- .to change(enqueued_jobs, :count)
- .by(2)
-
- expect(Mails::ReminderJob)
- .to have_been_enqueued
- .exactly(:once)
- .with(23)
-
- expect(Mails::ReminderJob)
- .to have_been_enqueued
- .exactly(:once)
- .with(42)
+ shared_examples_for 'schedules reminder mails' do
+ it 'schedules reminder jobs for every user with a reminder mails to be sent' do
+ expect { subject }
+ .to change(Delayed::Job, :count)
+ .by(2)
+
+ jobs = Delayed::Job.all.map do |job|
+ YAML.safe_load(job.handler, permitted_classes: [ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper])
+ end
+
+ reminder_jobs = jobs.select { |job| job.job_data['job_class'] == "Mails::ReminderJob" }
+
+ expect(reminder_jobs[0].job_data['arguments'])
+ .to match_array([23])
+
+ expect(reminder_jobs[1].job_data['arguments'])
+ .to match_array([42])
+ end
+
+ it 'queries with the intended job execution time (which might have been missed due to high load)' do
+ subject
+
+ expect(User)
+ .to have_received(:having_reminder_mail_to_send)
+ .with(run_at)
+ end
+ end
+
+ it_behaves_like 'schedules reminder mails'
+
+ context 'with a job that missed some runs' do
+ let(:run_at) { scheduled_job.run_at - 3.hours }
+
+ it_behaves_like 'schedules reminder mails'
end
end
end