From fe5228b3063c883de32f3e1ce7affa3faf1f9ae6 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Tue, 31 Mar 2026 17:16:40 -0500 Subject: [PATCH] Prevent duplicate PropagationGridWorker jobs via Oban unique constraint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../propagation/freshness_monitor.ex | 19 ++++--------------- .../workers/propagation_grid_worker.ex | 3 ++- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/lib/microwaveprop/propagation/freshness_monitor.ex b/lib/microwaveprop/propagation/freshness_monitor.ex index 9aea7485..71df9917 100644 --- a/lib/microwaveprop/propagation/freshness_monitor.ex +++ b/lib/microwaveprop/propagation/freshness_monitor.ex @@ -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 diff --git a/lib/microwaveprop/workers/propagation_grid_worker.ex b/lib/microwaveprop/workers/propagation_grid_worker.ex index 0fb0417d..2289422b 100644 --- a/lib/microwaveprop/workers/propagation_grid_worker.ex +++ b/lib/microwaveprop/workers/propagation_grid_worker.ex @@ -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