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
|
use Application
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
migrate()
|
migrate()
|
||||||
|
|
@ -16,7 +18,8 @@ defmodule Microwaveprop.Application do
|
||||||
{Phoenix.PubSub, name: Microwaveprop.PubSub},
|
{Phoenix.PubSub, name: Microwaveprop.PubSub},
|
||||||
{Oban, Application.fetch_env!(:microwaveprop, Oban)},
|
{Oban, Application.fetch_env!(:microwaveprop, Oban)},
|
||||||
# Start to serve requests, typically the last entry
|
# Start to serve requests, typically the last entry
|
||||||
MicrowavepropWeb.Endpoint
|
MicrowavepropWeb.Endpoint,
|
||||||
|
{Task, &ensure_fresh_scores/0}
|
||||||
]
|
]
|
||||||
|
|
||||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
|
|
@ -31,6 +34,26 @@ defmodule Microwaveprop.Application do
|
||||||
Microwaveprop.Release.migrate()
|
Microwaveprop.Release.migrate()
|
||||||
end
|
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
|
@impl true
|
||||||
def config_change(changed, _new, removed) do
|
def config_change(changed, _new, removed) do
|
||||||
MicrowavepropWeb.Endpoint.config_change(changed, removed)
|
MicrowavepropWeb.Endpoint.config_change(changed, removed)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue