When RabbitMQ consumers is idle for a day, then the consumer is removed automatically

Viewed 422

I am facing below issue regarding RabbitMQ consumers.

Issue

When RabbitMQ consumers are idle for a day, then the consumer is removed automatically (maybe the connection is closed). The exact issue is that the consumer script is still running. If consumer is closed then the below consumer script should be stopped. The consumer is removed silently and script is still running so I am not notified when consumer is closed unless I look into RabbitMQ console manually. I have some mechanism that notifies when script is stopped running, but here consumer is removed and script is still running and not able to be notified when consumer is removed automatically if it is idle for a day.

run.py (consumer script)

import pika
def main():
    parameters = pika.URLParameters(amqp_url)
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    channel.queue_declare(queue='hello')
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=False)
    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
if __name__ == '__main__':
    main()

requirements.txt

pika=1.1.0

Metadata

pika version: 1.1.0

heartbeat: 60s

script is deployed in Kubernetes docker container

Project structure

  • requirements.txt
  • run.py

DockerFile

FROM python:3.9-slim
WORKDIR /consumer
ADD requirements.txt ./
RUN pip install --upgrade pip==20.2.4
RUN pip install -r requirements.txt
ADD . ./ 
CMD python run.py
1 Answers

It sounds to me like once this container is started, it is creating or altering some resource or state in a way that closing the container does not undo. It sounds to me like it could be a Pika issue. I think your problem could very much be connected to this post. In this case, some sort of close_connection() is likely firing but simply failing to what is expected.

Another problem could be that proper closing instructions are not being sent before the close is complete. In that case, you may need to send those instructions yourself. I'd recommend checking out the PreStop Kubernetes LifeCycle Hook which would help you control the container shutdown process.

Related