diff --git a/lib/trento/discovery.ex b/lib/trento/discovery.ex index f6c416818d..646f4119aa 100644 --- a/lib/trento/discovery.ex +++ b/lib/trento/discovery.ex @@ -36,11 +36,7 @@ defmodule Trento.Discovery do :ok <- dispatch(commands) do :ok else - {:error, reason} = error -> - Logger.error("Failed to handle discovery event: #{inspect(reason)}") - store_discarded_discovery_event(event, inspect(reason)) - - error + result -> handle_not_dispatched(event, result) end end @@ -164,24 +160,47 @@ defmodule Trento.Discovery do defp do_handle(_), do: {:error, :unknown_discovery_type} - @spec dispatch(command | [command]) :: :ok | {:error, any} + @spec dispatch(command | [command]) :: :ok | {:error, any} | {:ignore, any} defp dispatch(commands) when is_list(commands) do Enum.reduce(commands, :ok, fn command, acc -> - case {commanded().dispatch(command), acc} do - {:ok, :ok} -> - :ok - - {{:error, error}, :ok} -> - {:error, [error]} - - {{:error, error}, {:error, errors}} -> - {:error, [error | errors]} - end + result = commanded().dispatch(command) + aggregate_dispatch_results(acc, result) end) end defp dispatch(command), do: commanded().dispatch(command) + defp aggregate_dispatch_results(:ok, :ok), do: :ok + defp aggregate_dispatch_results(:ok, {:ignore, reason}), do: {:ignore, [reason]} + defp aggregate_dispatch_results(:ok, {:error, reason}), do: {:error, [reason]} + defp aggregate_dispatch_results({:ignore, reasons}, :ok), do: {:ignore, reasons} + + defp aggregate_dispatch_results({:ignore, reasons}, {:ignore, reason}), + do: {:ignore, [reason | reasons]} + + defp aggregate_dispatch_results({:ignore, reasons}, {:error, reason}), + do: {:error, [reason | reasons]} + + defp aggregate_dispatch_results({:error, reasons}, :ok), do: {:errors, reasons} + + defp aggregate_dispatch_results({:error, reasons}, {:ignore, reason}), + do: {:error, [reason | reasons]} + + defp aggregate_dispatch_results({:error, reasons}, {:error, reason}), + do: {:error, [reason | reasons]} + defp commanded, do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter] + + defp handle_not_dispatched(event, {:ignore, reasons}) do + Logger.warning("Ignored discovery event: #{inspect(reasons)}") + store_discarded_discovery_event(event, inspect(reasons)) + :ok + end + + defp handle_not_dispatched(event, {:error, reasons} = error) do + Logger.error("Failed to handle discovery event: #{inspect(reasons)}") + store_discarded_discovery_event(event, inspect(reasons)) + error + end end diff --git a/lib/trento/infrastructure/commanded/middleware/enrich.ex b/lib/trento/infrastructure/commanded/middleware/enrich.ex index 3ff00e60d6..faf2a60b09 100644 --- a/lib/trento/infrastructure/commanded/middleware/enrich.ex +++ b/lib/trento/infrastructure/commanded/middleware/enrich.ex @@ -26,6 +26,11 @@ defmodule Trento.Infrastructure.Commanded.Middleware.Enrich do pipeline |> respond({:error, reason}) |> halt + + {:ignore, reason} -> + pipeline + |> respond({:ignore, reason}) + |> halt end end diff --git a/lib/trento/infrastructure/commanded/middleware/enrich_register_application_instance.ex b/lib/trento/infrastructure/commanded/middleware/enrich_register_application_instance.ex index cbc84d8e99..851c85f644 100644 --- a/lib/trento/infrastructure/commanded/middleware/enrich_register_application_instance.ex +++ b/lib/trento/infrastructure/commanded/middleware/enrich_register_application_instance.ex @@ -36,7 +36,7 @@ defimpl Trento.Infrastructure.Commanded.Middleware.Enrichable, }} nil -> - {:error, :database_not_registered} + {:ignore, :database_not_registered} end end end diff --git a/test/trento/infrastructure/commanded/middleware/enrich_register_application_instance_test.exs b/test/trento/infrastructure/commanded/middleware/enrich_register_application_instance_test.exs index a9efe1efb6..6d6505de16 100644 --- a/test/trento/infrastructure/commanded/middleware/enrich_register_application_instance_test.exs +++ b/test/trento/infrastructure/commanded/middleware/enrich_register_application_instance_test.exs @@ -68,7 +68,7 @@ defmodule Trento.Infrastructure.Commanded.Middleware.EnrichRegisterApplicationIn health: :passing ) - assert {:error, :database_not_registered} = Enrichable.enrich(command, %{}) + assert {:ignore, :database_not_registered} = Enrichable.enrich(command, %{}) end test "should return an error if the database was not found" do @@ -85,6 +85,6 @@ defmodule Trento.Infrastructure.Commanded.Middleware.EnrichRegisterApplicationIn health: :passing ) - assert {:error, :database_not_registered} = Enrichable.enrich(command, %{}) + assert {:ignore, :database_not_registered} = Enrichable.enrich(command, %{}) end end diff --git a/test/trento_web/controllers/v1/discovery_controller_test.exs b/test/trento_web/controllers/v1/discovery_controller_test.exs index b55ca90722..0c824c7149 100644 --- a/test/trento_web/controllers/v1/discovery_controller_test.exs +++ b/test/trento_web/controllers/v1/discovery_controller_test.exs @@ -41,13 +41,13 @@ defmodule TrentoWeb.V1.DiscoveryControllerTest do |> json_response(202) end - test "collect action discards application instance registrations when the associated database does not exists", + test "collect action discards and store events when the command fails", %{conn: conn} do body = load_discovery_event_fixture("sap_system_discovery_application") expect(Trento.Commanded.Mock, :dispatch, fn _ -> - {:error, :any_error} + {:error, :any_reason} end) %{status: status} = @@ -59,7 +59,29 @@ defmodule TrentoWeb.V1.DiscoveryControllerTest do [discarded_event] = Discovery.get_discarded_discovery_events(1) - assert %DiscardedDiscoveryEvent{payload: ^body, reason: "[:any_error]"} = + assert %DiscardedDiscoveryEvent{payload: ^body, reason: "[:any_reason]"} = + discarded_event + end + + test "collect action discards and store events when the command is not dispatched", + %{conn: conn} do + body = + load_discovery_event_fixture("sap_system_discovery_application") + + expect(Trento.Commanded.Mock, :dispatch, fn _ -> + {:ignore, :any_reason} + end) + + %{status: status} = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/collect", body) + + assert status == 202 + + [discarded_event] = Discovery.get_discarded_discovery_events(1) + + assert %DiscardedDiscoveryEvent{payload: ^body, reason: "[:any_reason]"} = discarded_event end end