Skip to content

Commit

Permalink
fix: start dedicated process for runtime logging
Browse files Browse the repository at this point in the history
Fixes #92
  • Loading branch information
mhanberg committed Jul 6, 2023
1 parent 5753246 commit a76e7cf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
14 changes: 7 additions & 7 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ defmodule NextLS do
extensions = Keyword.get(args, :extensions, [NextLS.ElixirExtension])
cache = Keyword.fetch!(args, :cache)
symbol_table = Keyword.fetch!(args, :symbol_table)
{:ok, logger} = DynamicSupervisor.start_child(dynamic_supervisor, {NextLS.Logger, lsp: lsp})

{:ok,
assign(lsp,
exit_code: 1,
documents: %{},
refresh_refs: %{},
cache: cache,
logger: logger,
symbol_table: symbol_table,
task_supervisor: task_supervisor,
dynamic_supervisor: dynamic_supervisor,
Expand Down Expand Up @@ -269,7 +271,11 @@ defmodule NextLS do
{:ok, runtime} =
DynamicSupervisor.start_child(
lsp.assigns.dynamic_supervisor,
{NextLS.Runtime, extension_registry: lsp.assigns.extension_registry, working_dir: working_dir, parent: self()}
{NextLS.Runtime,
extension_registry: lsp.assigns.extension_registry,
working_dir: working_dir,
parent: self(),
logger: lsp.assigns.logger}
)

Process.monitor(runtime)
Expand Down Expand Up @@ -447,12 +453,6 @@ defmodule NextLS do
{:noreply, assign(lsp, runtime: nil)}
end

def handle_info({:log, message}, lsp) do
GenLSP.log(lsp, "[NextLS] " <> String.trim(message))

{:noreply, lsp}
end

def handle_info(message, lsp) do
GenLSP.log(lsp, "[NextLS] Unhanded message: #{inspect(message)}")
{:noreply, lsp}
Expand Down
22 changes: 22 additions & 0 deletions lib/next_ls/logger.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule NextLS.Logger do
use GenServer

def start_link(arg) do
GenServer.start_link(__MODULE__, arg, Keyword.take(arg, [:name]))
end

def log(server, msg), do: GenServer.cast(server, {:log, :log, msg})
def error(server, msg), do: GenServer.cast(server, {:log, :error, msg})
def info(server, msg), do: GenServer.cast(server, {:log, :info, msg})
def warning(server, msg), do: GenServer.cast(server, {:log, :warning, msg})

def init(args) do
lsp = Keyword.fetch!(args, :lsp)
{:ok, %{lsp: lsp}}
end

def handle_cast({:log, type, msg}, state) do
apply(GenLSP, type, [state.lsp, "[NextLS] #{msg}" |> String.trim()])
{:noreply, state}
end
end
11 changes: 6 additions & 5 deletions lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule NextLS.Runtime do
sname = "nextls-runtime-#{System.system_time()}"
working_dir = Keyword.fetch!(opts, :working_dir)
parent = Keyword.fetch!(opts, :parent)
logger = Keyword.fetch!(opts, :logger)
extension_registry = Keyword.fetch!(opts, :extension_registry)

port =
Expand Down Expand Up @@ -81,15 +82,15 @@ defmodule NextLS.Runtime do
with {:ok, host} <- :inet.gethostname(),
node <- :"#{sname}@#{host}",
true <- connect(node, port, 120) do
send(parent, {:log, "Connected to node #{node}"})
NextLS.Logger.log(logger, "Connected to node #{node}")

:next_ls
|> :code.priv_dir()
|> Path.join("monkey/_next_ls_private_compiler.ex")
|> then(&:rpc.call(node, Code, :compile_file, [&1]))
|> tap(fn
{:badrpc, :EXIT, {error, _}} ->
send(parent, {:log, error})
NextLS.Logger.error(logger, error)

_ ->
:ok
Expand All @@ -103,7 +104,7 @@ defmodule NextLS.Runtime do
end
end)

{:ok, %{port: port, parent: parent, errors: nil, extension_registry: extension_registry}}
{:ok, %{port: port, logger: logger, parent: parent, errors: nil, extension_registry: extension_registry}}
end

@impl GenServer
Expand Down Expand Up @@ -143,12 +144,12 @@ defmodule NextLS.Runtime do
end

def handle_info({port, {:data, data}}, %{port: port} = state) do
send(state.parent, {:log, data})
NextLS.Logger.log(state.logger, data)
{:noreply, state}
end

def handle_info({port, other}, %{port: port} = state) do
send(state.parent, {:log, other})
NextLS.Logger.log(state.logger, other)
{:noreply, state}
end

Expand Down
21 changes: 16 additions & 5 deletions test/next_ls/runtime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ defmodule NextLs.RuntimeTest do
Task.start_link(fn ->
recv = fn recv ->
receive do
{:log, msg} ->
Logger.debug(msg)
msg ->
Logger.debug(inspect(msg))
end

recv.(recv)
Expand All @@ -41,7 +41,11 @@ defmodule NextLs.RuntimeTest do

test "returns the response in an ok tuple", %{logger: logger, cwd: cwd} do
start_supervised!({Registry, keys: :unique, name: RuntimeTestRegistry})
pid = start_supervised!({Runtime, working_dir: cwd, parent: logger, extension_registry: RuntimeTestRegistry})

pid =
start_supervised!(
{Runtime, working_dir: cwd, parent: self(), logger: logger, extension_registry: RuntimeTestRegistry}
)

Process.link(pid)

Expand All @@ -52,7 +56,11 @@ defmodule NextLs.RuntimeTest do

test "call returns an error when the runtime is node ready", %{logger: logger, cwd: cwd} do
start_supervised!({Registry, keys: :unique, name: RuntimeTestRegistry})
pid = start_supervised!({Runtime, working_dir: cwd, parent: logger, extension_registry: RuntimeTestRegistry})

pid =
start_supervised!(
{Runtime, working_dir: cwd, parent: self(), logger: logger, extension_registry: RuntimeTestRegistry}
)

Process.link(pid)

Expand All @@ -63,7 +71,10 @@ defmodule NextLs.RuntimeTest do
start_supervised!({Registry, keys: :unique, name: RuntimeTestRegistry})

capture_log(fn ->
pid = start_supervised!({Runtime, working_dir: cwd, parent: logger, extension_registry: RuntimeTestRegistry})
pid =
start_supervised!(
{Runtime, working_dir: cwd, parent: self(), logger: logger, extension_registry: RuntimeTestRegistry}
)

Process.link(pid)

Expand Down

0 comments on commit a76e7cf

Please sign in to comment.