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
|
end
|
||||||
|
|
||||||
defp enqueue_if_not_queued do
|
defp enqueue_if_not_queued do
|
||||||
# Only enqueue if there isn't already a pending/running grid worker job
|
# Oban unique constraint on PropagationGridWorker prevents duplicates —
|
||||||
import Ecto.Query
|
# if a job is already available/scheduled/executing/retryable within the
|
||||||
|
# last hour, insert is a no-op.
|
||||||
pending =
|
Oban.insert(PropagationGridWorker.new(%{}))
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,8 @@ defmodule Microwaveprop.Workers.PropagationGridWorker do
|
||||||
|
|
||||||
use Oban.Worker,
|
use Oban.Worker,
|
||||||
queue: :propagation,
|
queue: :propagation,
|
||||||
max_attempts: 3
|
max_attempts: 3,
|
||||||
|
unique: [period: 3600, states: [:available, :scheduled, :executing, :retryable]]
|
||||||
|
|
||||||
alias Microwaveprop.Propagation
|
alias Microwaveprop.Propagation
|
||||||
alias Microwaveprop.Propagation.Grid
|
alias Microwaveprop.Propagation.Grid
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue