diff --git a/elixirkit/lib/elixirkit/server.ex b/elixirkit/lib/elixirkit/server.ex index 08d48a6c6..5525dc404 100644 --- a/elixirkit/lib/elixirkit/server.ex +++ b/elixirkit/lib/elixirkit/server.ex @@ -24,7 +24,6 @@ defmodule ElixirKit.Server do @impl true def handle_info({:tcp_closed, socket}, state) when socket == state.socket do - System.stop() {:stop, :shutdown, state} end end diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index 0cffac2af..e6f675534 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -115,6 +115,20 @@ defmodule Livebook.Config do Application.get_env(:livebook, :iframe_url) end + @doc """ + Shuts down the system, if possible. + """ + def shutdown do + case Livebook.Config.shutdown_callback() do + {m, f, a} -> + Phoenix.PubSub.broadcast(Livebook.PubSub, "sidebar", :shutdown) + apply(m, f, a) + + nil -> + :ok + end + end + @doc """ Returns an mfa if there's a way to shut down the system. """ diff --git a/lib/livebook_app.ex b/lib/livebook_app.ex index ca007cb5f..3ea238582 100644 --- a/lib/livebook_app.ex +++ b/lib/livebook_app.ex @@ -8,8 +8,9 @@ if Mix.target() == :app do @impl true def init(_) do - {:ok, _} = ElixirKit.start() - {:ok, nil} + {:ok, pid} = ElixirKit.start() + ref = Process.monitor(pid) + {:ok, %{ref: ref}} end @impl true @@ -18,6 +19,12 @@ if Mix.target() == :app do {:noreply, state} end + @impl true + def handle_info({:DOWN, ref, :process, _, :shutdown}, state) when ref == state.ref do + Livebook.Config.shutdown() + {:noreply, state} + end + defp open("") do open(LivebookWeb.Endpoint.access_url()) end diff --git a/lib/livebook_web/live/hooks/sidebar_hook.ex b/lib/livebook_web/live/hooks/sidebar_hook.ex index 3c7837955..00c44d66f 100644 --- a/lib/livebook_web/live/hooks/sidebar_hook.ex +++ b/lib/livebook_web/live/hooks/sidebar_hook.ex @@ -7,17 +7,23 @@ defmodule LivebookWeb.SidebarHook do def on_mount(:default, _params, _session, socket) do if connected?(socket) do Livebook.Hubs.subscribe([:crud, :connection]) + Phoenix.PubSub.subscribe(Livebook.PubSub, "sidebar") end socket = socket |> assign(saved_hubs: Livebook.Hubs.get_metadatas()) |> attach_hook(:hubs, :handle_info, &handle_info/2) + |> attach_hook(:shutdown, :handle_info, &handle_info/2) |> attach_hook(:shutdown, :handle_event, &handle_event/3) {:cont, socket} end + defp handle_info(:shutdown, socket) do + {:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")} + end + @connection_events ~w(hub_connected hub_disconnected hubs_metadata_changed)a defp handle_info(event, socket) when event in @connection_events do @@ -33,15 +39,8 @@ defmodule LivebookWeb.SidebarHook do defp handle_info(_event, socket), do: {:cont, socket} defp handle_event("shutdown", _params, socket) do - case Livebook.Config.shutdown_callback() do - {m, f, a} -> - apply(m, f, a) - - {:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")} - - _ -> - socket - end + Livebook.Config.shutdown() + {:halt, socket} end defp handle_event(_event, _params, socket), do: {:cont, socket}