diff --git a/lib/aprsme/application.ex b/lib/aprsme/application.ex index d9fb95e..e64202e 100644 --- a/lib/aprsme/application.ex +++ b/lib/aprsme/application.ex @@ -24,10 +24,6 @@ defmodule Aprsme.Application do # Start the PubSub system pubsub_config(), # Start Redis-based rate limiter and caches (only if Redis is available) - # Start signal handler for OS signals - Aprsme.SignalHandler, - # Start shutdown handler for graceful shutdowns - Aprsme.ShutdownHandler, # Start circuit breaker Aprsme.CircuitBreaker, # Start device cache manager @@ -55,6 +51,15 @@ defmodule Aprsme.Application do ] children = children ++ redis_children() + + # Add shutdown handlers at the end, after everything else is started + children = + children ++ + [ + Aprsme.SignalHandler, + Aprsme.ShutdownHandler + ] + children = maybe_add_cluster_components(children) children = maybe_add_is_supervisor(children, Application.get_env(:aprsme, :env)) children = maybe_add_aprs_connection(children, Application.get_env(:aprsme, :env)) diff --git a/lib/aprsme/device_cache.ex b/lib/aprsme/device_cache.ex index 8635440..da75e82 100644 --- a/lib/aprsme/device_cache.ex +++ b/lib/aprsme/device_cache.ex @@ -53,18 +53,26 @@ defmodule Aprsme.DeviceCache do @impl true def init(_) do + # Delay initial load to allow Redis connections to establish + Process.send_after(self(), :initial_load, 1_000) + + {:ok, %{initial_load_done: false}} + end + + @impl true + def handle_info(:initial_load, state) do # Load devices on startup case load_devices_into_cache() do :ok -> # Schedule periodic refresh Process.send_after(self(), :refresh_cache, @refresh_interval) + {:noreply, %{state | initial_load_done: true}} :error -> # If initial load failed, retry sooner - Process.send_after(self(), :refresh_cache, 5_000) + Process.send_after(self(), :initial_load, 5_000) + {:noreply, state} end - - {:ok, %{}} end @impl true