use oban cron
This commit is contained in:
parent
2dd5571577
commit
70aecf091b
3 changed files with 2 additions and 123 deletions
|
|
@ -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
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue