Catch RabbitMQ :heartbeat_timeout

Good evening,

I am working on a escript executable in plain elixir. I’m currently using RabbitMQ with the AMQP library which is great. I’m using rabbitMQ heartbeat to handle lack of connection. When i turn down my wifi connection there is a heartbeat_timeout and this is normal ! I just want to catch this message to avoid showing this to the user in his console, but i can’t figure it out. How can i do this ? The message in the console is below:

22:02:37.681 [error] GenServer #PID<0.257.0> terminating
** (stop) :heartbeat_timeout
Last message: :heartbeat_timeout
State: {:state, :amqp_network_connection, {:state, #Port<0.9883>, "client adress -> adress:5672", 10, #PID<0.260.0>, 131072, #PID<0.256.0>, :undefined, false}, #PID<0.259.0>, {:amqp_params_network, "1v62q9wtxu2sckso", "33oga51ho8yswo0gwswg44440kwc4k4okkksgg0kks4wkkcc08", "/", 'adress.com', 5672, 0, 0, 10, :infinity, :none, [&:amqp_auth_mechanisms.plain/3, &:amqp_auth_mechanisms.amqplain/3], [], []}, 0, [{"capabilities", :table, [{"publisher_confirms", :bool, true}, {"exchange_exchange_bindings", :bool, true}, {"basic.nack", :bool, true}, {"consumer_cancel_notify", :bool, true}, {"connection.blocked", :bool, true}, {"consumer_priorities", :bool, true}, {"authentication_failure_close", :bool, true}, {"per_consumer_qos", :bool, true}]}, {"cluster_name", :longstr, "rabbit@adress-production.localdomain"}, {"copyright", :longstr, "Copyright (C) 2007-2015 Pivotal Software, Inc."}, {"information", :longstr, "Licensed under the MPL.  See http://www.rabbitmq.com/"}, {"platform", :longstr, "Erlang/OTP"}, {"product", :longstr, "RabbitMQ"}, {"version", :longstr, "3.5.7"}], :none, false}

Did you try to use handle_info that will for sure grab this message?

def handle_info(anything, channel) do
  # do something with that
end

It should be at the bottom of your handle_info list.

I can’t remember if I was able to suppress this by defining a terminate/2 to match :heartbeat_timeout, but it’s worth a try:

def terminate(:heartbeat_timeout, _state), do: []

Of course I’ve tried the handle_info/2 and the def terminate(:heartbeat_timeout, _state), do: [] with no success… I don’t understand why it’s not working… It’s really important for the user experience, because the error message is scary.

Look at the Logger.Translator mechanism, which will allow you handle cases like this.

2 Likes

Thanks, so if i understand well, I should add a translator but add_translator({module, function :: atom}) :: :ok take a module and a function in parameter, how can i know the corresponding one ?

Here is a sample translator: https://github.com/elixir-plug/plug/blob/master/lib/plug/adapters/translator.ex

The idea is that you will be able to pattern match in certain cases and convert those to better error messages.