91 lines
3.2 KiB
Elixir
91 lines
3.2 KiB
Elixir
defmodule Microwaveprop.Workers.HrrrFetchWorker do
|
|
@moduledoc false
|
|
use Oban.Worker, queue: :hrrr, max_attempts: 20
|
|
|
|
alias Microwaveprop.Weather
|
|
alias Microwaveprop.Weather.HrrrClient
|
|
alias Microwaveprop.Weather.SoundingParams
|
|
|
|
require Logger
|
|
|
|
@impl Oban.Worker
|
|
def backoff(%Oban.Job{attempt: attempt}) do
|
|
# Exponential backoff: 2m, 4m, 8m, 16m, 32m, 1h, 2h, 4h, 6h, 6h, ...
|
|
min(120 * Integer.pow(2, attempt - 1), _six_hours = 21_600)
|
|
end
|
|
|
|
@impl Oban.Worker
|
|
def perform(%Oban.Job{args: args}) do
|
|
%{"lat" => raw_lat, "lon" => raw_lon, "valid_time" => valid_time_str} = args
|
|
|
|
{:ok, valid_time, _} = DateTime.from_iso8601(valid_time_str)
|
|
{lat, lon} = Weather.round_to_hrrr_grid(raw_lat, raw_lon)
|
|
|
|
if Weather.has_hrrr_profile?(lat, lon, valid_time) do
|
|
Logger.info("HRRR profile already exists for #{lat},#{lon} @ #{valid_time_str}")
|
|
:ok
|
|
else
|
|
Logger.info("Fetching HRRR profile for #{lat},#{lon} @ #{valid_time_str}")
|
|
|
|
case HrrrClient.fetch_profile(lat, lon, valid_time) do
|
|
{:ok, data} ->
|
|
params = SoundingParams.derive(data.profile)
|
|
levels = length(data.profile)
|
|
|
|
attrs =
|
|
maybe_add_derived_params(
|
|
%{
|
|
valid_time: valid_time,
|
|
lat: lat,
|
|
lon: lon,
|
|
run_time: data.run_time,
|
|
profile: data.profile,
|
|
hpbl_m: data.hpbl_m,
|
|
pwat_mm: data.pwat_mm,
|
|
surface_temp_c: data.surface_temp_c,
|
|
surface_dewpoint_c: data.surface_dewpoint_c,
|
|
surface_pressure_mb: data.surface_pressure_mb
|
|
},
|
|
params
|
|
)
|
|
|
|
Weather.upsert_hrrr_profile(attrs)
|
|
|
|
Logger.info("HRRR profile saved for #{lat},#{lon} @ #{valid_time_str} (#{levels} levels, run #{data.run_time})")
|
|
|
|
:ok
|
|
|
|
{:error, reason} ->
|
|
if permanent_failure?(reason) do
|
|
Logger.warning("HRRR permanent failure for #{lat},#{lon} @ #{valid_time_str}: #{inspect(reason)}")
|
|
{:cancel, reason}
|
|
else
|
|
Logger.error("HRRR fetch error for #{lat},#{lon} @ #{valid_time_str}: #{inspect(reason)}")
|
|
{:error, reason}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
defp permanent_failure?("HRRR idx HTTP 404"), do: true
|
|
defp permanent_failure?("HRRR grib HTTP 404"), do: true
|
|
defp permanent_failure?(:outside_grid), do: true
|
|
defp permanent_failure?("malformed section"), do: true
|
|
defp permanent_failure?("missing sections: " <> _), do: true
|
|
defp permanent_failure?("not a GRIB2 message"), do: true
|
|
defp permanent_failure?("unsupported GRIB edition " <> _), do: true
|
|
defp permanent_failure?("GRIB2 extraction failed: " <> _), do: true
|
|
defp permanent_failure?("GRIB2 complex packing decode failed: " <> _), do: true
|
|
defp permanent_failure?(_), do: false
|
|
|
|
defp maybe_add_derived_params(attrs, nil), do: attrs
|
|
|
|
defp maybe_add_derived_params(attrs, params) do
|
|
Map.merge(attrs, %{
|
|
surface_refractivity: params.surface_refractivity,
|
|
min_refractivity_gradient: params.min_refractivity_gradient,
|
|
ducting_detected: params.ducting_detected,
|
|
duct_characteristics: params.duct_characteristics
|
|
})
|
|
end
|
|
end
|