Search code examples
pythonrabbitmqpika

RabbitMQ Message/Topic Receiver Crashing when RabbitMQ is not running


I am using python and pika on linux OS Environment. Message/Topic Receiver keeps crashing when RabbitMQ is not running. I am wondering is there a way to keep the Message/Topic Receiver running when RabbitMQ is not because RabbitMQ would not be on the same Virtual Machine as the Message/Topic Receiver.

This cover if RabbitMQ crashes for some reason but the Message/Topic Receiver should keep running. Saving having to start/restart the Message/Topic Receiver again.


Solution

  • As far as I understand "Message/Topic Reciever" in your case is the consumer. You are responsible to make an application in such a way that it will catch an exception if it is trying to connect to the not running RabbitMQ.

    for example:

    creds = pika.PlainCredentials(**creds)                             
    params = pika.ConnectionParameters(credentials=creds,                   
                                       **conn_params)                  
    try:                                                                    
        connection = pika.BlockingConnection(params)                   
        LOG.info("Connection to Rabbit was established")                    
        return connection                                              
    except (ProbableAuthenticationError, AuthenticationError):              
        LOG.error("Authentication Failed", exc_info=True)                   
    except ProbableAccessDeniedError:                                       
        LOG.error("The Virtual Host configured wrong!", exc_info=True)         
    except ChannelClosed:                                                   
        LOG.error("ChannelClosed error", exc_info=True)                     
    except AMQPConnectionError:                                             
        LOG.error("RabbitMQ server is down or Host Unreachable")            
        LOG.error("Connection attempt timed out!")                          
        LOG.error("Trying to re-connect to RabbitMQ...")                    
        time.sleep(reconnection_interval)  
        # <here goes your reconnection logic >                             
    

    And as far as making sure that you Rabbit server is always up and running:

    • you can create a cluster make you queue durable, HA
    • install some type of supervision (let say monit or supervisord) and configure it to check rabbit process. for example:

      check process rabbitmq with pidfile /var/run/rabbitmq/pid                       
        start program = "/etc/init.d/rabbitmq-server stop"                            
        stop program = "/etc/init.d/rabbitmq-server start"                            
        if 3 restarts within 5 cycles then alert