From 32a3697ef0327ee35aa7c7f1dc9571dbc560b4d2 Mon Sep 17 00:00:00 2001 From: Vincent Degove Date: Mon, 16 Dec 2024 17:06:51 +0100 Subject: [PATCH] Move job to Oban --- .../transport/lib/jobs/outdated_data_notification_job.ex | 9 +++++++++ apps/transport/lib/transport/data_checker.ex | 6 +----- apps/transport/lib/transport/scheduler.ex | 2 -- apps/transport/test/transport/data_checker_test.exs | 4 ++-- config/runtime.exs | 3 ++- 5 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 apps/transport/lib/jobs/outdated_data_notification_job.ex diff --git a/apps/transport/lib/jobs/outdated_data_notification_job.ex b/apps/transport/lib/jobs/outdated_data_notification_job.ex new file mode 100644 index 0000000000..8fbf9f21a6 --- /dev/null +++ b/apps/transport/lib/jobs/outdated_data_notification_job.ex @@ -0,0 +1,9 @@ +defmodule Transport.Jobs.OutdatedDataNotificationJob do + use Oban.Worker, max_attempts: 3, tags: ["notifications"] + + @impl Oban.Worker + + def perform(%Oban.Job{id: job_id}) do + Transport.DataChecker.outdated_data(job_id) + end +end diff --git a/apps/transport/lib/transport/data_checker.ex b/apps/transport/lib/transport/data_checker.ex index b11d48c31c..7b7786b481 100644 --- a/apps/transport/lib/transport/data_checker.ex +++ b/apps/transport/lib/transport/data_checker.ex @@ -106,11 +106,7 @@ defmodule Transport.DataChecker do ) end - def outdated_data do - # Generated as an integer rather than a UUID because `payload.job_id` - # for other notifications are %Oban.Job.id (bigint). - job_id = Enum.random(1..Integer.pow(2, 63)) - + def outdated_data(job_id) do for delay <- possible_delays(), date = Date.add(Date.utc_today(), delay) do {delay, gtfs_datasets_expiring_on(date)} diff --git a/apps/transport/lib/transport/scheduler.ex b/apps/transport/lib/transport/scheduler.ex index 3c5dcfc799..5dc8d56fe2 100644 --- a/apps/transport/lib/transport/scheduler.ex +++ b/apps/transport/lib/transport/scheduler.ex @@ -13,8 +13,6 @@ defmodule Transport.Scheduler do [ # Every day at 4am UTC {"0 4 * * *", {Transport.ImportData, :import_validate_all, []}}, - # Send email for outdated data - {"@daily", {Transport.DataChecker, :outdated_data, []}}, # Set inactive data {"@daily", {Transport.DataChecker, :inactive_data, []}}, # Watch for new comments on datasets diff --git a/apps/transport/test/transport/data_checker_test.exs b/apps/transport/test/transport/data_checker_test.exs index 87d3f1a9d3..9f8dd16e26 100644 --- a/apps/transport/test/transport/data_checker_test.exs +++ b/apps/transport/test/transport/data_checker_test.exs @@ -233,7 +233,7 @@ defmodule Transport.DataCheckerTest do dataset_id: dataset.id }) - Transport.DataChecker.outdated_data() + Transport.DataChecker.outdated_data(42) # a first mail to our team @@ -271,7 +271,7 @@ defmodule Transport.DataCheckerTest do end test "outdated_data job with nothing to send should not send email" do - Transport.DataChecker.outdated_data() + Transport.DataChecker.outdated_data(42) assert_no_email_sent() end end diff --git a/config/runtime.exs b/config/runtime.exs index 3c72728c3e..59102d4062 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -160,7 +160,8 @@ oban_prod_crontab = [ {"30 5 * * *", Transport.Jobs.ImportDatasetMonthlyMetricsJob}, {"45 5 * * *", Transport.Jobs.ImportResourceMonthlyMetricsJob}, {"0 8 * * *", Transport.Jobs.WarnUserInactivityJob}, - {"*/5 * * * *", Transport.Jobs.UpdateCounterCacheJob} + {"*/5 * * * *", Transport.Jobs.UpdateCounterCacheJob}, + {"0 0 * * *", Transport.Jobs.OutdatedDataNotificationJob} ] # Make sure that all modules exist