diff --git a/apps/core/lib/core/domain/invoker.ex b/apps/core/lib/core/domain/invoker.ex index 4d05a10f..043866b8 100644 --- a/apps/core/lib/core/domain/invoker.ex +++ b/apps/core/lib/core/domain/invoker.ex @@ -45,17 +45,20 @@ 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 @@ -63,13 +66,8 @@ defmodule Core.Domain.Invoker do {: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()) :: diff --git a/apps/core/test/core/integration/invoke_test.exs b/apps/core/test/core/integration/invoke_test.exs index 3b85d59c..96ab9ed0 100644 --- a/apps/core/test/core/integration/invoke_test.exs +++ b/apps/core/test/core/integration/invoke_test.exs @@ -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", @@ -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} diff --git a/apps/core/test/core_web/integration/controllers/function_controller_test.exs b/apps/core/test/core_web/integration/controllers/function_controller_test.exs index 6ed5a7bf..64e615ee 100644 --- a/apps/core/test/core_web/integration/controllers/function_controller_test.exs +++ b/apps/core/test/core_web/integration/controllers/function_controller_test.exs @@ -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