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:
parent
09a26f1167
commit
fe5228b306
2 changed files with 6 additions and 16 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue