Support specifying erl flags for standalone runtime node (#2843)

This commit is contained in:
Jonatan Kłosko 2024-10-28 17:56:32 +01:00 committed by GitHub
parent 4b324bb79e
commit c14bdb7830
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 125 additions and 30 deletions

View file

@ -1,5 +1,5 @@
defmodule Livebook.Runtime.Standalone do defmodule Livebook.Runtime.Standalone do
defstruct [:node, :server_pid] defstruct [:erl_flags, :node, :server_pid]
# A runtime backed by a standalone Elixir node managed by Livebook. # A runtime backed by a standalone Elixir node managed by Livebook.
# #
@ -31,16 +31,23 @@ defmodule Livebook.Runtime.Standalone do
alias Livebook.Utils alias Livebook.Utils
@type t :: %__MODULE__{ @type t :: %__MODULE__{
erl_flags: String.t() | nil,
node: node() | nil, node: node() | nil,
server_pid: pid() | nil server_pid: pid() | nil
} }
@doc """ @doc """
Returns a new runtime instance. Returns a new runtime instance.
## Options
* `:erl_flags` - erl flags to specify when starting the node
""" """
@spec new() :: t() @spec new(keyword()) :: t()
def new() do def new(opts \\ []) do
%__MODULE__{} opts = Keyword.validate!(opts, [:erl_flags])
%__MODULE__{erl_flags: opts[:erl_flags]}
end end
def __connect__(runtime) do def __connect__(runtime) do
@ -66,7 +73,7 @@ defmodule Livebook.Runtime.Standalone do
] ]
with {:ok, elixir_path} <- find_elixir_executable(), with {:ok, elixir_path} <- find_elixir_executable(),
port = start_elixir_node(elixir_path, child_node), port = start_elixir_node(elixir_path, child_node, runtime.erl_flags),
{:ok, server_pid} <- parent_init_sequence(child_node, port, init_opts) do {:ok, server_pid} <- parent_init_sequence(child_node, port, init_opts) do
runtime = %{runtime | node: child_node, server_pid: server_pid} runtime = %{runtime | node: child_node, server_pid: server_pid}
send(caller, {:runtime_connect_done, self(), {:ok, runtime}}) send(caller, {:runtime_connect_done, self(), {:ok, runtime}})
@ -84,7 +91,7 @@ defmodule Livebook.Runtime.Standalone do
end end
end end
defp start_elixir_node(elixir_path, node_name) do defp start_elixir_node(elixir_path, node_name, erl_flags) do
# Here we create a port to start the system process in a non-blocking way. # Here we create a port to start the system process in a non-blocking way.
Port.open({:spawn_executable, elixir_path}, [ Port.open({:spawn_executable, elixir_path}, [
:binary, :binary,
@ -93,7 +100,7 @@ defmodule Livebook.Runtime.Standalone do
# to the terminal # to the terminal
:nouse_stdio, :nouse_stdio,
:hide, :hide,
args: elixir_flags(node_name) args: elixir_flags(node_name, erl_flags)
]) ])
end end
@ -167,7 +174,7 @@ defmodule Livebook.Runtime.Standalone do
|> Base.encode64() |> Base.encode64()
end end
defp elixir_flags(node_name) do defp elixir_flags(node_name, erl_flags) do
parent_name = node() parent_name = node()
parent_port = Livebook.EPMD.dist_port() parent_port = Livebook.EPMD.dist_port()
@ -190,7 +197,8 @@ defmodule Livebook.Runtime.Standalone do
# startup # startup
# #
"+sbwt none +sbwtdcpu none +sbwtdio none +sssdio 128 -elixir ansi_enabled true -noinput " <> "+sbwt none +sbwtdcpu none +sbwtdio none +sssdio 128 -elixir ansi_enabled true -noinput " <>
"-epmd_module Elixir.Livebook.Runtime.EPMD", "-epmd_module Elixir.Livebook.Runtime.EPMD " <>
(erl_flags || ""),
# Add the location of Livebook.Runtime.EPMD # Add the location of Livebook.Runtime.EPMD
"-pa", "-pa",
epmd_module_path!(), epmd_module_path!(),
@ -247,8 +255,8 @@ defimpl Livebook.Runtime, for: Livebook.Runtime.Standalone do
:ok = RuntimeServer.stop(runtime.server_pid) :ok = RuntimeServer.stop(runtime.server_pid)
end end
def duplicate(_runtime) do def duplicate(runtime) do
Livebook.Runtime.Standalone.new() Livebook.Runtime.Standalone.new(erl_flags: runtime.erl_flags)
end end
def evaluate_code(runtime, language, code, locator, parent_locators, opts \\ []) do def evaluate_code(runtime, language, code, locator, parent_locators, opts \\ []) do

