prop/lib/microwaveprop/workers/solar_index_worker.ex
Graham McIntire 68fdd1d2b6
Fix unique constraint blocking re-enqueues of completed jobs
- Only deduplicate against active jobs (available/scheduled/executing/
  retryable), not completed ones. Completed jobs no longer block new
  inserts for the same args.
- Period reduced from 3600s to 300s
- Fixes contacts stuck in :queued with no actual Oban jobs
2026-04-02 16:34:39 -05:00

47 lines
1.2 KiB
Elixir

defmodule Microwaveprop.Workers.SolarIndexWorker do
@moduledoc false
use Oban.Worker, queue: :solar, max_attempts: 3, unique: [period: 300, states: [:available, :scheduled, :executing, :retryable]]
alias Microwaveprop.Weather
alias Microwaveprop.Weather.SolarClient
@impl Oban.Worker
def perform(%Oban.Job{args: %{"date" => date_str}}) do
{:ok, target_date} = Date.from_iso8601(date_str)
case SolarClient.fetch_solar_indices() do
{:ok, all_records} ->
matched = Enum.filter(all_records, fn r -> r.date == target_date end)
Enum.each(matched, &Weather.upsert_solar_index/1)
if matched != [] do
Phoenix.PubSub.broadcast(
Microwaveprop.PubSub,
"contact_enrichment:solar",
{:solar_ready, date_str}
)
end
:ok
{:error, reason} ->
{:error, reason}
end
end
def perform(%Oban.Job{}) do
since_date = Date.add(Date.utc_today(), -7)
case SolarClient.fetch_solar_indices() do
{:ok, all_records} ->
all_records
|> SolarClient.filter_since(since_date)
|> Enum.each(&Weather.upsert_solar_index/1)
:ok
{:error, reason} ->
{:error, reason}
end
end
end