def _consume_queues(self, ch: amqp.Channel) -> None: self.consuming = True for queue in self.queues: logger.debug("basic_consume: %s", queue) ch.basic_consume(queue=queue, consumer_tag=self._consumer_tag(queue), callback=self._process_message)
def _send_reply( self, reply_to: str, channel: amqp.Channel, result: Any, exc_info: ExcInfoType, ) -> None: logger.debug("Sending reply result=%r", result) reply = {'result': result} if exc_info: reply['exception'] = self._exc_info_dict(exc_info) try: body = json.dumps(reply) except Exception as e: logger.error('Cannot serialize result as JSON: %s', e) exc_info = sys.exc_info() reply = { 'result': None, 'exception': self._exc_info_dict(exc_info) } body = json.dumps(reply) msg = amqp.Message(body=body) channel.basic_publish(msg, exchange="", routing_key=reply_to)
def publish(channel: amqp.Channel, message_number: int, mandatory=True): _log.info('publishing message %s', message_number) try: channel.basic_publish( amqp.Message( body=json.dumps({'pyamqp': message_number}), content_type='application/json', delivery_mode=2, ), exchange='', routing_key='myqueue', mandatory=mandatory, # timeout=2.0, ) except socket.timeout: _log.exception('timeout. Closing channel to prevent message to get into the queue.') # If message already arrived to the server, but basic.ack from server didn't arrive in time, # closing channel cannot prevent message from being delivered to the queue. channel.close() raise
def _send_reply( self, reply_to: str, channel: amqp.Channel, result: Any, exc_info: ExcInfoType, ) -> None: logger.debug("Sending reply result=%r", result) reply = {'result': result} if exc_info: reply['exception'] = self._exc_info_dict(exc_info) try: body = json.dumps(reply) except Exception as e: logger.error('Cannot serialize result as JSON: %s', e) exc_info = sys.exc_info() reply = {'result': None, 'exception': self._exc_info_dict(exc_info)} body = json.dumps(reply) msg = amqp.Message(body=body) channel.basic_publish(msg, exchange="", routing_key=reply_to)
def _cancel_queues(self, ch: amqp.Channel) -> None: self.consuming = False for queue in self.queues: logger.debug("basic_cancel: %s", queue) ch.basic_cancel(self._consumer_tag(queue))
def _declare_queues(self, ch: amqp.Channel) -> None: for queue in self.queues: logger.debug("queue_declare: %s", queue) ch.queue_declare(queue=queue, durable=True, auto_delete=False)
def sink(channel:amqp.Channel, source:[('msg', 'queue')]) -> "IO ()": """Amqp sink""" for msg, queue in ((amqp.Message(msg), queue) for msg, queue in source): channel.basic_publish(msg, routing_key=queue)
def sink(channel: amqp.Channel, source: [('msg', 'queue')]) -> "IO ()": """Amqp sink""" for msg, queue in ((amqp.Message(msg), queue) for msg, queue in source): channel.basic_publish(msg, routing_key=queue)