Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable heartbeats with pika and rabbitmq

I am using rabbitmq to facilitate some tasks from my rabbit server to my respective consumers. I have noticed that when I run some rather lengthy tests, 20+ minutes, my consumer will lose contact with the producer after it completes it's task. In my rabbit logs, I have seen the error

closing AMQP connection <0.14009.27> (192.168.101.2:64855 -> 
192.168.101.3:5672):
missed heartbeats from client, timeout: 60s

Also, I receive this error from pika

pika.exceptions.ConnectionClosed: (-1, "error(10054, 'An existing connection was forcibly closed by the remote host')")

I'm assuming this is due to this code right here and the conflict of heartbeats with the lengthy blocking connection time.

 self.connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.101.2', 5672, 'user', credentials))
    self.channel = self.connection.channel()
    self.channel.queue_declare(queue=self.tool,
                               arguments={'x-message-ttl': 1000,
                                             "x-dead-letter-exchange": "dlx",
                                             "x-dead-letter-routing-key": "dl",
                                             'durable': True})

Is there a proper way to increase the heartbeat time or how would I turn it off(would it be wise to) completely? Like I said, tests that are 20+ min seem to lead to a closedconnection error but I've ran plenty of tests from the 1-15 minute mark where everything is fine and the consumer client continues to wait for a message to be delivered.

like image 676
UCProgrammer Avatar asked Aug 08 '18 17:08

UCProgrammer


1 Answers

Please don't disable heartbeats. Instead, use Pika correctly. This means:

  • Use Pika version 0.12.0
  • Do your long-running task in a separate thread
  • When your task completes, use the add_callback_threadsafe method to schedule the basic_ack call.

Example code can be found here: link

I'm a RabbitMQ core team member and Pika maintainer so if you have further questions or issues, I recommend following up on either the pika-python or rabbitmq-users mailing list. Thanks!


NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

like image 116
Luke Bakken Avatar answered Oct 24 '22 07:10

Luke Bakken