Prevent duplicate PropagationGridWorker jobs via Oban unique constraint

Worker now uses unique: [period: 3600, states: [:available, :scheduled,
:executing, :retryable]] so Oban atomically prevents duplicates.
FreshnessMonitor simplified — no longer does manual oban_jobs query.
This commit is contained in:
Graham McIntire 2026-03-31 17:16:40 -05:00
parent 09a26f1167
commit fe5228b306
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59
2 changed files with 6 additions and 16 deletions

View file

@ -53,20 +53,9 @@ defmodule Microwaveprop.Propagation.FreshnessMonitor do
end
defp enqueue_if_not_queued do
# Only enqueue if there isn't already a pending/running grid worker job
import Ecto.Query
pending =
Microwaveprop.Repo.exists?(
from(j in "oban_jobs",
where:
j.worker == "Microwaveprop.Workers.PropagationGridWorker" and
j.state in ["available", "scheduled", "executing"]
)
)
if !pending do
Oban.insert(PropagationGridWorker.new(%{}))
end
# Oban unique constraint on PropagationGridWorker prevents duplicates —
# if a job is already available/scheduled/executing/retryable within the
# last hour, insert is a no-op.
Oban.insert(PropagationGridWorker.new(%{}))
end
end

View file

@ -7,7 +7,8 @@ defmodule Microwaveprop.Workers.PropagationGridWorker do
use Oban.Worker,
queue: :propagation,
max_attempts: 3
max_attempts: 3,
unique: [period: 3600, states: [:available, :scheduled, :executing, :retryable]]
alias Microwaveprop.Propagation
alias Microwaveprop.Propagation.Grid