View file

@ -60,7 +60,7 @@ defmodule LivebookWeb.AppsDashboardLive do
<div class="flex flex-col space-y-4"> <div class="flex flex-col space-y-4">
<div :for={app <- Enum.sort_by(@apps, & &1.slug)} data-app-slug={app.slug}> <div :for={app <- Enum.sort_by(@apps, & &1.slug)} data-app-slug={app.slug}>
<a <a
phx-click={JS.toggle(to: "[data-app-slug=#{app.slug}] .toggle")} phx-click={JS.toggle(to: "[data-app-slug=#{app.slug}] [data-toggle]")}
class="flex items-center justify-between mb-2 hover:cursor-pointer" class="flex items-center justify-between mb-2 hover:cursor-pointer"
> >
<span class="text-gray-800 font-medium text-xl break-all"> <span class="text-gray-800 font-medium text-xl break-all">
@ -68,11 +68,11 @@ defmodule LivebookWeb.AppsDashboardLive do
</span> </span>
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<.app_group_tag app_spec={app.app_spec} /> <.app_group_tag app_spec={app.app_spec} />
<.remix_icon icon="arrow-drop-down-line" class="text-3xl text-gray-400 toggle" /> <.remix_icon icon="arrow-down-s-line" class="text-xl text-gray-700" data-toggle />
<.remix_icon icon="arrow-drop-right-line" class="text-3xl text-gray-400 hidden toggle" /> <.remix_icon icon="arrow-right-s-line" class="text-xl text-gray-700 hidden" data-toggle />
</div> </div>
</a> </a>
<div class="toggle"> <div data-toggle>
<div :if={app.warnings != []} class="my-3 flex flex-col gap-3"> <div :if={app.warnings != []} class="my-3 flex flex-col gap-3">
<.message_box :for={warning <- app.warnings} kind={:warning} message={warning} /> <.message_box :for={warning <- app.warnings} kind={:warning} message={warning} />
</div> </div>

View file

@ -78,7 +78,7 @@ defmodule LivebookWeb.SessionLive.AttachedRuntimeComponent do
autocomplete="off" autocomplete="off"
spellcheck="false" spellcheck="false"
> >
<div class="flex flex-col space-y-4 mb-5"> <div class="flex flex-col space-y-4 mb-6">
<.text_field field={f[:name]} label="Name" placeholder={test_node()} /> <.text_field field={f[:name]} label="Name" placeholder={test_node()} />
<.text_field field={f[:cookie]} label="Cookie" placeholder="mycookie" /> <.text_field field={f[:cookie]} label="Cookie" placeholder="mycookie" />
</div> </div>

View file

