defmodule Microwaveprop.Application do # See https://hexdocs.pm/elixir/Application.html # for more information on OTP Applications @moduledoc false use Application require Logger @impl true def start(_type, _args) do migrate() children = [ MicrowavepropWeb.Telemetry, Microwaveprop.Repo, {DNSCluster, query: Application.get_env(:microwaveprop, :dns_cluster_query) || :ignore}, {Phoenix.PubSub, name: Microwaveprop.PubSub}, {Oban, Application.fetch_env!(:microwaveprop, Oban)}, # Start to serve requests, typically the last entry MicrowavepropWeb.Endpoint, {Task, &ensure_fresh_scores/0} ] # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: Microwaveprop.Supervisor] Supervisor.start_link(children, opts) end # Tell Phoenix to update the endpoint configuration # whenever the application is updated. defp migrate 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) :ok end end