Enqueue propagation grid worker on startup if scores are stale
On app start, check if latest scores are older than 2 hours. If so, immediately enqueue a PropagationGridWorker job. Covers app restarts, deploys, and missed cron ticks.
This commit is contained in:
parent
4301f48d27
commit
c709fb5c32
1 changed files with 24 additions and 1 deletions
|
|
@ -5,6 +5,8 @@ defmodule Microwaveprop.Application do
|
|||
|
||||
use Application
|
||||
|
||||
require Logger
|
||||
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
migrate()
|
||||
|
|
@ -16,7 +18,8 @@ defmodule Microwaveprop.Application do
|
|||
{Phoenix.PubSub, name: Microwaveprop.PubSub},
|
||||
{Oban, Application.fetch_env!(:microwaveprop, Oban)},
|
||||
# Start to serve requests, typically the last entry
|
||||
MicrowavepropWeb.Endpoint
|
||||
MicrowavepropWeb.Endpoint,
|
||||
{Task, &ensure_fresh_scores/0}
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
|
|
@ -31,6 +34,26 @@ defmodule Microwaveprop.Application do
|
|||
Microwaveprop.Release.migrate()
|
||||
end
|
||||
|
||||
# If propagation scores are older than 2 hours, enqueue a grid worker immediately.
|
||||
# Covers app restarts, deploys, and missed cron ticks.
|
||||
defp ensure_fresh_scores do
|
||||
case Microwaveprop.Propagation.latest_valid_time() do
|
||||
nil ->
|
||||
Logger.info("No propagation scores found, enqueuing grid worker")
|
||||
Oban.insert(Microwaveprop.Workers.PropagationGridWorker.new(%{}))
|
||||
|
||||
latest ->
|
||||
age_minutes = DateTime.diff(DateTime.utc_now(), latest, :minute)
|
||||
|
||||
if age_minutes > 120 do
|
||||
Logger.info("Propagation scores are #{age_minutes}m old, enqueuing grid worker")
|
||||
Oban.insert(Microwaveprop.Workers.PropagationGridWorker.new(%{}))
|
||||
else
|
||||
Logger.info("Propagation scores are #{age_minutes}m old, fresh enough")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def config_change(changed, _new, removed) do
|
||||
MicrowavepropWeb.Endpoint.config_change(changed, removed)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue