Beispiel #1
0
def get_connection(amqp_url, heartbeat=60.0):
    global connection
    if connection:
        if connection.connected:
            return connection
#        else:
#            # block whole process?
#            # connectied first then close rabbitmq, then test if block, then start rabbitmq, then test if recover
#            return connection.ensure_connection(
#                errback=connection_ensure_fail,
#                max_retries=3)

    logger.debug("Babel connect amqp_url: %s, try heartbeat: %s", amqp_url,
                 heartbeat)
    if LOCAL_DEBUG:
        connection = Connection(amqp_url,
                                heartbeat=heartbeat,
                                transport="memory")
    else:
        connection = Connection(
            amqp_url,
            heartbeat=heartbeat)  #transport_options=dict(heartbeat=60.0))
    if logger.level <= logging.DEBUG:
        connection._logger = True


#    connection.connect()
    connection.ensure_connection(
        errback=lambda exc, interval: logger.debug(
            "Connect exception: %s, will sleep :%s s", exc, interval),
        max_retries=10,
        callback=lambda: logger.debug("retry once..."))
    logger.debug("Babel connection Info: %s" % connection.info())
    if connection.transport_cls == "amqp" and supports_librabbitmq():
        logger.debug("Babel transport way is %s", "librabbitmq")
    else:
        logger.debug("Babel transport way is %s", connection.transport_cls)
    logger.info(
        """Babel connection support heartbeat? %s ,server heartbeat: %s,
                client heartbeat:%s, final heartbeat: %s, heartbeat interval: %s"""
        % (connection.supports_heartbeats,
           getattr(connection.connection, "server_heartbeat", None),
           getattr(connection.connection, "client_heartbeat",
                   None), getattr(connection.connection, "heartbeat",
                                  None), connection.get_heartbeat_interval()))

    #    global connection_consumers
    #    if connection_consumers:
    #        for c in connection_consumers.itervalues():
    #            c._reconnect()
    #            c.start_consuming()

    return connection