livebook/lib/live_book/runtime/attached.ex
Jonatan Kłosko 79e5c432b3
Move evaluation to a separate Elixir runtime (#20)
* Isolate evaluation in separate node for each session

* Start new remote upon first evaluation and handle nodedown

* Add UI for managing evaluation node, improve naming and structure

* Show runtime initialization errors and some fixes

* Improve standalone node initialization

* Correctly handle multiple sessions connecting to the same node

* Fix session tests concerning evaluation

* Documentation and some refactoring

* Various improvements

* Configure schedulers to get to sleep immediately after evaluation

* Move EvaluatorSpervisor into the Remote namespace

* Fix evaluators cleanup

* Add tests

* Improve flash messages

* Introduce remote genserver taking care of cleanup

* Redefine the Runtime protocol to serve as an interface for evaluation

* Cleanup operations

* Use reference for communication with a standalone node

* Use shortnames for distribution by default

* Update node configuration and make sure epmd is running

* Rename Remote to ErlDist
2021-02-11 12:42:17 +01:00

69 lines
1.8 KiB
Elixir

defmodule LiveBook.Runtime.Attached do
@moduledoc false
# A runtime backed by an Elixir node managed externally.
#
# Such node must be already started and available,
# LiveBook doesn't manage its lifetime in any way
# and only loads/unloads the necessary elements.
# The node can be an oridinary Elixir runtime,
# a mix project shell, a running release or anything else.
defstruct [:node]
@type t :: %__MODULE__{
node: node()
}
@doc """
Checks if the given node is available for use and initializes
it with LiveBook-specific modules and processes.
"""
@spec init(node()) :: {:ok, t()} | {:error, :unreachable | :already_in_use}
def init(node) do
case Node.ping(node) do
:pong ->
case LiveBook.Runtime.ErlDist.initialize(node) do
:ok ->
{:ok, %__MODULE__{node: node}}
{:error, :already_in_use} ->
{:error, :already_in_use}
end
:pang ->
{:error, :unreachable}
end
end
end
defimpl LiveBook.Runtime, for: LiveBook.Runtime.Attached do
alias LiveBook.Runtime.ErlDist
def connect(runtime) do
ErlDist.Manager.set_owner(runtime.node, self())
Process.monitor({ErlDist.Manager, runtime.node})
end
def disconnect(runtime) do
ErlDist.Manager.stop(runtime.node)
end
def evaluate_code(runtime, code, container_ref, evaluation_ref, prev_evaluation_ref \\ :initial) do
ErlDist.Manager.evaluate_code(
runtime.node,
code,
container_ref,
evaluation_ref,
prev_evaluation_ref
)
end
def forget_evaluation(runtime, container_ref, evaluation_ref) do
ErlDist.Manager.forget_evaluation(runtime.node, container_ref, evaluation_ref)
end
def drop_container(runtime, container_ref) do
ErlDist.Manager.drop_container(runtime.node, container_ref)
end
end