Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send shutdown messages to users when signaltower shuts down #24

Merged
merged 1 commit into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 prep_stop(_state) do
DynamicSupervisor.which_children(Room.Supervisor)
|> Enum.map(fn
{:undefined, pid, :worker, _} when is_pid(pid) ->
send(pid, :shutdown)

_ ->
:ok
end)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the signal handler?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... Is this the signal handler

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by signal handler? stop(state) is a callback of the Application behaviour ("interface") and it gets called when the application is asked to shut down - is that what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats what i mean. I was refering to signal handlers used in c++ programms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the 'asked to shutdown' compareable to unix signal e.g.: sigterm?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. "By default, a SIGTERM from the operating system will automatically translate to System.stop/0." https://hexdocs.pm/elixir/Application.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx a lot for clarification!

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