def run_pulse_listener(username, password, timeout, no_send):
    """Run a Pulse message queue listener."""
    connection = Connection(
        hostname='pulse.mozilla.org',
        port=5671,
        ssl=True,
        userid=username,
        password=password,
    )

    # Connect and pass in our own low value for retries so the connection
    # fails fast if there is a problem.
    connection.ensure_connection(
        max_retries=1
    )  # Retries must be >=1 or it will retry forever.

    with closing(connection):
        hgpush_exchange = Exchange(config.PULSE_EXCHANGE, 'topic', channel=connection)

        # Pulse queue names need to be prefixed with the username
        queue_name = f'queue/{username}/{config.PULSE_QUEUE_NAME}'
        queue = Queue(
            queue_name,
            exchange=hgpush_exchange,
            routing_key=config.PULSE_QUEUE_ROUTING_KEY,
            durable=True,
            exclusive=False,
            auto_delete=False,
            channel=connection,
        )

        # Passing passive=True will assert that the exchange exists but won't
        #  try to declare it.  The Pulse server forbids declaring exchanges.
        hgpush_exchange.declare(passive=True)

        # Queue.declare() also declares the exchange, which isn't allowed by
        # the Pulse server. Use the low-level Queue API to only declare the
        # queue itself.
        queue.queue_declare()
        queue.queue_bind()

        callback = partial(process_push_message, no_send=no_send)

        # Pass auto_declare=False so that Consumer does not try to declare the
        # exchange.  Declaring exchanges is not allowed by the Pulse server.
        with connection.Consumer(
            queue, callbacks=[callback], auto_declare=False
        ) as consumer:

            if no_send:
                log.info('transmission of ping data has been disabled')
                log.info('message acks has been disabled')

            log.info('reading messages')
            try:
                connection.drain_events(timeout=timeout)
            except socket.timeout:
                log.info('message queue is empty, nothing to do')

    log.info('done')
Exemple #2
0
    def test_declare_but_no_exchange(self):
        q = Queue('a')
        q.queue_declare = Mock()
        q.queue_bind = Mock()
        q.exchange = None

        q.declare()
        q.queue_declare.assert_called_with(False, passive=False)
Exemple #3
0
    def test_declare_but_no_exchange(self):
        q = Queue('a')
        q.queue_declare = Mock()
        q.queue_bind = Mock()
        q.exchange = None

        q.declare()
        q.queue_declare.assert_called_with(False, passive=False)
Exemple #4
0
    def test_declare__no_declare(self):
        q = Queue('a', no_declare=True)
        q.queue_declare = Mock()
        q.queue_bind = Mock()
        q.exchange = None

        q.declare()
        q.queue_declare.assert_not_called()
        q.queue_bind.assert_not_called()
Exemple #5
0
    def test_declare__no_declare(self):
        q = Queue('a', no_declare=True)
        q.queue_declare = Mock()
        q.queue_bind = Mock()
        q.exchange = None

        q.declare()
        self.assertFalse(q.queue_declare.called)
        self.assertFalse(q.queue_bind.called)
Exemple #6
0
    def test_declare__no_declare(self):
        q = Queue('a', no_declare=True)
        q.queue_declare = Mock()
        q.queue_bind = Mock()
        q.exchange = None

        q.declare()
        q.queue_declare.assert_not_called()
        q.queue_bind.assert_not_called()
Exemple #7
0
    def test_declare__no_declare(self):
        q = Queue('a', no_declare=True)
        q.queue_declare = Mock()
        q.queue_bind = Mock()
        q.exchange = None

        q.declare()
        self.assertFalse(q.queue_declare.called)
        self.assertFalse(q.queue_bind.called)
def run_pulse_listener(
    username,
    password,
    exchange_name,
    queue_name,
    routing_key,
    timeout,
    no_send,
    worker_args=None,
    empty_queue_callback=None,
):
    """Run a Pulse message queue listener."""
    connection = build_connection(password, username)

    # Connect and pass in our own low value for retries so the connection
    # fails fast if there is a problem.
    connection.ensure_connection(
        max_retries=1)  # Retries must be >=1 or it will retry forever.

    with closing(connection):
        hgpush_exchange = Exchange(exchange_name, "topic", channel=connection)

        # Pulse queue names need to be prefixed with the username
        queue_name = f"queue/{username}/{queue_name}"
        queue = Queue(
            queue_name,
            exchange=hgpush_exchange,
            routing_key=routing_key,
            durable=True,
            exclusive=False,
            auto_delete=False,
            channel=connection,
        )

        # Passing passive=True will assert that the exchange exists but won't
        #  try to declare it.  The Pulse server forbids declaring exchanges.
        hgpush_exchange.declare(passive=True)

        # Queue.declare() also declares the exchange, which isn't allowed by
        # the Pulse server. Use the low-level Queue API to only declare the
        # queue itself.
        queue.queue_declare()
        queue.queue_bind()

        callback = partial(process_push_message,
                           no_send=no_send,
                           extra_data=worker_args)

        # Pass auto_declare=False so that Consumer does not try to declare the
        # exchange.  Declaring exchanges is not allowed by the Pulse server.
        with connection.Consumer(queue,
                                 callbacks=[callback],
                                 auto_declare=False):

            if no_send:
                log.info("transmission of monitoring data has been disabled")
                log.info("message acks has been disabled")

            log.info("reading messages")
            try:
                connection.drain_events(timeout=timeout)
            except socket.timeout:
                log.info("message queue is empty")
                if empty_queue_callback:
                    empty_queue_callback()
            except HaltQueueProcessing:
                log.debug("queue processing halted by consumer")

    log.info("done")