Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 15 additions & 17 deletions apps/core/lib/core/domain/invoker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,29 @@ defmodule Core.Domain.Invoker do
def invoke(ivk_pars) do
Logger.info("Invoker: invocation for #{ivk_pars.module}/#{ivk_pars.function} requested")

# could be {:error, :no_workers}
with {:ok, worker} <- Nodes.worker_nodes() |> Scheduler.select() do
case invoke_without_code(worker, ivk_pars) do
{:error, :code_not_found} ->
worker
|> invoke_with_code(ivk_pars)
|> save_to_sinks(ivk_pars.module, ivk_pars.function)

res ->
save_to_sinks(res, ivk_pars.module, ivk_pars.function)
if Functions.exists_in_mod?(ivk_pars.function, ivk_pars.module) do
with {:ok, worker} <- Nodes.worker_nodes() |> Scheduler.select() do
case invoke_without_code(worker, ivk_pars) do
{:error, :code_not_found} ->
worker
|> invoke_with_code(ivk_pars)
|> save_to_sinks(ivk_pars.module, ivk_pars.function)

res ->
save_to_sinks(res, ivk_pars.module, ivk_pars.function)
end
end
else
{:error, :not_found}
end
end

@spec invoke_without_code(atom(), InvokeParams.t()) ::
{:ok, InvokeResult.t()} | {:error, :code_not_found} | invoke_errors()
def invoke_without_code(worker, ivk) do
Logger.debug("Invoker: invoking #{ivk.module}/#{ivk.function} without code")

if Functions.exists_in_mod?(ivk.function, ivk.module) do
# send invocation without code
Commands.send_invoke(worker, ivk.function, ivk.module, ivk.args)
else
{:error, :not_found}
end
# send invocation without code
Commands.send_invoke(worker, ivk.function, ivk.module, ivk.args)
end

@spec invoke_with_code(atom(), InvokeParams.t()) ::
Expand Down
19 changes: 14 additions & 5 deletions apps/core/test/core/integration/invoke_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ defmodule Core.InvokeTest do
assert Invoker.invoke(pars) == {:error, {:exec_error, "some error"}}
end

test "invoke should return {:error, :no_workers} when no workers are found" do
test "invoke should return {:error, :no_workers} when no workers are found", %{
function: function,
module: module
} do
expected = {:error, :no_workers}
assert Invoker.invoke(%{"module" => "_", "function" => "test"}) == expected
pars = %InvokeParams{function: function.name, module: module.name}
assert Invoker.invoke(pars) == expected
end

test "invoke on node list with nodes other than workers should only use workers",
Expand All @@ -84,14 +88,19 @@ defmodule Core.InvokeTest do
assert Invoker.invoke(pars) == {:ok, :worker@localhost}
end

test "invoke on node list without workers should return {:error, no workers}" do
test "invoke on node list without workers should return {:error, no workers}",
%{
function: function,
module: module
} do
Core.Cluster.Mock |> Mox.expect(:all_nodes, fn -> [:core@somewhere] end)

assert Invoker.invoke(%{"function" => "test"}) == {:error, :no_workers}
pars = %InvokeParams{function: function.name, module: module.name}
assert Invoker.invoke(pars) == {:error, :no_workers}
end

test "invoke on a non-existent function should return {:error, :not_found}" do
Core.Cluster.Mock |> Mox.expect(:all_nodes, fn -> [:worker@localhost] end)
Core.Cluster.Mock |> Mox.expect(:all_nodes, 0, fn -> [:worker@localhost] end)

pars = %InvokeParams{function: "no_fun", module: "some module"}
assert Invoker.invoke(pars) == {:error, :not_found}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,13 @@ defmodule CoreWeb.FunctionControllerTest do
end

test "renders error when function does not exist", %{conn: conn} do
Core.Cluster.Mock |> Mox.expect(:all_nodes, fn -> [:worker@localhost] end)
Core.Cluster.Mock |> Mox.expect(:all_nodes, 0, fn -> [:worker@localhost] end)
conn = post(conn, Routes.function_path(conn, :invoke, "some_module", "no_function"))
assert response(conn, 404)
end

test "renders error when module does not exist", %{conn: conn} do
Core.Cluster.Mock |> Mox.expect(:all_nodes, fn -> [:worker@localhost] end)
Core.Cluster.Mock |> Mox.expect(:all_nodes, 0, fn -> [:worker@localhost] end)
conn = post(conn, Routes.function_path(conn, :invoke, "no_module", "some_function"))
assert response(conn, 404)
end
Expand Down