aprs.me/lib/aprsme/postgres_notifier.ex

34 lines
1.1 KiB
Elixir

defmodule Aprsme.PostgresNotifier do
@moduledoc """
Listens to PostgreSQL NOTIFY events on the "aprs_events" channel (used by
bad_packets) and broadcasts them via Phoenix.PubSub.
Packet notifications previously came through here via the "aprs_packets"
channel + `packets_notify_insert` trigger. That path was removed to avoid
per-row trigger overhead and double-broadcasting — packets are now
broadcast directly from `Aprsme.PacketConsumer` to the same topics.
"""
use GenServer
@event_channel "aprs_events"
@event_topic "postgres:aprsme_events"
def start_link(_opts) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
@impl true
def init(_) do
{:ok, conn} = Postgrex.Notifications.start_link(Aprsme.Repo.config())
{:ok, _ref} = Postgrex.Notifications.listen(conn, @event_channel)
{:ok, %{conn: conn}}
end
@impl true
def handle_info({:notification, _conn, _pid, @event_channel, payload}, state) do
Phoenix.PubSub.broadcast(Aprsme.PubSub, @event_topic, {:postgres_notify, payload})
{:noreply, state}
end
def handle_info(_msg, state), do: {:noreply, state}
end