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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
apply cron_at based interval determination to ScheduleReminderJob
ulfertsc5749f4929b1
diff --git a/app/models/users/scopes/having_reminder_mail_to_send.rb b/app/models/users/scopes/having_reminder_mail_to_send.rbindex 35ffcaede62..24d03c4ca0b 100644--- a/app/models/users/scopes/having_reminder_mail_to_send.rb+++ b/app/models/users/scopes/having_reminder_mail_to_send.rb@@ -31,13 +31,13 @@ module Users::Scopesextend ActiveSupport::Concernclass_methods do- # Returns all users for which a reminder mails should be sent now. A user+ # Returns all users for which a reminder mails should be sent. A user# will be included if:#- # * That user has an unread notification+ # * That user has an unread notification that is not older than the latest_time value.# * The user hasn't been informed about the unread notification before# * The user has configured reminder mails to be within the time frame- # between the provided time and now.+ # between the provided earliest_time and latest_time.## This assumes that users only have full hours specified for the times# they desire to receive a reminder mail at.@@ -48,9 +48,14 @@ module Users::Scopes# Only the time part is used which is moved forward to the next quarter# hour (e.g. 2021-05-03 10:34:12+02:00 -> 08:45:00). This is done# because time zones always have a mod(15) == 0 minutes offset. Needs to- # be before the current time.- def having_reminder_mail_to_send(earliest_time)- local_times = local_times_from(earliest_time)+ # be before the latest_time.+ # @param [DateTime] latest_time The latest time to consider as a matching+ # slot.+ #+ # Only the time part is used which is moved back to the last quarter hour+ # less than the latest_time value.+ def having_reminder_mail_to_send(earliest_time, latest_time)+ local_times = local_times_from(earliest_time, latest_time)return none if local_times.empty?@@ -62,13 +67,15 @@ module Users::Scopes.joins(local_time_join(local_times))subscriber_ids = Notification- .unsent_reminders_before(recipient: recipient_candidates, time: Time.current)+ .unsent_reminders_before(recipient: recipient_candidates, time: latest_time).group(:recipient_id).select(:recipient_id)where(id: subscriber_ids)end+ private+def local_time_join(local_times)# Joins the times local to the user preferences and then checks whether:#@@ -129,8 +136,8 @@ module Users::ScopesSQLend- def local_times_from(earliest_time)- times = quarters_between_earliest_and_now(earliest_time)+ def local_times_from(earliest_time, latest_time)+ times = quarters_between_earliest_and_latest(earliest_time, latest_time)times_for_zones(times)end@@ -167,20 +174,20 @@ module Users::Scopesendend- def quarters_between_earliest_and_now(earliest_time)- latest_time = Time.current+ def quarters_between_earliest_and_latest(earliest_time, latest_time) # rubocop:disable Metrics/AbcSizeraise ArgumentError if latest_time < earliest_time || (latest_time - earliest_time) > 1.day- quarters = ((latest_time - earliest_time) / 60 / 15).floor+ # The first quarter is equal or greater to the earliest time+ first_quarter = earliest_time.change(min: (earliest_time.min.to_f / 15).ceil * 15)+ # The last quarter is the one smaller than the latest time. But needs to be at least equal to the first quarter.+ last_quarter = [first_quarter, latest_time.change(min: latest_time.min / 15 * 15)].max- (1..quarters).each_with_object([next_quarter_hour(earliest_time)]) do |_, times|- times << (times.last + 15.minutes)+ (first_quarter.to_i..last_quarter.to_i)+ .step(15.minutes)+ .map do |time|+ Time.zone.at(time)endend-- def next_quarter_hour(time)- (time + (time.min % 15 == 0 ? 0.minutes : (15 - (time.min % 15)).minutes))- endendendenddiff --git a/app/workers/cron/quarter_hour_schedule_job.rb b/app/workers/cron/quarter_hour_schedule_job.rbnew file mode 100644index 00000000000..822b9cae29d--- /dev/null+++ b/app/workers/cron/quarter_hour_schedule_job.rb@@ -0,0 +1,68 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++module Cron::QuarterHourScheduleJob+ extend ActiveSupport::Concern++ included do+ include GoodJob::ActiveJobExtensions::Concurrency++ # With good_job and the limit of only allowing a single job to be enqueued and+ # also a single job being performed at the same time we end up having+ # up to two jobs that are not yet finished.++ good_job_control_concurrency_with(+ enqueue_limit: 1,+ perform_limit: 1+ )+ end++ private++ def upper_boundary+ @upper_boundary ||= GoodJob::Job+ .find(job_id)+ .cron_at+ end++ def lower_boundary+ @lower_boundary ||= begin+ predecessor = GoodJob::Job+ .succeeded+ .where(job_class: self.class.name)+ .order(cron_at: :desc)+ .first++ if predecessor+ predecessor.cron_at + 15.minutes+ else+ upper_boundary+ end+ end+ end+enddiff --git a/app/workers/notifications/schedule_date_alerts_notifications_job.rb b/app/workers/notifications/schedule_date_alerts_notifications_job.rbindex 922fb59e21b..a2cd750e60d 100644--- a/app/workers/notifications/schedule_date_alerts_notifications_job.rb+++ b/app/workers/notifications/schedule_date_alerts_notifications_job.rb@@ -30,7 +30,7 @@ module Notifications# Creates date alert jobs for users whose local time is 1:00 am.# The job is scheduled to run every 15 minutes.class ScheduleDateAlertsNotificationsJob < ApplicationJob- include GoodJob::ActiveJobExtensions::Concurrency+ include Cron::QuarterHourScheduleJobdef performreturn unless EnterpriseToken.allows_to?(:date_alerts)@@ -38,16 +38,7 @@ module NotificationsService.new(every_quater_hour_between_predecessor_cron_at_and_own_cron_at).callend- # With good_job and the limit of only allowing a single job to be enqueued and- # also a single job being performed at the same time we end up having- # up to two jobs that are not yet finished.-- good_job_control_concurrency_with(- enqueue_limit: 1,- perform_limit: 1- )-- # What cannot be controlled however is the time at which a job is actually performed.+ # What cannot be controlled is the time at which a job is actually performed.# A high load on the system can lead to a job being performed later than expected.## It might also happen that due to some outage of the background workers, cron@@ -86,25 +77,5 @@ module NotificationsTime.zone.at(time)endend-- def upper_boundary- GoodJob::Job- .find(job_id)- .cron_at- end-- def lower_boundary- predecessor = GoodJob::Job- .succeeded- .where(job_class: self.class.name)- .order(cron_at: :desc)- .first-- if predecessor- predecessor.cron_at + 15.minutes- else- upper_boundary- end- endendenddiff --git a/app/workers/notifications/schedule_reminder_mails_job.rb b/app/workers/notifications/schedule_reminder_mails_job.rbindex 969f04c1551..3a393cc7f29 100644--- a/app/workers/notifications/schedule_reminder_mails_job.rb+++ b/app/workers/notifications/schedule_reminder_mails_job.rb@@ -28,8 +28,12 @@module Notificationsclass ScheduleReminderMailsJob < ApplicationJob+ include Cron::QuarterHourScheduleJob+def perform- User.having_reminder_mail_to_send(job_scheduled_at)+ return unless lower_boundary.present? && upper_boundary.present?++ User.having_reminder_mail_to_send(lower_boundary, upper_boundary).pluck(:id).each do |user_id|Mails::ReminderJob.perform_later(user_id)diff --git a/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rbindex 6f183e42e40..f3ae01d9e53 100644--- a/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb+++ b/spec/models/users/scopes/having_reminder_mail_to_send_spec.rb@@ -30,20 +30,13 @@ require "spec_helper"RSpec.describe User, ".having_reminder_mail_to_send" dosubject(:scope) do- described_class.having_reminder_mail_to_send(scope_time)- end-- # Fix the time of the specs to ensure a consistent run- around do |example|- Timecop.travel(current_time) do- example.run- end+ described_class.having_reminder_mail_to_send(scope_lower_boundary_time, scope_upper_boundary_time)end# Let the date be one where workdays are enabled by default# to avoid specifying them explicitly- let(:current_time) { "2021-09-30T08:10:59Z".to_datetime }- let(:scope_time) { "2021-09-30T08:00:00Z".to_datetime }+ let(:scope_upper_boundary_time) { "2021-09-30T08:10:59Z".to_datetime }+ let(:scope_lower_boundary_time) { "2021-09-30T08:00:00Z".to_datetime }let(:paris_user) docreate(@@ -66,10 +59,14 @@ RSpec.describe User, ".having_reminder_mail_to_send" dolet(:paris_user_daily_reminders) do{enabled: true,- times: [hitting_reminder_slot_for("Europe/Paris", current_time)]+ times: [hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time)]}end- let(:notifications) { create(:notification, recipient: paris_user, created_at: 5.minutes.ago) }+ let(:notifications) do+ create(:notification,+ recipient: paris_user,+ created_at: scope_upper_boundary_time - 5.minutes)+ endlet(:users) { [paris_user] }before do@@ -140,8 +137,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendcontext "for a user whose local time is not matching the configured time" do- let(:current_time) { "2021-09-30T08:20:59Z".to_datetime }- let(:scope_time) { "2021-09-30T08:15:00Z".to_datetime }+ let(:scope_upper_boundary_time) { "2021-09-30T08:20:59Z".to_datetime }+ let(:scope_lower_boundary_time) { "2021-09-30T08:15:00Z".to_datetime }it "is empty" doexpect(scope)@@ -160,7 +157,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" doworkdays: hawaii_user_workdays,daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Pacific/Honolulu", current_time)]+ times: [hitting_reminder_slot_for("Pacific/Honolulu", scope_upper_boundary_time)]},pause_reminders: hawaii_user_pause_reminders}@@ -172,7 +169,9 @@ RSpec.describe User, ".having_reminder_mail_to_send" do}endlet(:notifications) do- create(:notification, recipient: hawaii_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: hawaii_user,+ created_at: scope_upper_boundary_time - 5.minutes)endlet(:users) { [hawaii_user] }let(:hawaii_user_workdays) { paris_user_workdays }@@ -191,7 +190,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "with local date range for pausing that includes scope_time" do+ context "with local date range for pausing that includes the boundary time" dolet(:hawaii_user_pause_reminders) do{enabled: true,@@ -206,7 +205,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "with local date range for pausing that excludes scope_time" do+ context "with local date range for pausing that excludes the boundary time" dolet(:hawaii_user_pause_reminders) do{enabled: true,@@ -224,8 +223,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" docontext "for a user whose local time is on the next workday" do# 12:00 thursday Etc/UTC = 03:00 friday @ Pacific/Apia- let(:current_time) { "2021-09-30T12:05:59Z".to_datetime }- let(:scope_time) { "2021-09-30T12:00:00Z".to_datetime }+ let(:scope_upper_boundary_time) { "2021-09-30T12:05:59Z".to_datetime }+ let(:scope_lower_boundary_time) { "2021-09-30T12:00:00Z".to_datetime }let(:samoa_user) docreate(@@ -236,13 +235,15 @@ RSpec.describe User, ".having_reminder_mail_to_send" doworkdays: samoa_user_workdays,daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Pacific/Apia", current_time)]+ times: [hitting_reminder_slot_for("Pacific/Apia", scope_upper_boundary_time)]}})endlet(:notifications) do- create(:notification, recipient: samoa_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: samoa_user,+ created_at: scope_upper_boundary_time - 5.minutes)endlet(:users) { [samoa_user] }let(:samoa_user_workdays) { paris_user_workdays }@@ -271,13 +272,15 @@ RSpec.describe User, ".having_reminder_mail_to_send" dotime_zone: "Europe/Moscow",daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Europe/Moscow", current_time)]+ times: [hitting_reminder_slot_for("Europe/Moscow", scope_upper_boundary_time)]}})endlet(:notifications) do- create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: moscow_user,+ created_at: scope_upper_boundary_time - 5.minutes)endlet(:users) { [moscow_user] }@@ -292,9 +295,9 @@ RSpec.describe User, ".having_reminder_mail_to_send" do{enabled: true,times: [- hitting_reminder_slot_for("Europe/Paris", current_time - 3.hours),- hitting_reminder_slot_for("Europe/Paris", current_time),- hitting_reminder_slot_for("Europe/Paris", current_time + 3.hours)+ hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time - 3.hours),+ hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time),+ hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time + 3.hours)]}end@@ -305,17 +308,17 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "for a user who has configured a slot between the earliest_time (in local time) and his current local time" do+ context "for a user who has configured a slot between the earliest_time (in local time) and latest_time (in local time)" dolet(:paris_user_daily_reminders) do{enabled: true,times: [- hitting_reminder_slot_for("Europe/Paris", current_time - 2.hours),- hitting_reminder_slot_for("Europe/Paris", current_time + 3.hours)+ hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time - 2.hours),+ hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time + 3.hours)]}end- let(:scope_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("06:00") }+ let(:scope_lower_boundary_time) { scope_upper_boundary_time - 3.hours }it "contains the user" doexpect(scope)@@ -323,17 +326,17 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "for a user who has configured a slot before the earliest_time (in local time) and after his current local time" do+ context "for a user who has configured a slot before the earliest_time (in local time) and after latest_time (in local time)" dolet(:paris_user_daily_reminders) do{enabled: true,times: [- hitting_reminder_slot_for("Europe/Paris", current_time - 3.hours),- hitting_reminder_slot_for("Europe/Paris", current_time + 1.hour)+ hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time - 3.hours),+ hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time + 1.hour)]}end- let(:scope_time) { current_time - 2.hours }+ let(:scope_lower_boundary_time) { scope_upper_boundary_time - 2.hours }it "is empty" doexpect(scope)@@ -356,7 +359,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" dolet(:paris_user_daily_reminders) do{enabled: false,- times: [hitting_reminder_slot_for("Europe/Paris", current_time)]+ times: [hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time)]}end@@ -376,8 +379,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" do})end- let(:current_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T08:09").utc }- let(:scope_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T08:00") }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T08:09").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T08:00") }it "contains the user" doexpect(scope)@@ -395,8 +398,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" do})end- let(:current_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T10:00").utc }- let(:scope_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T10:00") }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T10:00").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Europe/Paris"].parse("2021-09-30T10:00") }it "is empty" doexpect(scope)@@ -404,7 +407,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "for a user who is in a 45 min time zone and having reminder set to 8:00 and being executed at 8:10" do+ context "for a user who is in a 45 min time zone and having reminder set to 8:00 and with the boundaries matching" dolet(:kathmandu_user) docreate(:user,@@ -413,15 +416,17 @@ RSpec.describe User, ".having_reminder_mail_to_send" dotime_zone: "Asia/Kathmandu",daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Asia/Kathmandu", current_time)]+ times: [hitting_reminder_slot_for("Asia/Kathmandu", scope_upper_boundary_time)]}})end- let(:current_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:10").utc }- let(:scope_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:00").utc }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:10").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:00").utc }let(:notifications) do- create(:notification, recipient: kathmandu_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: kathmandu_user,+ created_at: scope_upper_boundary_time - 5.minutes)endlet(:users) { [kathmandu_user] }@@ -432,7 +437,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "for a user who is in a 45 min time zone and having reminder set to 8:00 and being executed at 8:40" do+ context "for a user who is in a 45 min time zone and having reminder set to 8:00 and with the boundaries too high" dolet(:kathmandu_user) docreate(:user,@@ -441,15 +446,17 @@ RSpec.describe User, ".having_reminder_mail_to_send" dotime_zone: "Asia/Kathmandu",daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Asia/Kathmandu", current_time)]+ times: [hitting_reminder_slot_for("Asia/Kathmandu", scope_upper_boundary_time)]}})end- let(:current_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:40").utc }- let(:scope_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:30").utc }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:40").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T08:30").utc }let(:notifications) do- create(:notification, recipient: kathmandu_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: kathmandu_user,+ created_at: scope_upper_boundary_time - 5.minutes)endlet(:users) { [kathmandu_user] }@@ -460,7 +467,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "for a user who is in a 45 min time zone and having reminder set to 8:00 and being executed at 7:55" do+ context "for a user who is in a 45 min time zone and having reminder set to 8:00 and with the boundaries being too low" dolet(:kathmandu_user) docreate(:user,@@ -469,15 +476,17 @@ RSpec.describe User, ".having_reminder_mail_to_send" dotime_zone: "Asia/Kathmandu",daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Asia/Kathmandu", current_time)]+ times: [hitting_reminder_slot_for("Asia/Kathmandu", scope_upper_boundary_time)]}})end- let(:current_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T07:55").utc }- let(:scope_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T07:45").utc }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T07:55").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Asia/Kathmandu"].parse("2021-09-30T07:45").utc }let(:notifications) do- create(:notification, recipient: kathmandu_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: kathmandu_user,+ created_at: scope_upper_boundary_time - 5.minutes)endlet(:users) { [kathmandu_user] }@@ -490,7 +499,10 @@ RSpec.describe User, ".having_reminder_mail_to_send" docontext "for a user whose local time is matching the configured time but with an already read notification (IAN)" dolet(:notifications) do- create(:notification, recipient: paris_user, created_at: 5.minutes.ago, read_ian: true)+ create(:notification,+ recipient: paris_user,+ created_at: scope_upper_boundary_time - 5.minutes,+ read_ian: true)endit "is empty" do@@ -501,7 +513,10 @@ RSpec.describe User, ".having_reminder_mail_to_send" docontext "for a user whose local time is matching the configured time but with an already read notification (reminder)" dolet(:notifications) do- create(:notification, recipient: paris_user, created_at: 5.minutes.ago, mail_reminder_sent: true)+ create(:notification,+ recipient: paris_user,+ created_at: scope_upper_boundary_time - 5.minutes,+ mail_reminder_sent: true)endit "is empty" do@@ -512,7 +527,9 @@ RSpec.describe User, ".having_reminder_mail_to_send" docontext "for a user whose local time is matching the configured time but with the user being inactive" dolet(:notifications) do- create(:notification, recipient: paris_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: paris_user,+ created_at: scope_upper_boundary_time - 5.minutes)endbefore do@@ -525,11 +542,24 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "for a user whose local time is before the configured time" do+ context "for a user whose local time is matching the configured time but with the notification being created later" do+ let(:notifications) do+ create(:notification,+ recipient: paris_user,+ created_at: scope_upper_boundary_time + 5.minutes)+ end++ it "is empty" do+ expect(scope)+ .to be_empty+ end+ end++ context "for a user whose local time is before the boundary time" dolet(:paris_user_daily_reminders) do{enabled: true,- times: [hitting_reminder_slot_for("Europe/Paris", current_time + 1.hour)]+ times: [hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time + 1.hour)]}end@@ -539,11 +569,11 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "for a user whose local time is after the configured time" do+ context "for a user whose local time is after the boundary time" dolet(:paris_user_daily_reminders) do{enabled: true,- times: [hitting_reminder_slot_for("Europe/Paris", current_time - 1.hour)]+ times: [hitting_reminder_slot_for("Europe/Paris", scope_upper_boundary_time - 1.hour)]}end@@ -561,7 +591,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" dopreferences: {daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Etc/UTC", current_time)]+ times: [hitting_reminder_slot_for("Etc/UTC", scope_upper_boundary_time)]}})@@ -582,7 +612,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" dotime_zone: "",daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Etc/UTC", current_time)]+ times: [hitting_reminder_slot_for("Etc/UTC", scope_upper_boundary_time)]}})@@ -602,7 +632,7 @@ RSpec.describe User, ".having_reminder_mail_to_send" dopreferences: {})end- let(:current_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T08:00").utc }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T08:00").utc }it "is including the user as Etc/UTC at 08:00 is assumed" doexpect(scope)@@ -618,8 +648,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" dopreferences: {})end- let(:current_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }- let(:scope_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }it "is empty as Etc/UTC at 08:00 is assumed" doexpect(scope)@@ -636,13 +666,15 @@ RSpec.describe User, ".having_reminder_mail_to_send" dopreferences: {daily_reminders: {enabled: true,- times: [hitting_reminder_slot_for("Europe/Moscow", current_time)]+ times: [hitting_reminder_slot_for("Europe/Moscow", scope_upper_boundary_time)]}})endlet(:notifications) do- create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)+ create(:notification,+ recipient: moscow_user,+ created_at: scope_upper_boundary_time - 5.minutes)endlet(:users) { [moscow_user] }@@ -652,8 +684,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendend- context "when the provided scope_time is after the current time" do- let(:scope_time) { 1.minute.from_now }+ context "when the provided lower boundary is after the current time" do+ let(:scope_lower_boundary_time) { 1.minute.from_now }it "raises an error" doexpect { scope }@@ -662,8 +694,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendcontext "for a user without preferences at 08:00" do- let(:current_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T08:00").utc }- let(:scope_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T08:00").utc }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T08:00").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T08:00").utc }before doparis_user.pref.destroy@@ -676,8 +708,8 @@ RSpec.describe User, ".having_reminder_mail_to_send" doendcontext "for a user without preferences at 10:00" do- let(:current_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }- let(:scope_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }+ let(:scope_upper_boundary_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }+ let(:scope_lower_boundary_time) { ActiveSupport::TimeZone["Etc/UTC"].parse("2021-09-30T10:00").utc }before doparis_user.pref.destroydiff --git a/spec/workers/notifications/schedule_reminder_mails_job_spec.rb b/spec/workers/notifications/schedule_reminder_mails_job_spec.rbindex a8c3a2fcfa4..61ab0eb8704 100644--- a/spec/workers/notifications/schedule_reminder_mails_job_spec.rb+++ b/spec/workers/notifications/schedule_reminder_mails_job_spec.rb@@ -29,48 +29,92 @@require "spec_helper"RSpec.describe Notifications::ScheduleReminderMailsJob, type: :job do- let(:scheduled_job) { described_class.perform_later }+ let(:scheduled_job) do+ described_class.perform_later.tap do |job|+ set_cron_time(job, job_cron_at)+ GoodJob.perform_inline+ end+ end+ let(:job_cron_at) { Time.current.then { |t| t.change(min: t.min / 15 * 15) } }++ let(:previous_job) do+ described_class.perform_later.tap do |job|+ GoodJob.perform_inline+ set_cron_time(job, previous_job_cron_at)+ end+ end+let(:ids) { [23, 42] }before do# We need to access the job as stored in the database to get at the scheduled_at time persisted thereActiveJob::Base.disable_test_adapter- scheduled_jobscope = instance_double(ActiveRecord::Relation)allow(User).to receive(:having_reminder_mail_to_send).and_return(scope)allow(scope).to receive(:pluck).with(:id).and_return(ids)end+ def set_cron_time(job, cron_at)+ GoodJob::Job+ .where(id: job.job_id)+ .update_all(cron_at:)+ end+describe "#perform" doshared_examples_for "schedules reminder mails" do- it "schedules reminder jobs for every user with a reminder mails to be sent" do- expect { GoodJob.perform_inline }.to change(GoodJob::Job, :count).by(2)+ it "schedules reminder jobs for every user with a reminder mail to be sent" do+ expect { scheduled_job }+ .to change(GoodJob::Job.where(job_class: "Mails::ReminderJob"), :count)+ .by(2)arguments_from_both_jobs =GoodJob::Job.where(job_class: "Mails::ReminderJob")- .flat_map {|i| i.serialized_params["arguments"]}+ .flat_map { |i| i.serialized_params["arguments"] }.sortexpect(arguments_from_both_jobs).to eq(ids)endit "queries with the intended job execution time (which might have been missed due to high load)" do- GoodJob.perform_inline+ scheduled_job- expect(User).to have_received(:having_reminder_mail_to_send).with(scheduled_job.job_scheduled_at)+ expect(User)+ .to have_received(:having_reminder_mail_to_send)+ .with(expected_lower_boundary, expected_upper_boundary)endend- it_behaves_like "schedules reminder mails"+ context "when there is no predecessor job" do+ it_behaves_like "schedules reminder mails" do+ let(:expected_lower_boundary) { job_cron_at }+ let(:expected_upper_boundary) { job_cron_at }+ end+ end++ context "when there is a predecessor job with a cron_at 15 min before" do+ let(:previous_job_cron_at) { job_cron_at - 15.minutes }- context "with a job that missed some runs" dobefore do- GoodJob::Job- .where(id: scheduled_job.job_id)- .update_all(scheduled_at: scheduled_job.job_scheduled_at - 3.hours)+ previous_job+ end++ it_behaves_like "schedules reminder mails" do+ let(:expected_lower_boundary) { previous_job_cron_at + 15.minutes }+ let(:expected_upper_boundary) { job_cron_at }end+ end++ context "when there is a predecessor job with a cron_at 2 hours before" do+ let(:previous_job_cron_at) { job_cron_at - 2.hours }- it_behaves_like "schedules reminder mails"+ before do+ previous_job+ end++ it_behaves_like "schedules reminder mails" do+ let(:expected_lower_boundary) { previous_job_cron_at + 15.minutes }+ let(:expected_upper_boundary) { job_cron_at }+ endendendend