From b47e99604d9e0f4ee4bc3299ae4e6f8fae8ade58 Mon Sep 17 00:00:00 2001 From: "Howard M. Miller" <3620291+h-m-m@users.noreply.github.com> Date: Tue, 11 Jun 2024 19:11:50 -0400 Subject: [PATCH 1/2] Schedule weekly reports to run Monday for the previous week Our jobs use the default Rails definition of a week starting on a Monday. I think this is the ISO standard. Running a job on Monday and giving it the argument `Time.zone.yesterday` means that weekly jobs will process data for the previous week changelog: Internal, Reporting, Move weekly reports to run at 00:00 Monday UTC --- config/initializers/job_configurations.rb | 16 +++++++-------- spec/jobs/job_configurations_spec.rb | 24 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 spec/jobs/job_configurations_spec.rb diff --git a/config/initializers/job_configurations.rb b/config/initializers/job_configurations.rb index c15b6af5f9d..e1e7e7fb71b 100644 --- a/config/initializers/job_configurations.rb +++ b/config/initializers/job_configurations.rb @@ -6,7 +6,7 @@ cron_24h = '0 0 * * *' cron_24h_1am = '0 1 * * *' # 1am UTC is 8pm EST/9pm EDT gpo_cron_24h = '0 10 * * *' # 10am UTC is 5am EST/6am EDT -cron_1w = '0 0 * * 0' +cron_every_monday = 'every Monday at 0:00 UTC' # equivalent to '0 0 * * 1' if defined?(Rails::Console) Rails.logger.info 'job_configurations: console detected, skipping schedule' @@ -211,11 +211,11 @@ cron: cron_24h, args: -> { [Time.zone.yesterday.end_of_day] }, }, - # Send weekly authentication reports to partners + # Send previous week's authentication reports to partners weekly_authentication_report: { class: 'Reports::AuthenticationReport', - cron: cron_1w, - args: -> { [Time.zone.now] }, + cron: cron_every_monday, + args: -> { [Time.zone.yesterday] }, }, # Send fraud metrics to Team Judy fraud_metrics_report: { @@ -223,11 +223,11 @@ cron: cron_24h, args: -> { [Time.zone.yesterday.end_of_day] }, }, - # Weekly drop of report - drop_off_report: { + # Previous week's drop of report + weekly_drop_off_report: { class: 'Reports::DropOffReport', - cron: cron_1w, - args: -> { [Time.zone.today] }, + cron: cron_every_monday, + args: -> { [Time.zone.yesterday] }, }, }.compact end diff --git a/spec/jobs/job_configurations_spec.rb b/spec/jobs/job_configurations_spec.rb new file mode 100644 index 00000000000..24417c828b7 --- /dev/null +++ b/spec/jobs/job_configurations_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' + +RSpec.describe 'job_configurations' do + describe 'weekly report' do + %w[drop_off_report authentication_report].each do |job_name| + it "schedules the #{job_name.humanize} to run after the end of the week with yesterday's date" do + report = GoodJob.configuration.cron[:"weekly_#{job_name}"] + expect(report[:class]).to eq("Reports::#{job_name.camelize}") + + # Always passes the previous day as the argument + expect(report[:args].call).to eq([Time.zone.yesterday]) + + now = Time.zone.now + next_time = Fugit.parse(report[:cron]).next_time + expect(next_time.utc > now.utc.end_of_week). + to be(true), "Expected #{job_name.humanize} to run after the end of the week" + expect(next_time.utc). + to be_within(1).of(now.utc.end_of_week), + "Expected #{job_name.humanize} to run soon after the end of week, \ + \nso CONUS 'yesterday' and UTC 'yesterday' will never be different" + end + end + end +end From 452fe40c97790a7a80971f4837e832326acabdd3 Mon Sep 17 00:00:00 2001 From: "Howard M. Miller" <3620291+h-m-m@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:08:51 -0400 Subject: [PATCH 2/2] Update job config spec based on PR feedback Co-authored-by: Zach Margolis changelog: Internal, Reporting, Move weekly reports to run at 00:00 Monday UTC --- .../initializers/job_configurations_spec.rb | 24 +++++++++++++++++++ spec/jobs/job_configurations_spec.rb | 24 ------------------- 2 files changed, 24 insertions(+), 24 deletions(-) delete mode 100644 spec/jobs/job_configurations_spec.rb diff --git a/spec/config/initializers/job_configurations_spec.rb b/spec/config/initializers/job_configurations_spec.rb index cb03410ce42..0ebf67e9a93 100644 --- a/spec/config/initializers/job_configurations_spec.rb +++ b/spec/config/initializers/job_configurations_spec.rb @@ -11,4 +11,28 @@ end end end + + describe 'weekly reporting' do + %w[drop_off_report authentication_report].each do |job_name| + it "schedules the #{job_name} to run after the end of the week with yesterday's date" do + report = GoodJob.configuration.cron[:"weekly_#{job_name}"] + expect(report[:class]).to eq("Reports::#{job_name.camelize}") + + freeze_time do + # Always passes the previous day as the argument + expect(report[:args].call).to eq([Time.zone.yesterday]) + + now = Time.zone.now + next_time = Fugit.parse(report[:cron]).next_time + expect(next_time.utc > now.utc.end_of_week). + to be(true), "Expected #{job_name} to next run after the end of this week" + expect(next_time.utc). + to be_within(1).of(now.utc.end_of_week), <<~EOS.squish + Expected #{job_name} to run soon after the end of week, + so CONUS 'yesterday' and UTC 'yesterday' will never be different + EOS + end + end + end + end end diff --git a/spec/jobs/job_configurations_spec.rb b/spec/jobs/job_configurations_spec.rb deleted file mode 100644 index 24417c828b7..00000000000 --- a/spec/jobs/job_configurations_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'rails_helper' - -RSpec.describe 'job_configurations' do - describe 'weekly report' do - %w[drop_off_report authentication_report].each do |job_name| - it "schedules the #{job_name.humanize} to run after the end of the week with yesterday's date" do - report = GoodJob.configuration.cron[:"weekly_#{job_name}"] - expect(report[:class]).to eq("Reports::#{job_name.camelize}") - - # Always passes the previous day as the argument - expect(report[:args].call).to eq([Time.zone.yesterday]) - - now = Time.zone.now - next_time = Fugit.parse(report[:cron]).next_time - expect(next_time.utc > now.utc.end_of_week). - to be(true), "Expected #{job_name.humanize} to run after the end of the week" - expect(next_time.utc). - to be_within(1).of(now.utc.end_of_week), - "Expected #{job_name.humanize} to run soon after the end of week, \ - \nso CONUS 'yesterday' and UTC 'yesterday' will never be different" - end - end - end -end