Fix startup issues with Redis and shutdown handlers

- Delay DeviceCache initial load by 1 second to allow Redis connections
- Move shutdown handlers to end of supervision tree
- This prevents shutdown handlers from interfering with startup failures

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2025-07-26 16:00:54 -05:00
parent 60efa7677b
commit aa5cfb8240
No known key found for this signature in database
2 changed files with 20 additions and 7 deletions

View file

@ -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))

View file

@ -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