Skip to content

Commit

Permalink
Send shutdown messages to users when signaltower shuts down
Browse files Browse the repository at this point in the history
  • Loading branch information
farao committed May 28, 2020
1 parent 63ccfff commit 74b001e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/signal_tower.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions lib/signal_tower/websocket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
Expand Down

0 comments on commit 74b001e

Please sign in to comment.