prop/lib/microwaveprop/workers/solar_index_worker.ex
Graham McIntire ea3033da03
chore: update to elixir 1.20 / otp 29, fix compile warnings
- Update .tool-versions to elixir 1.20.0-otp-29 / erlang 29.0.1
- Update all hex dependencies
- Remove unused aliases in path_compute.ex and rover_planning_live/show.ex
- Fix Oban unique states to include :suspended (use :incomplete group)
2026-06-03 14:34:39 -05:00

51 lines
1.2 KiB
Elixir

defmodule Microwaveprop.Workers.SolarIndexWorker do
@moduledoc false
use Oban.Worker,
queue: :solar,
max_attempts: 3,
unique: [period: 300, states: :incomplete]
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)
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 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)
|> Weather.upsert_solar_indices_batch()
:ok
{:error, reason} ->
{:error, reason}
end
end
end