From 74b001e4e429e16cc5cee22b528983b85274ebd7 Mon Sep 17 00:00:00 2001 From: Marius Melzer Date: Fri, 29 May 2020 01:40:58 +0200 Subject: [PATCH] Send shutdown messages to users when signaltower shuts down --- lib/signal_tower.ex | 17 ++++++++++++----- lib/signal_tower/websocket_handler.ex | 5 +++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/signal_tower.ex b/lib/signal_tower.ex index 0c67b57..da03a5d 100644 --- a/lib/signal_tower.ex +++ b/lib/signal_tower.ex @@ -10,11 +10,6 @@ defmodule SignalTower do |> start_supervisor() end - @impl Application - def stop(_state) do - :ok - end - defp start_cowboy() do {port, _} = Integer.parse(System.get_env("SIGNALTOWER_PORT") || "4233") @@ -45,4 +40,16 @@ defmodule SignalTower do Logger.info("SignalTower started") ret end + + @impl Application + def stop(_) do + DynamicSupervisor.which_children() + |> Enum.map(fn + {:undefined, pid, :worker, _} when is_pid(pid) -> + send(pid, :shutdown) + + _ -> + :ok + end) + end end diff --git a/lib/signal_tower/websocket_handler.ex b/lib/signal_tower/websocket_handler.ex index bac62be..3196ce2 100644 --- a/lib/signal_tower/websocket_handler.ex +++ b/lib/signal_tower/websocket_handler.ex @@ -67,6 +67,11 @@ defmodule SignalTower.WebsocketHandler do {:reply, {:text, internal_to_json(msg)}, state} end + @impl :cowboy_websocket + def websocket_info(:shutdown, state) do + {:reply, {:text, internal_to_json(%{event: "shutdown"})}, state} + end + @impl :cowboy_websocket def websocket_info(msg, state) do Logger.warn("Unknown info message: #{inspect(msg)}")