prop/lib/microwaveprop/workers/solar_index_worker.ex
2026-06-16 12:38:08 -05:00

51 lines
1.2 KiB
Elixir

defmodule Microwaveprop.Workers.SolarIndexWorker do
@moduledoc false
use Oban.Pro.Worker,
queue: :solar,
max_attempts: 3,
unique: [period: 300, states: :incomplete]
alias Microwaveprop.Weather
alias Microwaveprop.Weather.SolarClient
@impl Oban.Pro.Worker
def process(%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)
Weather.upsert_solar_indices_batch(matched)
_ =
if matched != [] do
Phoenix.PubSub.broadcast(
Microwaveprop.PubSub,
"contact_enrichment:solar",
{:solar_ready, date_str}
)
end
:ok
{:error, reason} ->
{:error, reason}
end
end
def process(%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)
|> Weather.upsert_solar_indices_batch()
:ok
{:error, reason} ->
{:error, reason}
end
end
end