예제 #1
0
class RabbitMQHandler(object):
    def __init__(self, connection_string, exchange):
        self._connection = BrokerConnection(connection_string)
        self._connections = {self._connection}  # set of connection for the heartbeat
        self._exchange = Exchange(exchange, durable=True, delivry_mode=2, type='topic')
        monitor_heartbeats(self._connections)

    @retry(wait_fixed=200, stop_max_attempt_number=3)
    def publish(self, item, contributor):
        with self._connection.channel() as channel:
            with Producer(channel) as producer:
                producer.publish(item, exchange=self._exchange, routing_key=contributor, declare=[self._exchange])

    def info(self):
        return self._connection.info()

    def listen_load_realtime(self, queue_name, max_retries=10):
        log = logging.getLogger(__name__)

        route = 'task.load_realtime.*'
        log.info('listening route {} on exchange {}...'.format(route, self._exchange))
        rt_queue = Queue(queue_name, routing_key=route, exchange=self._exchange, durable=False)
        RTReloader(connection=self._connection,
                   rpc_queue=rt_queue,
                   exchange=self._exchange,
                   max_retries=max_retries).run()
예제 #2
0
class RabbitMQHandler(object):
    def __init__(self,
                 connection_string,
                 exchange_name,
                 exchange_type="topic"):
        self._connection = BrokerConnection(connection_string)
        self._connections = {self._connection
                             }  # set of connection for the heartbeat
        self._exchange = Exchange(exchange_name,
                                  durable=True,
                                  delivery_mode=2,
                                  type=exchange_type,
                                  auto_delete=False,
                                  no_declare=False)
        monitor_heartbeats(self._connections)

    @retry(wait_fixed=200, stop_max_attempt_number=3)
    def publish(self, item, contributor_id):
        with self._connection.channel() as channel:
            with Producer(channel) as producer:
                producer.publish(
                    item,
                    exchange=self._exchange,
                    routing_key=contributor_id,
                    declare=[self._exchange],
                    content_type="plain/text",
                )

    def info(self):
        info = self._connection.info()
        info.pop("password", None)
        return info

    def check_connection(self, force=False):
        """
        Trying to connect is the best way to check that the connection works
        if force is set to True, we will force the connection again
        """
        try:
            # we need to refresh the connection to be notified as soon as rabbitmq stopped working
            if force:
                self._connection._establish_connection()
            self._connection.ensure_connection(interval_start=0, max_retries=1)
            return True
        except Exception:
            return False

    def connect(self):
        self._connection.connect()

    def close(self):
        for c in self._connections:
            c.release()

    def listen_load_realtime(self, queue_name, max_retries=10):
        log = logging.getLogger(__name__)

        route = "task.load_realtime.*"
        log.info("listening route {} on exchange {}...".format(
            route, self._exchange))
        rt_queue = Queue(queue_name,
                         routing_key=route,
                         exchange=self._exchange,
                         durable=False)
        RTReloader(connection=self._connection,
                   rpc_queue=rt_queue,
                   exchange=self._exchange,
                   max_retries=max_retries).run()