Skip to content

Commit 421157e

Browse files
authored
Merge pull request #192 from giusdp/main
Check if functions exists before picking worker
2 parents 68587cc + 4921c6f commit 421157e

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

apps/core/lib/core/domain/invoker.ex

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,29 @@ defmodule Core.Domain.Invoker do
4545
def invoke(ivk_pars) do
4646
Logger.info("Invoker: invocation for #{ivk_pars.module}/#{ivk_pars.function} requested")
4747

48-
# could be {:error, :no_workers}
49-
with {:ok, worker} <- Nodes.worker_nodes() |> Scheduler.select() do
50-
case invoke_without_code(worker, ivk_pars) do
51-
{:error, :code_not_found} ->
52-
worker
53-
|> invoke_with_code(ivk_pars)
54-
|> save_to_sinks(ivk_pars.module, ivk_pars.function)
55-
56-
res ->
57-
save_to_sinks(res, ivk_pars.module, ivk_pars.function)
48+
if Functions.exists_in_mod?(ivk_pars.function, ivk_pars.module) do
49+
with {:ok, worker} <- Nodes.worker_nodes() |> Scheduler.select() do
50+
case invoke_without_code(worker, ivk_pars) do
51+
{:error, :code_not_found} ->
52+
worker
53+
|> invoke_with_code(ivk_pars)
54+
|> save_to_sinks(ivk_pars.module, ivk_pars.function)
55+
56+
res ->
57+
save_to_sinks(res, ivk_pars.module, ivk_pars.function)
58+
end
5859
end
60+
else
61+
{:error, :not_found}
5962
end
6063
end
6164

6265
@spec invoke_without_code(atom(), InvokeParams.t()) ::
6366
{:ok, InvokeResult.t()} | {:error, :code_not_found} | invoke_errors()
6467
def invoke_without_code(worker, ivk) do
6568
Logger.debug("Invoker: invoking #{ivk.module}/#{ivk.function} without code")
66-
67-
if Functions.exists_in_mod?(ivk.function, ivk.module) do
68-
# send invocation without code
69-
Commands.send_invoke(worker, ivk.function, ivk.module, ivk.args)
70-
else
71-
{:error, :not_found}
72-
end
69+
# send invocation without code
70+
Commands.send_invoke(worker, ivk.function, ivk.module, ivk.args)
7371
end
7472

7573
@spec invoke_with_code(atom(), InvokeParams.t()) ::

apps/core/test/core/integration/invoke_test.exs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ defmodule Core.InvokeTest do
6565
assert Invoker.invoke(pars) == {:error, {:exec_error, "some error"}}
6666
end
6767

68-
test "invoke should return {:error, :no_workers} when no workers are found" do
68+
test "invoke should return {:error, :no_workers} when no workers are found", %{
69+
function: function,
70+
module: module
71+
} do
6972
expected = {:error, :no_workers}
70-
assert Invoker.invoke(%{"module" => "_", "function" => "test"}) == expected
73+
pars = %InvokeParams{function: function.name, module: module.name}
74+
assert Invoker.invoke(pars) == expected
7175
end
7276

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

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

90-
assert Invoker.invoke(%{"function" => "test"}) == {:error, :no_workers}
98+
pars = %InvokeParams{function: function.name, module: module.name}
99+
assert Invoker.invoke(pars) == {:error, :no_workers}
91100
end
92101

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

96105
pars = %InvokeParams{function: "no_fun", module: "some module"}
97106
assert Invoker.invoke(pars) == {:error, :not_found}

apps/core/test/core_web/integration/controllers/function_controller_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,13 @@ defmodule CoreWeb.FunctionControllerTest do
422422
end
423423

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

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

0 commit comments

Comments
 (0)