defmodule Microwaveprop.Propagation.GridTaskEnqueuer do @moduledoc """ Seeds `grid_tasks` rows for the Rust `prop-grid-rs` worker. Called from `PropagationGridWorker.seed_chain/0` alongside the existing Elixir f00..f18 Oban fan-out. Rust only claims rows with `forecast_hour > 0`; Elixir still owns the f00 analysis-hour chain because of native-duct + NEXRAD + commercial-link enrichment. Inserts are idempotent via the `(run_time, forecast_hour)` unique index — re-seeding the same cycle is a no-op. """ alias Microwaveprop.Repo require Logger @max_forecast_hour 18 @spec seed(DateTime.t()) :: {:ok, non_neg_integer()} | {:error, term()} def seed(%DateTime{} = run_time) do run_time = DateTime.truncate(run_time, :second) now = DateTime.truncate(DateTime.utc_now(), :microsecond) rows = for fh <- 1..@max_forecast_hour do %{ id: Ecto.UUID.bingenerate(), run_time: run_time, forecast_hour: fh, valid_time: DateTime.add(run_time, fh * 3600, :second), status: "queued", attempt: 0, claimed_at: nil, completed_at: nil, error: nil, inserted_at: now, updated_at: now } end {count, _} = Repo.insert_all("grid_tasks", rows, on_conflict: :nothing, conflict_target: [:run_time, :forecast_hour] ) if count > 0 do Logger.info("GridTaskEnqueuer: seeded #{count} grid_tasks for run_time=#{run_time}") else Logger.info("GridTaskEnqueuer: run_time=#{run_time} already seeded") end {:ok, count} rescue e -> Logger.error("GridTaskEnqueuer: seed failed: #{inspect(e)}") {:error, e} end end