aprs.me/lib/aprsme/cluster/packet_distributor.ex
Graham McIntire d8606bb609
Complete nested module alias fixes and update Sobelow skips
Software design fixes:
- Add proper aliases for all nested modules
- release.ex: Ecto.Adapters.SQL
- packet_consumer.ex: Aprsme.Cluster.PacketDistributor
- cleanup_scheduler.ex: Aprsme.Workers.PacketCleanupWorker
- health_check.ex: Ecto.Adapters.SQL
- status_live/index.ex: Aprsme.Cluster.LeaderElection
- packet_receiver.ex: Aprsme.Cluster.LeaderElection
- packet_distributor.ex: Aprsme.Cluster.LeaderElection, AprsmeWeb.MapLive.PacketStore

Security:
- Update .sobelow-skips for false positive SQL injection warning (div is builtin function)

All 13 software design suggestions now complete.
All 13 warnings previously fixed.
Remaining: 42 refactoring opportunities (complex/nested functions)
2026-02-09 11:26:47 -06:00

43 lines
1.3 KiB
Elixir

defmodule Aprsme.Cluster.PacketDistributor do
@moduledoc """
Distributes APRS packets from the leader node to all cluster members.
This ensures all nodes can serve real-time updates via LiveView while
only the leader maintains the APRS-IS connection.
"""
alias Aprsme.Cluster.LeaderElection
alias AprsmeWeb.MapLive.PacketStore
require Logger
@pubsub_topic "cluster:packets"
def distribute_packet(packet) do
# Only distribute if clustering is enabled and we're the leader
cluster_enabled = Application.get_env(:aprsme, :cluster_enabled, false)
if cluster_enabled and LeaderElection.leader?() do
# Broadcast to all nodes including self
Phoenix.PubSub.broadcast(
Aprsme.PubSub,
@pubsub_topic,
{:distributed_packet, packet}
)
Logger.debug("Distributed packet #{packet.raw} to cluster")
end
end
def subscribe do
Phoenix.PubSub.subscribe(Aprsme.PubSub, @pubsub_topic)
end
def handle_distributed_packet({:distributed_packet, packet}) do
# Broadcast to local LiveView clients
Aprsme.StreamingPacketsPubSub.broadcast_packet(packet)
# Update packet store for LiveView
PacketStore.store_packet(packet)
Logger.debug("Received distributed packet on node #{node()}")
end
end