@ -1,6 +1,8 @@
defmodule LivebookWeb.SessionLive.StandaloneRuntimeComponent do defmodule LivebookWeb.SessionLive.StandaloneRuntimeComponent do
use LivebookWeb, :live_component use LivebookWeb, :live_component
import Ecto.Changeset
alias Livebook.{Session, Runtime} alias Livebook.{Session, Runtime}
@impl true @impl true
@ -12,30 +14,115 @@ defmodule LivebookWeb.SessionLive.StandaloneRuntimeComponent do
{:ok, socket} {:ok, socket}
end end
@impl true
def update(assigns, socket) do
changeset =
case socket.assigns[:changeset] do
nil ->
changeset(assigns.runtime)
changeset when socket.assigns.runtime == assigns.runtime ->
changeset
changeset ->
changeset(assigns.runtime, changeset.params)
end
socket =
socket
|> assign(assigns)
|> assign(:changeset, changeset)
{:ok, socket}
end
defp changeset(runtime, attrs \\ %{}) do
data =
case runtime do
%Runtime.Standalone{erl_flags: erl_flags} ->
%{erl_flags: erl_flags}
_ ->
%{erl_flags: nil}
end
types = %{erl_flags: :string}
cast({data, types}, attrs, [:erl_flags])
end
@impl true @impl true
def render(assigns) do def render(assigns) do
~H""" ~H"""
<div class="flex-col space-y-5"> <div id={@id} class="flex-col space-y-5">
<p class="text-gray-700"> <p class="text-gray-700">
Start a new local Elixir node to evaluate code. Whenever you reconnect this runtime, Start a new local Elixir node to evaluate code. Whenever you reconnect this runtime,
a fresh node is started. a fresh node is started.
</p> </p>
<.button phx-click="init" phx-target={@myself} disabled={@runtime_status == :connecting}> <.form
<%= label(@runtime, @runtime_status) %> :let={f}
</.button> for={@changeset}
as={:data}
phx-submit="init"
phx-change="validate"
phx-target={@myself}
autocomplete="off"
spellcheck="false"
>
<div id={"#{@id}-advanced"} class="mb-6">
<div
class="flex items-center gap-0.5 text-gray-700 font-medium cursor-pointer"
phx-click={JS.toggle(to: "##{@id}-advanced [data-toggle]")}
>
<span>Advanced configuration</span>
<.remix_icon icon="arrow-down-s-line" class="text-xl hidden" data-toggle />
<.remix_icon icon="arrow-right-s-line" class="text-xl" data-toggle />
</div>
<div class="mt-2 flex flex-col space-y-4 hidden" data-toggle>
<.text_field field={f[:erl_flags]} label="Erl flags" />
</div>
</div>
<.button type="submit" disabled={@runtime_status == :connecting or not @changeset.valid?}>
<%= label(@changeset, @runtime_status) %>
</.button>
</.form>
</div> </div>
""" """
end end
defp label(%Runtime.Standalone{}, :connecting), do: "Connecting..." defp label(changeset, runtime_status) do
defp label(%Runtime.Standalone{}, :connected), do: "Reconnect" reconnecting? = changeset.valid? and changeset.data == apply_changes(changeset)
defp label(_runtime, _runtime_status), do: "Connect"
case {reconnecting?, runtime_status} do
{true, :connected} -> "Reconnect"
{true, :connecting} -> "Connecting..."
_ -> "Connect"
end
end
@impl true @impl true
def handle_event("init", _params, socket) do def handle_event("validate", %{"data" => data}, socket) do
runtime = Runtime.Standalone.new() changeset =
Session.set_runtime(socket.assigns.session.pid, runtime) socket.assigns.runtime
Session.connect_runtime(socket.assigns.session.pid) |> changeset(data)
{:noreply, socket} |> Map.replace!(:action, :validate)
{:noreply, assign(socket, changeset: changeset)}
end
def handle_event("init", %{"data" => data}, socket) do
socket.assigns.runtime
|> changeset(data)
|> apply_action(:insert)
|> case do
{:ok, data} ->
runtime = Runtime.Standalone.new(erl_flags: data.erl_flags)
Session.set_runtime(socket.assigns.session.pid, runtime)
Session.connect_runtime(socket.assigns.session.pid)
{:noreply, socket}
{:error, changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end end
end end

View file

@ -907,8 +907,8 @@ defmodule LivebookWeb.SessionLiveTest do
|> render_click() |> render_click()
view view
|> element("#runtime-settings-modal button", "Connect") |> element("#runtime-settings-modal form")
|> render_click() |> render_submit(%{data: %{}})
assert_receive {:operation, {:set_runtime, _pid, %Runtime.Standalone{}}} assert_receive {:operation, {:set_runtime, _pid, %Runtime.Standalone{}}}
assert_receive {:operation, {:runtime_connected, _pid, %Runtime.Standalone{} = runtime}} assert_receive {:operation, {:runtime_connected, _pid, %Runtime.Standalone{} = runtime}}