- SolarIndexWorker accepts date arg for specific date fetch - Contact show page enqueues solar fetch when data missing - PubSub broadcast on solar data arrival, live-updates page - Spinner shown while fetching
47 lines
1.2 KiB
Elixir
47 lines
1.2 KiB
Elixir
defmodule Microwaveprop.Workers.SolarIndexWorker do
|
|
@moduledoc false
|
|
use Oban.Worker, queue: :solar, max_attempts: 3, unique: [period: 3600]
|
|
|
|
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
|