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

take non hour time zone offsets into account

ulferts4cd9dac8dd5c
diff --git a/app/models/users/scopes/having_reminder_mail_to_send.rb b/app/models/users/scopes/having_reminder_mail_to_send.rb
index a64b0ff78e8..7ff5c829fa4 100644
--- a/app/models/users/scopes/having_reminder_mail_to_send.rb
+++ b/app/models/users/scopes/having_reminder_mail_to_send.rb
@@ -36,20 +36,25 @@ module Users::Scopes
# Returns all users for which a reminder mails should be sent now. A user will be included if:
# * That user has an unread notification
# * The user hasn't been informed about the unread notification before
- # * The user has configured reminder mails to be sent now.
+ # * The user has configured reminder mails to be within the time frame between the provided time and now.
# This assumes that users only have full hours specified for the times they desire
# to receive a reminder mail at.
- # @param [DateTime] earliest_time The earliest time to consider as a matching slot. All full hours from that time
+ # @param [DateTime] earliest_time The earliest time to consider as a matching slot. All quarter 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).
+ # 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)
+
+ return none if local_times.empty?
+
# 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(earliest_time))
+ .joins(local_time_join(local_times))
subscriber_ids = Notification
.unsent_reminders_before(recipient: recipient_candidates, time: Time.current)
@@ -59,14 +64,16 @@ module Users::Scopes
where(id: subscriber_ids)
end
- def local_time_join(earliest_time)
+ def local_time_join(local_times)
# 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(earliest_time)}) AS local_times
+ JOIN (
+ SELECT * FROM #{arel_table.grouping(Arel::Nodes::ValuesList.new(local_times)).as('t(time, zone)').to_sql}
+ ) AS local_times
ON COALESCE(user_preferences.settings->>'time_zone', 'UTC') = local_times.zone
AND (
(
@@ -82,12 +89,10 @@ module Users::Scopes
SQL
end
- def local_time_table(earliest_time)
- times = hours_between_earliest_and_now(earliest_time)
+ def local_times_from(earliest_time)
+ times = quarters_between_earliest_and_now(earliest_time)
- values_list = Arel::Nodes::ValuesList.new(times_for_zones(times))
-
- "SELECT * FROM #{arel_table.grouping(values_list).as('t(time, zone)').to_sql}"
+ times_for_zones(times)
end
def times_for_zones(times)
@@ -95,19 +100,33 @@ module Users::Scopes
.all
.map do |z|
times.map do |time|
- [time.in_time_zone(z).strftime('%H:00:00+00:00'), z.name.gsub("'", "''")]
+ local_time = time.in_time_zone(z)
+
+ # Since only full hours can be configured, we can disregard any local time that is not
+ # a full hour.
+ next if local_time.min != 0
+
+ [local_time.strftime('%H:00:00+00:00'), z.name.gsub("'", "''")]
end
end
.flatten(1)
+ .compact
end
- def hours_between_earliest_and_now(earliest_time)
- raise ArgumentError if Time.current - earliest_time < 0
+ def quarters_between_earliest_and_now(earliest_time)
+ latest_time = Time.current
+ raise ArgumentError if latest_time < earliest_time || (latest_time - earliest_time) > 1.day
+
+ quarters = ((latest_time - earliest_time) / 60 / 15).floor
- (0..[((Time.current - earliest_time) / 1.hour).round, 24].min).map do |i|
- (earliest_time + i.hours).utc
+ (1..quarters).each_with_object([next_quarter_hour(earliest_time)]) do |i, times|
+ times << times.last + i * 15.minutes
end
end
+
+ def next_quarter_hour(time)
+ (time + (time.min % 15 == 0 ? 0.minutes : (15 - time.min % 15).minutes))
+ 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 0307edda806..bc0f0e45308 100644
--- a/app/workers/notifications/schedule_reminder_mails_job.rb
+++ b/app/workers/notifications/schedule_reminder_mails_job.rb
@@ -30,8 +30,8 @@
module Notifications
class ScheduleReminderMailsJob < Cron::CronJob
- # runs every hour, so 00:00, 01:00...
- self.cron_expression = '0 * * * *'
+ # runs every quarter of an hour, so 00:00, 00:15...
+ self.cron_expression = '*/15 * * * *'
def perform
User.having_reminder_mail_to_send(run_at).pluck(:id).each do |user_id|
diff --git a/spec/features/notifications/reminder_mail_spec.rb b/spec/features/notifications/reminder_mail_spec.rb
index 51e9e38d7f3..b34dfb59a87 100644
--- a/spec/features/notifications/reminder_mail_spec.rb
+++ b/spec/features/notifications/reminder_mail_spec.rb
@@ -71,7 +71,13 @@ describe "Reminder email", type: :feature, js: true do
let(:work_package) { FactoryBot.create(:work_package, project: project) }
let(:watched_work_package) { FactoryBot.create(:work_package, project: project, watcher_users: [current_user]) }
let(:involved_work_package) { FactoryBot.create(:work_package, project: project, assigned_to: current_user) }
- let(:current_utc_time) { ActiveSupport::TimeZone['Hawaii'].parse("08:43").utc }
+ # The run_at time of the delayed job used for scheduling the reminder mails
+ # needs to be within a time frame eligible for sending out mails for the chose
+ # time zone. For the time zone Hawaii (UTC-10) this means between 8:00:00 and 8:14:59 UTC.
+ # The job is scheduled to run every 15 min so the run_at will in production always move between the quarters of an hour.
+ # The current time can be way behind that.
+ let(:current_utc_time) { ActiveSupport::TimeZone['Hawaii'].parse("08:34:10").utc }
+ let(:job_run_at) { ActiveSupport::TimeZone['Hawaii'].parse("08:00").utc }
current_user do
FactoryBot.create(
@@ -112,7 +118,7 @@ describe "Reminder email", type: :feature, js: true do
# so we have to mock it.
allow(Notifications::ScheduleReminderMailsJob)
.to receive(:delayed_job)
- .and_return(instance_double(Delayed::Backend::ActiveRecord::Job, run_at: Time.current.utc))
+ .and_return(instance_double(Delayed::Backend::ActiveRecord::Job, run_at: job_run_at))
end
it 'sends a digest mail based on the configuration', with_settings: { journal_aggregation_time_minutes: 0 } do
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.rb
index 3fdfc76e0bc..ceb106b5ee2 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
@@ -37,8 +37,8 @@ describe User, '.having_reminder_mail_to_send', type: :model do
# 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(:current_utc_time) { ActiveSupport::TimeZone['UTC'].parse("08:10:59") }
+ let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("08:00") }
let(:paris_user) do
FactoryBot.create(
@@ -72,6 +72,16 @@ describe User, '.having_reminder_mail_to_send', type: :model do
end
end
+ context 'for a user whose local time is not matching the configured time' do
+ let(:current_utc_time) { ActiveSupport::TimeZone['UTC'].parse("08:20:59") }
+ let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("08:15") }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
context 'for a user whose local time is matching the configured time (in a non CET time zone)' do
let(:moscow_user) do
FactoryBot.create(
@@ -125,7 +135,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
]
}
end
- let(:scope_time) { Time.current - 2.hours }
+ let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("06:00") }
it 'contains the user' do
expect(scope)
@@ -186,7 +196,8 @@ describe User, '.having_reminder_mail_to_send', type: :model do
}
)
end
- let(:current_utc_time) { ActiveSupport::TimeZone['Paris'].parse("08:00").utc }
+ let(:current_utc_time) { ActiveSupport::TimeZone['Paris'].parse("08:09").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Paris'].parse("08:00") }
it 'contains the user' do
expect(scope)
@@ -205,6 +216,91 @@ describe User, '.having_reminder_mail_to_send', type: :model do
)
end
let(:current_utc_time) { ActiveSupport::TimeZone['Paris'].parse("10:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Paris'].parse("10:00") }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
+ 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
+ let(:kathmandu_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Kathmandu',
+ preferences: {
+ time_zone: "Kathmandu",
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Asia/Kathmandu", current_utc_time)]
+ }
+ }
+ )
+ end
+ let(:current_utc_time) { ActiveSupport::TimeZone['Asia/Kathmandu'].parse("08:10").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Asia/Kathmandu'].parse("08:00").utc }
+ let(:notifications) do
+ FactoryBot.create(:notification, recipient: kathmandu_user, created_at: 5.minutes.ago)
+ end
+
+ let(:users) { [kathmandu_user] }
+
+ it 'contains the user' do
+ expect(scope)
+ .to match_array([kathmandu_user])
+ end
+ end
+
+ 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
+ let(:kathmandu_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Kathmandu',
+ preferences: {
+ time_zone: "Kathmandu",
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Asia/Kathmandu", current_utc_time)]
+ }
+ }
+ )
+ end
+ let(:current_utc_time) { ActiveSupport::TimeZone['Asia/Kathmandu'].parse("08:40").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Asia/Kathmandu'].parse("08:30").utc }
+ let(:notifications) do
+ FactoryBot.create(:notification, recipient: kathmandu_user, created_at: 5.minutes.ago)
+ end
+
+ let(:users) { [kathmandu_user] }
+
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
+ 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
+ let(:kathmandu_user) do
+ FactoryBot.create(
+ :user,
+ firstname: 'Kathmandu',
+ preferences: {
+ time_zone: "Kathmandu",
+ daily_reminders: {
+ enabled: true,
+ times: [hitting_reminder_slot_for("Asia/Kathmandu", current_utc_time)]
+ }
+ }
+ )
+ end
+ let(:current_utc_time) { ActiveSupport::TimeZone['Asia/Kathmandu'].parse("07:55").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['Asia/Kathmandu'].parse("07:45").utc }
+ let(:notifications) do
+ FactoryBot.create(:notification, recipient: kathmandu_user, created_at: 5.minutes.ago)
+ end
+
+ let(:users) { [kathmandu_user] }
it 'is empty' do
expect(scope)
@@ -322,6 +418,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
)
end
let(:current_utc_time) { ActiveSupport::TimeZone['UTC'].parse("10:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("10:00").utc }
it 'is empty as UTC at 08:00 is assumed' do
expect(scope)
@@ -340,6 +437,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
context 'for a user without preferences at 08:00' do
let(:current_utc_time) { ActiveSupport::TimeZone['UTC'].parse("08:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("08:00").utc }
before do
paris_user.pref.destroy
@@ -353,6 +451,7 @@ describe User, '.having_reminder_mail_to_send', type: :model do
context 'for a user without preferences at 10:00' do
let(:current_utc_time) { ActiveSupport::TimeZone['UTC'].parse("10:00").utc }
+ let(:scope_time) { ActiveSupport::TimeZone['UTC'].parse("10:00").utc }
before do
paris_user.pref.destroy