From 339510da225c581a30184e2882e4f8394fe8f57f Mon Sep 17 00:00:00 2001 From: Emilio Junior Francischetti Date: Fri, 6 Dec 2024 12:41:16 +0100 Subject: [PATCH] [COART-181]: Check Gracefully termination in all our applications (#211) --- CHANGELOG.md | 12 +++++++++++- Dockerfile | 6 +++--- README.md | 14 ++++++++++++++ docker-compose.yml | 28 +++++++++++++++++++++------- entrypoint | 15 --------------- lib/amqp/helper.ex | 14 +++++++++++++- mix.exs | 2 +- test/helper_test.exs | 27 +++++++++++++++++++++++++++ 8 files changed, 90 insertions(+), 28 deletions(-) delete mode 100755 entrypoint diff --git a/CHANGELOG.md b/CHANGELOG.md index 3103907..c44a438 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,15 @@ and this project adheres to --- +## [6.1.2] - 2024-12-02 + +### Added + +- ([#211](https://github.com/primait/amqpx/pull/211)) Allow to skip the DLK check for a + list of dead letter queues + +--- + ## [6.1.1] - 2024-12-02 ### Added @@ -88,7 +97,8 @@ and this project adheres to - ([#129](https://github.com/primait/amqpx/pull/)) Default binding for DLX queues instead of wildcard -[Unreleased]: https://github.com/primait/amqpx/compare/6.1.1...HEAD +[Unreleased]: https://github.com/primait/amqpx/compare/6.1.2...HEAD +[6.1.2]: https://github.com/primait/amqpx/compare/6.1.1...6.1.2 [6.1.1]: https://github.com/primait/amqpx/compare/6.1.0...6.1.1 [6.1.0]: https://github.com/primait/amqpx/compare/6.0.4...6.1.0 [6.0.4]: https://github.com/primait/amqpx/compare/6.0.3...6.0.4 diff --git a/Dockerfile b/Dockerfile index 862e51d..4d1eeb6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ -FROM public.ecr.aws/prima/elixir:1.12.0-1 +FROM public.ecr.aws/prima/elixir:1.14.2-5 WORKDIR /code USER app -COPY ["entrypoint", "/entrypoint"] +RUN mix local.hex --force && \ + mix local.rebar --force -ENTRYPOINT ["/entrypoint"] diff --git a/README.md b/README.md index 534c4a6..21d4301 100644 --- a/README.md +++ b/README.md @@ -245,3 +245,17 @@ defmodule Myapp.HandleRejectionConsumer do end end ``` + +## Local Environment + +### Test suite + +In order to run the test suite, you need to startup the docker compose and jump into it with: +``` +docker compose run --service-ports console bash +``` + +and run the test suite with: +``` +mix test +``` diff --git a/docker-compose.yml b/docker-compose.yml index c0942ab..2fc48ca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,18 @@ version: "2" services: - web: + console: build: . volumes: - - .:/$PWD - - ~/.cache:/home/app/.cache - working_dir: $PWD - links: - - rabbit - - rabbit_two + - .:/code:delegated + working_dir: /code + stdin_open: true + tty: true + command: bash + depends_on: + rabbit: + condition: service_started + rabbit_two: + condition: service_healthy rabbit: image: rabbitmq:3-management @@ -18,6 +22,11 @@ services: RABBITMQ_DEFAULT_PASS: amqpx ports: - "23555:15672" + healthcheck: + test: "rabbitmq-diagnostics -q ping" + interval: 10s + timeout: 5s + retries: 3 rabbit_two: image: rabbitmq:3-management @@ -27,3 +36,8 @@ services: RABBITMQ_DEFAULT_PASS: amqpx ports: - "23556:15672" + healthcheck: + test: "rabbitmq-diagnostics -q ping" + interval: 10s + timeout: 5s + retries: 3 diff --git a/entrypoint b/entrypoint deleted file mode 100755 index 5ca4a31..0000000 --- a/entrypoint +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash -# source /setup_common.sh -# source /decrypt_secrets.sh - -if [ "$1" == "mix" ]; then - exec "$@" -elif [ -n "$1" ]; then - sh -c "$@" -else - mix deps.get - mix ecto.setup - mix phx.server - - trap : TERM INT; sleep infinity & wait -fi diff --git a/lib/amqp/helper.ex b/lib/amqp/helper.ex index adf8592..1dcbc40 100644 --- a/lib/amqp/helper.ex +++ b/lib/amqp/helper.ex @@ -3,6 +3,8 @@ defmodule Amqpx.Helper do Helper functions """ + require Logger + alias Amqpx.{Exchange, Queue} def manager_supervisor_configuration(config) do @@ -92,7 +94,14 @@ defmodule Amqpx.Helper do end def setup_dead_lettering(_channel, %{queue: dlq, exchange: "", routing_key: bad_dlq}) do - raise "If x-dead-letter-exchange is an empty string, x-dead-letter-routing-key should be '#{dlq}' instead of '#{bad_dlq}'" + msg = + "If x-dead-letter-exchange is an empty string, x-dead-letter-routing-key should be '#{dlq}' instead of '#{bad_dlq}'" + + if Enum.member?(skip_dead_letter_routing_key_check_for(), bad_dlq) do + Logger.warn(msg) + else + raise msg + end end def setup_dead_lettering(channel, %{queue: dlq, exchange: exchange, routing_key: routing_key}) do @@ -188,4 +197,7 @@ defmodule Amqpx.Helper do start: {Amqpx.SignalHandler, :start_link, []} } ] + + defp skip_dead_letter_routing_key_check_for, + do: Application.get_env(:amqpx, :skip_dead_letter_routing_key_check_for, []) end diff --git a/mix.exs b/mix.exs index 0ad897e..20e41fe 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule Amqpx.MixProject do [ app: :amqpx, name: "amqpx", - version: "6.1.1", + version: "6.1.2", elixir: "~> 1.14", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :production, diff --git a/test/helper_test.exs b/test/helper_test.exs index e59e85b..a29086b 100644 --- a/test/helper_test.exs +++ b/test/helper_test.exs @@ -120,6 +120,33 @@ defmodule HelperTest do end end + test "bad configuration with empty dead letter exchange and routing key is not a blocking error if the check is disabled", + meta do + queue_name = rand_name() + routing_key_name = rand_name() + exchange_name = rand_name() + wrong_dead_letter_key = rand_name() + + Application.put_env(:amqpx, :skip_dead_letter_routing_key_check_for, [wrong_dead_letter_key]) + + :ok = + Helper.declare(meta[:chan], %{ + exchanges: [ + %{name: exchange_name, opts: [durable: true], routing_keys: [routing_key_name], type: :topic} + ], + opts: [ + durable: true, + arguments: [ + {"x-dead-letter-exchange", :longstr, ""}, + {"x-dead-letter-routing-key", :longstr, wrong_dead_letter_key} + ] + ], + queue: queue_name + }) + + Application.put_env(:amqpx, :skip_dead_letter_routing_key_check_for, []) + end + defp rand_name do :crypto.strong_rand_bytes(8) |> Base.encode64() end