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

Support remote processes in Kino.Process #317

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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
56 changes: 36 additions & 20 deletions lib/kino/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ defmodule Kino.Process do

Kino.Process.app_tree(:logger, direction: :left_right)
"""
@spec app_tree(atom(), keyword()) :: Mermaid.t()
def app_tree(application, opts \\ []) when is_atom(application) do
@spec app_tree(atom() | {atom(), node()}, keyword()) :: Mermaid.t()
def app_tree(application, opts \\ []) do
{application, node} =
case application do
application when is_atom(application) -> {application, node()}
{application, node} when is_atom(application) and is_atom(node) -> {application, node}
end

{master, root_supervisor} =
case :application_controller.get_master(application) do
case :erpc.call(node, :application_controller, :get_master, [application]) do
:undefined ->
if Application.spec(application, :vsn) do
raise ArgumentError,
Expand All @@ -72,7 +78,7 @@ defmodule Kino.Process do
direction = direction_from_opts(opts)
edges = traverse_supervisor(root_supervisor)

{:dictionary, dictionary} = Process.info(root_supervisor, :dictionary)
{:dictionary, dictionary} = process_info(root_supervisor, :dictionary)
[ancestor] = dictionary[:"$ancestors"]

Mermaid.new("""
Expand Down Expand Up @@ -130,10 +136,26 @@ defmodule Kino.Process do

Kino.Process.sup_tree(MyApp.Supervisor, direction: :left_right)
"""
@spec sup_tree(supervisor(), keyword()) :: Mermaid.t()
@spec sup_tree(supervisor() | {supervisor(), node()}, keyword()) :: Mermaid.t()
def sup_tree(supervisor, opts \\ []) do
direction = direction_from_opts(opts)
edges = traverse_supervisor(supervisor)

supervisor_pid =
supervisor
|> case do
{name, node} -> :erpc.call(node, GenServer, :whereis, [name])
supervisor -> GenServer.whereis(supervisor)
end
|> case do
supervisor_pid when is_pid(supervisor_pid) ->
supervisor_pid

_ ->
raise ArgumentError,
"the provided identifier #{inspect(supervisor)} does not reference a running process"
end

edges = traverse_supervisor(supervisor_pid)

Mermaid.new("""
graph #{direction};
Expand Down Expand Up @@ -361,7 +383,7 @@ defmodule Kino.Process do

defp generate_participant_entry(pid, idx) do
try do
{:registered_name, name} = Process.info(pid, :registered_name)
{:registered_name, name} = process_info(pid, :registered_name)
"participant #{idx} AS #{module_or_atom_to_string(name)};"
rescue
_ -> "participant #{idx} AS #35;PID#{:erlang.pid_to_list(pid)};"
Expand Down Expand Up @@ -428,17 +450,7 @@ defmodule Kino.Process do
|> convert_direction()
end

defp traverse_supervisor(supervisor) do
supervisor =
case GenServer.whereis(supervisor) do
supervisor_pid when is_pid(supervisor_pid) ->
supervisor_pid

_ ->
raise ArgumentError,
"the provided identifier #{inspect(supervisor)} does not reference a running process"
end

defp traverse_supervisor(supervisor) when is_pid(supervisor) do
supervisor_children =
try do
Supervisor.which_children(supervisor)
Expand Down Expand Up @@ -525,7 +537,7 @@ defmodule Kino.Process do
defp traverse_links({rels, _idx, pid_keys}) do
rels_with_links =
Enum.reduce(pid_keys, rels, fn {pid, _idx}, rels_with_links ->
{:links, links} = Process.info(pid, :links)
{:links, links} = process_info(pid, :links)

Enum.reduce(links, rels_with_links, fn link_pid, acc ->
add_new_links_to_acc(pid_keys, pid, link_pid, acc)
Expand Down Expand Up @@ -585,7 +597,7 @@ defmodule Kino.Process do
end

display =
case Process.info(pid, :registered_name) do
case process_info(pid, :registered_name) do
{:registered_name, []} -> inspect(pid)
{:registered_name, name} -> module_or_atom_to_string(name)
end
Expand All @@ -599,4 +611,8 @@ defmodule Kino.Process do
rest -> rest
end
end

defp process_info(pid, spec) do
:erpc.call(node(pid), Process, :info, [pid, spec])
end
end
Loading