Capture log from processes outside Livebook supervision (#1235)

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
aar2dee2 2022-06-23 17:09:27 +05:30 committed by GitHub
parent fcf3015c87
commit 0c04f659c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View file

@ -94,9 +94,12 @@ defmodule Livebook.Runtime.ErlDist.LoggerGLBackend do
end
defp log_event(level, msg, ts, md, gl, state) do
output = format_event(level, msg, ts, md, state)
if io_proxy?(gl) do
output = format_event(level, msg, ts, md, state)
async_io(gl, output)
else
send(Livebook.Runtime.ErlDist.NodeManager, {:orphan_log, output})
end
end
@ -105,7 +108,7 @@ defmodule Livebook.Runtime.ErlDist.LoggerGLBackend do
info[:dictionary][:"$initial_call"] == {Livebook.Runtime.Evaluator.IOProxy, :init, 1}
end
defp async_io(device, output) when is_pid(device) do
def async_io(device, output) when is_pid(device) do
send(device, {:io_request, self(), make_ref(), {:put_chars, :unicode, output}})
end

View file

@ -151,6 +151,11 @@ defmodule Livebook.Runtime.ErlDist.NodeManager do
end
end
def handle_info({:orphan_log, _output} = message, state) do
for pid <- state.runtime_servers, do: send(pid, message)
{:noreply, state}
end
def handle_info(_message, state), do: {:noreply, state}
@impl true

View file

@ -201,7 +201,8 @@ defmodule Livebook.Runtime.ErlDist.RuntimeServer do
smart_cell_definitions_module:
Keyword.get(opts, :smart_cell_definitions_module, Kino.SmartCell),
extra_smart_cell_definitions: Keyword.get(opts, :extra_smart_cell_definitions, []),
memory_timer_ref: nil
memory_timer_ref: nil,
last_evaluator: nil
}}
end
@ -244,6 +245,15 @@ defmodule Livebook.Runtime.ErlDist.RuntimeServer do
{:noreply, finish_scan_binding(ref, state)}
end
def handle_info({:orphan_log, output}, state) do
with %{} = evaluator <- state.last_evaluator,
{:group_leader, io_proxy} <- Process.info(evaluator.pid, :group_leader) do
ErlDist.LoggerGLBackend.async_io(io_proxy, output)
end
{:noreply, state}
end
def handle_info(_message, state), do: {:noreply, state}
defp handle_down_evaluator(state, {:DOWN, _, :process, pid, reason}) do
@ -338,7 +348,7 @@ defmodule Livebook.Runtime.ErlDist.RuntimeServer do
opts
)
{:noreply, state}
{:noreply, %{state | last_evaluator: state.evaluators[container_ref]}}
end
def handle_cast({:forget_evaluation, {container_ref, evaluation_ref}}, state) do