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
Schedule reminder mail jobs
Wieland Lindenthal00699c8d47ec
diff --git a/app/workers/cron/schedule_reminder_mails_job.rb b/app/workers/cron/schedule_reminder_mails_job.rbnew file mode 100644index 00000000000..5a3a45ccda3--- /dev/null+++ b/app/workers/cron/schedule_reminder_mails_job.rb@@ -0,0 +1,113 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++module Cron+ class ScheduleReminderMailsJob < CronJob+ # runs every 30min step, so 00:00, 00:30, 01:00, 01:30...+ self.cron_expression = '/30 * * * *'++ def perform+ subscribers_of_current_slot_having_notifications do |subscriber_ids|+ subscriber_ids.each do |recipient_id|+ Mails::ReminderJob.perform_later(recipient_id)+ end+ end+ end++ private++ def subscribers_of_current_slot_having_notifications+ # 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)+ .where(where_statement)++ subscriber_ids = Notification+ .unsent_reminders_before(recipient: recipient_candidates, time: Time.current)+ .group(:recipient_id)+ .pluck(:recipient_id)++ yield(subscriber_ids)+ end++ def where_statement+ current_timestamp_utc = Time.current.getutc++ age = age_statement(current_timestamp_utc)+ <<-SQL.squish+ #{age} < make_interval(mins=>30)+ AND+ #{age} >= make_interval(mins=>0)+ SQL+ end++ # Creates a SQL snippet for calculating the time between now and+ # the reminder time slot with the each user's time zone+ # @param [Time] current_timestamp_utc+ def age_statement(current_timestamp_utc)+ year = current_timestamp_utc.year+ month = current_timestamp_utc.month+ day = current_timestamp_utc.day++ case_statement = case_statement_zone_name_to_offset++ slot_time = Time.zone.parse(Setting.notification_email_digest_time)++ <<-SQL.squish+ age(+ now(),+ make_timestamptz(#{year}, #{month}, #{day}, #{slot_time.hour}, #{slot_time.min}, 0, (#{case_statement}))+ )+ SQL+ end++ def case_statement_zone_name_to_offset+ return @case_statement_zone_name_to_offset if @case_statement_zone_name_to_offset++ current_time = Time.current+ statement = ActiveSupport::TimeZone.all.map do |zone|+ offset = current_offset(zone, current_time)+ "WHEN user_preferences.settings->>'time_zone' = '#{zone.name.gsub("'") { "''" }}' THEN '#{offset}'"+ end+ @case_statement_zone_name_to_offset = "CASE\n#{statement.join("\n")}\nELSE '+00:00'\nEND"+ end++ # The real offset of a time zone depends of the moment we ask. Winter and summer time+ # have different offsets of UTC. For instance, time zone "Berlin" in winter has +01:00+ # and in summer +02:00+ # @param [Time] time+ # @param [TimeZone] zone+ # @return [String]+ def current_offset(zone, time = Time.current)+ time.in_time_zone(zone).formatted_offset+ end+ end+enddiff --git a/spec/features/notifications/digest_mail_spec.rb b/spec/features/notifications/digest_mail_spec.rbindex 50ac533594a..22a13c60850 100644--- a/spec/features/notifications/digest_mail_spec.rb+++ b/spec/features/notifications/digest_mail_spec.rb@@ -49,6 +49,9 @@ describe "Digest email", type: :feature, js: true doendbefore do+ allow(Setting).to receive(:notification_email_digest_time)+ .and_return(hitting_reminder_slot_for(current_user))+watched_work_packagework_packageinvolved_work_package@@ -121,12 +124,9 @@ describe "Digest email", type: :feature, js: true doinvolved_work_package.save!end- # Have to explicitly execute the delayed jobs. If we were to execute all- # by wrapping the above, work package altering code, inside a- # perform_enqueued_jobs block, the digest job would be executed right away- # so that the second update would trigger a new digest. But we want to test- # that only one digest is sent out- 5.times { perform_enqueued_jobs }+ Cron::ScheduleReminderMailsJob.perform_later+ perform_enqueued_jobs+ perform_enqueued_jobsexpect(ActionMailer::Base.deliveries.length).to be 1diff --git a/spec/support/schedule_reminder_mails.rb b/spec/support/schedule_reminder_mails.rbnew file mode 100644index 00000000000..b94f904beed--- /dev/null+++ b/spec/support/schedule_reminder_mails.rb@@ -0,0 +1,34 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++##+# Calculates a slot in the user's local time that hits for scheduling reminder mail jobs+def hitting_reminder_slot_for(user, current_utc_time = Time.current.getutc)+ local_time = current_utc_time.in_time_zone(user.time_zone)+ "#{'%02d' % local_time.hour}:#{local_time.min <= 30 ? '00' : '30'}"+enddiff --git a/spec/workers/cron/schedule_reminder_mails_job_spec.rb b/spec/workers/cron/schedule_reminder_mails_job_spec.rbnew file mode 100644index 00000000000..4da5dd5059c--- /dev/null+++ b/spec/workers/cron/schedule_reminder_mails_job_spec.rb@@ -0,0 +1,167 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe Cron::ScheduleReminderMailsJob, type: :job do+ # As it is hard to mock Postgres'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(:slot_time) { hitting_reminder_slot_for(hitting_user, current_utc_time) } # ie. "08:00", "08:30"++ let(:hitting_user) { paris_user }+ let(:paris_user) { FactoryBot.create(:user, preferences: { time_zone: "Paris" }) } # time_zone in winter is +01:00+ let(:no_zone_user) { FactoryBot.create(:user) } # time_zone is nil+ let(:notifications) { FactoryBot.create(:notification, recipient: hitting_user) }++ subject(:perform_job) do+ described_class.new.perform+ end++ before do+ allow(Setting).to receive(:notification_email_digest_time).and_return(slot_time)+ notifications+ end++ context 'with paris_user as hitting_user' do+ let(:moscow_user) { FactoryBot.create(:user, preferences: { time_zone: "Moscow" }) } # time_zone all year is +03:00+ let(:greenland_user) { FactoryBot.create(:user, preferences: { time_zone: "Greenland" }) } # time_zone in winter is -03:00+ let(:notifications) do+ FactoryBot.create(:notification, recipient: hitting_user, created_at: 5.minutes.ago)+ FactoryBot.create(:notification, recipient: moscow_user, created_at: 5.minutes.ago)+ FactoryBot.create(:notification, recipient: greenland_user, created_at: 5.minutes.ago)+ FactoryBot.create(:notification, recipient: no_zone_user, created_at: 5.minutes.ago)+ end++ before do+ allow(Time).to receive(:current).and_return(current_utc_time)+ end++ it 'schedules ReminderMailJobs for all users that subscribed for that slot in their local time' do+ # `hitting_user` is `paris_user`.+ # `slot_time` (expressed string in local time) is set to be at the beginning of the first or second half hour+ # block of the started hour.+ expect { perform_job }+ .to enqueue_job(Mails::ReminderJob)+ .with(hitting_user.id)++ # `moscow_user` is in a different time zone (higher offset than Paris) so should not hit for the same `slot_time`+ expect { perform_job }+ .not_to enqueue_job(Mails::ReminderJob)+ .with(moscow_user.id)++ # `greenland_user` is in a different time (lower offset than Paris) so should not hit for the same `slot_time`+ expect { perform_job }+ .not_to enqueue_job(Mails::ReminderJob)+ .with(greenland_user.id)++ # `no_zone_user` should fall back to UTC time zone and thus have lower offset as `paris_user` and not hit+ expect { perform_job }+ .not_to enqueue_job(Mails::ReminderJob)+ .with(no_zone_user.id)+ end+ end++ context 'when slot_time in UTC' do+ let(:hitting_user) { no_zone_user }+ let(:notifications) do+ FactoryBot.create(:notification, recipient: hitting_user, created_at: 5.minutes.ago)+ end++ it 'schedules a job for users without timezone set' do+ expect { perform_job }+ .to enqueue_job(Mails::ReminderJob)+ .with(hitting_user.id)+ end+ end++ context 'when hitting user is not active' do+ let(:hitting_user) do+ paris_user.locked!+ paris_user+ end+ let(:notifications) do+ FactoryBot.create(:notification, recipient: hitting_user)+ end++ it 'does not schedule for users that are not active' do+ expect { perform_job }+ .not_to enqueue_job(Mails::ReminderJob)+ .with(hitting_user.id)+ end+ end++ context 'with a user without notifications' do+ # Create another user just as `hitting_user` but without notifications+ let(:paris_user_without_notifications) { FactoryBot.create(:user, preferences: { time_zone: "Paris" }) }++ it 'does not schedule reminder mail job' do+ expect { perform_job }+ .not_to enqueue_job(Mails::ReminderJob)+ .with(paris_user_without_notifications.id)+ end+ end++ context 'with a user with read IAN notifications' do+ # Create another user just as `hitting_user` but without notifications+ let(:paris_user_with_read_ian_notifications) do+ FactoryBot.create(:user, preferences: { time_zone: "Paris" })+ end+ let(:notifications) do+ FactoryBot.create(:notification,+ recipient: paris_user_with_read_ian_notifications,+ read_ian: true)+ end++ it 'does not schedule reminder mail job' do+ expect { perform_job }+ .not_to enqueue_job(Mails::ReminderJob)+ .with(paris_user_with_read_ian_notifications.id)+ end+ end++ context 'with a user with who already received a reminder for a notification' do+ # Create another user just as `hitting_user` but without notifications+ let(:paris_user_with_read_mail_digest_notifications) do+ FactoryBot.create(:user, preferences: { time_zone: "Paris" })+ end+ let(:notifications) do+ FactoryBot.create(:notification,+ recipient: paris_user_with_read_mail_digest_notifications,+ read_mail_digest: true)+ end++ it 'does not schedule reminder mail job' do+ expect { perform_job }+ .not_to enqueue_job(Mails::ReminderJob)+ .with(paris_user_with_read_mail_digest_notifications.id)+ end+ end+end