From 70aecf091b515be2f593c899099c84b9ff162aa1 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Tue, 17 Jun 2025 11:59:40 -0500 Subject: [PATCH] use oban cron --- lib/aprs/application.ex | 2 - lib/aprs/workers/packet_cleanup_worker.ex | 42 +----------- lib/aprs/workers/scheduler.ex | 81 ----------------------- 3 files changed, 2 insertions(+), 123 deletions(-) delete mode 100644 lib/aprs/workers/scheduler.ex diff --git a/lib/aprs/application.ex b/lib/aprs/application.ex index dccf2f1..5cdd389 100644 --- a/lib/aprs/application.ex +++ b/lib/aprs/application.ex @@ -31,8 +31,6 @@ defmodule Aprs.Application do {Cluster.Supervisor, [topologies, [name: Aprs.ClusterSupervisor]]}, # Start Oban for background jobs {Oban, :aprs |> Application.get_env(Oban, []) |> Keyword.put(:queues, default: 10, maintenance: 2)}, - # Start the scheduler for recurring Oban jobs - Aprs.Workers.Scheduler, Aprs.Presence ] diff --git a/lib/aprs/workers/packet_cleanup_worker.ex b/lib/aprs/workers/packet_cleanup_worker.ex index 0ae0fef..e2c8097 100644 --- a/lib/aprs/workers/packet_cleanup_worker.ex +++ b/lib/aprs/workers/packet_cleanup_worker.ex @@ -7,12 +7,11 @@ defmodule Aprs.Workers.PacketCleanupWorker do 2. Providing granular cleanup with custom age parameters 3. Logging statistics about cleanup operations + This worker is scheduled to run daily at midnight UTC via Oban's cron feature. + ## Functions - `perform/1` - Standard cleanup using configured retention period - `cleanup_packets_older_than/1` - Cleanup packets older than specified days - - `schedule_cleanup/0` - Schedule standard cleanup job - - `schedule_cleanup_for_age/1` - Schedule cleanup with custom age parameter - - `schedule_daily_cleanup/0` - Schedule daily cleanup at midnight UTC """ use Oban.Worker, queue: :maintenance, max_attempts: 3 @@ -59,43 +58,6 @@ defmodule Aprs.Workers.PacketCleanupWorker do :ok end - # Schedule this job to run daily - def schedule_cleanup do - %{} - |> Oban.Job.new(worker: __MODULE__, queue: :maintenance) - |> Oban.insert() - end - - # Schedule cleanup with custom age parameter - def schedule_cleanup_for_age(days) when is_integer(days) and days > 0 do - %{"cleanup_days" => days} - |> Oban.Job.new(worker: __MODULE__, queue: :maintenance) - |> Oban.insert() - end - - # Schedule this job to run daily at midnight UTC - def schedule_daily_cleanup do - # Calculate when the next midnight UTC is - now = DateTime.utc_now() - tomorrow = Date.add(now, 1) - - next_midnight = %{ - now - | year: tomorrow.year, - month: tomorrow.month, - day: tomorrow.day, - hour: 0, - minute: 0, - second: 0, - microsecond: {0, 6} - } - - # Schedule the job - %{} - |> Oban.Job.new(worker: __MODULE__, queue: :maintenance) - |> Oban.insert(scheduled_at: next_midnight) - end - defp count_total_packets do Repo.aggregate(Packet, :count, :id) end diff --git a/lib/aprs/workers/scheduler.ex b/lib/aprs/workers/scheduler.ex deleted file mode 100644 index 651666e..0000000 --- a/lib/aprs/workers/scheduler.ex +++ /dev/null @@ -1,81 +0,0 @@ -defmodule Aprs.Workers.Scheduler do - @moduledoc """ - Scheduler for Oban jobs. - - This module is responsible for scheduling recurring jobs in the application. - It should be started as part of the application supervision tree. - """ - - use GenServer - - alias Aprs.Workers.PacketCleanupWorker - - require Logger - - # Start the scheduler on application boot - def start_link(_opts) do - GenServer.start_link(__MODULE__, %{}) - end - - @impl true - def init(state) do - # Schedule initial jobs - schedule_initial_jobs() - - # Return with state - {:ok, state} - end - - @impl true - def handle_info(:schedule_daily_jobs, state) do - # Schedule daily cleanup job - schedule_daily_jobs() - - # Schedule this function to run again tomorrow - schedule_next_day_scheduler() - - {:noreply, state} - end - - # Schedule jobs when the application starts - defp schedule_initial_jobs do - Logger.info("Scheduling initial Oban jobs") - - # Schedule the packet cleanup job - {:ok, _job} = PacketCleanupWorker.schedule_cleanup() - - # Schedule the daily scheduler to run at midnight - schedule_next_day_scheduler() - end - - # Schedule jobs that should run daily - defp schedule_daily_jobs do - Logger.info("Scheduling daily Oban jobs") - - # Schedule the packet cleanup job - {:ok, _job} = PacketCleanupWorker.schedule_cleanup() - end - - # Schedule the next day's job scheduler to run at midnight UTC - defp schedule_next_day_scheduler do - # Calculate when the next midnight UTC is - now = DateTime.utc_now() - tomorrow = Date.add(now, 1) - - next_midnight = %{ - now - | year: tomorrow.year, - month: tomorrow.month, - day: tomorrow.day, - hour: 0, - minute: 0, - second: 0, - microsecond: {0, 6} - } - - seconds_until_midnight = DateTime.diff(next_midnight, now) - - # Schedule this function to run at midnight - Process.send_after(self(), :schedule_daily_jobs, seconds_until_midnight * 1000) - end -end