From c85b907ab70ed4c6236c9a8b7aa34e5d5716bb2c Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sat, 26 Jul 2025 10:10:55 -0500 Subject: [PATCH] Move Redis PubSub config to runtime initialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove static Redis PubSub configuration from runtime.exs - Add dynamic pubsub_config/0 function in application.ex - Configure PubSub adapter based on cluster_enabled and REDIS_URL at startup - Ensures proper initialization order for distributed deployments 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- config/runtime.exs | 15 --------------- lib/aprsme/application.ex | 18 +++++++++++++++++- .../live/map_live/packet_processor.ex | 4 ++-- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index 989ca6e..7ea1f2c 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -122,21 +122,6 @@ if config_env() == :prod do config :aprsme, :cluster_enabled, cluster_enabled - # Configure Redis PubSub when clustering is enabled - if cluster_enabled and System.get_env("REDIS_URL") do - config :aprsme, AprsmeWeb.Endpoint, - pubsub_server: Aprsme.PubSub, - pubsub: [ - name: Aprsme.PubSub, - adapter: Phoenix.PubSub.Redis, - redis_pool_size: 10, - node_name: node(), - redis_options: [ - url: System.get_env("REDIS_URL") - ] - ] - end - # Optional: Set the default "from" email address config :aprsme, default_from_email: "w5isp@aprs.me" diff --git a/lib/aprsme/application.ex b/lib/aprsme/application.ex index f4056bc..8c8bebc 100644 --- a/lib/aprsme/application.ex +++ b/lib/aprsme/application.ex @@ -22,7 +22,7 @@ defmodule Aprsme.Application do # Start the Ecto repository Aprsme.Repo, # Start the PubSub system - {Phoenix.PubSub, name: Aprsme.PubSub}, + pubsub_config(), # Start the rate limiter Aprsme.RateLimiter, # Start cache systems @@ -162,4 +162,20 @@ defmodule Aprsme.Application do Logger.info("Automatic migrations disabled") end + + defp pubsub_config do + cluster_enabled = Application.get_env(:aprsme, :cluster_enabled, false) + redis_url = System.get_env("REDIS_URL") + + if cluster_enabled and redis_url do + {Phoenix.PubSub, + name: Aprsme.PubSub, + adapter: Phoenix.PubSub.Redis, + redis_pool_size: 10, + node_name: node(), + redis_options: [url: redis_url]} + else + {Phoenix.PubSub, name: Aprsme.PubSub} + end + end end diff --git a/lib/aprsme_web/live/map_live/packet_processor.ex b/lib/aprsme_web/live/map_live/packet_processor.ex index 7fddaf9..0655fde 100644 --- a/lib/aprsme_web/live/map_live/packet_processor.ex +++ b/lib/aprsme_web/live/map_live/packet_processor.ex @@ -38,11 +38,11 @@ defmodule AprsmeWeb.MapLive.PacketProcessor do # Handle packet visibility logic socket = handle_packet_visibility(packet, lat, lon, callsign_key, socket) - + # Update last update timestamp for real-time display in map sidebar # This timestamp is shown to users to indicate when data was last refreshed socket = assign(socket, :last_update_at, DateTime.utc_now()) - + {:noreply, socket} end