Example #1
0
class DT(threading.Thread):
    def __init__(self, connection, queue_name):
        super(DT, self).__init__()
        self.queue_name = queue_name
        self.connection = connection
        self.exchange = Exchange(name="event1", type="direct")
        self._queue = entity.Queue(queue_name,
                                   exchange=self.exchange,
                                   routing_key=queue_name)
        self.queue = SimpleQueue(self.connection, self._queue)

    def run(self):
        while True:
            try:
                self.main_loop()
            except:
                logging.exception("Unknown Exception: ")

    def main_loop(self):
        messages = []
        print("inside", self.queue_name)
        messages.append(self.queue.get(block=True))
        print("after ")
        messages[-1].ack()

        # Check if there is anymore messages
        try:
            while True:
                messages.append(self.queue.get(block=True, timeout=1))
                messages[-1].ack()
        except Empty:
            pass

        for message in messages:
            print(message.payload)
Example #2
0
class KombuQueueReceiver(KombuMessageQueue):
    def __init__(self, dc, exchange, exchange_type, queue, binding_key,
                 **kwargs):
        if self.class_name is None:
            self.class_name = "KombuQueueReceiver"
        KombuMessageQueue.__init__(self, dc, exchange, exchange_type, queue,
                                   binding_key, **kwargs)
        self.queue_obj = SimpleQueue(
            self.channel,
            self.Queue_obj,
        )

        self.uuid = gen_uuid()
        if not self.queue:
            raise RuntimeError("queue is required")

    def start_consuming(self):
        pass

    def dump_cache(self):
        return self.queue_obj.clear()

    def stop_consuming(self):
        self.queue_obj.close()

    @recover_connect
    def get_cached_number(self):
        return self.queue_obj.qsize()

    @recover_connect
    def get_nowait(self):
        try:
            log_message = self.queue_obj.get_nowait()
        except Empty:
            return None
        log_message.ack()
        #        p = log_message.payload # deserialized data.
        #        if isinstance(p, buffer):
        #            p = unicode(p)
        return log_message.body

    @recover_connect
    def get(self, block=True, timeout=None):
        #        logger.debug("Enter Message get callback method..")
        try:
            log_message = self.queue_obj.get(block=True, timeout=timeout)
            #            logger.debug("get return : %s, dir: %s, type: %s", log_message, dir(log_message), type(log_message))
            debug_message(log_message)
        except Empty:
            logger.debug("Empty error when get @todo infos..")
            raise Empty("{cn}[{id:#x}] is raise Empty when fetch.")


#        p = log_message.payload # deserialized data.
        log_message.ack()  # remove message from queue
        #        if isinstance(p, buffer):
        #            p = unicode(p)
        return log_message.body

    def __del__(self):
        if not self.closed:
            logger.debug(
                "=============== {class_name}[{id:#x}] Enter closing... ".
                format(class_name=self.class_name, id=id(self)))
            self.close()

    def close(self):
        logger.debug(
            "{cn}[{id:#x}] is calling KombuQueueReceiver close method".format(
                cn=self.class_name, id=id(self)))
        try:
            logger.debug(
                "{cn}[{id:#x}] is about to closing.. Queue size: {qsize}".
                format(cn=self.class_name,
                       id=id(self),
                       qsize=self.get_cached_number()))
            # clean Rabbitmq Queue
            self.dump_cache()
            logger.debug(
                "{cn}[{id:#x}] is cleared. Queue size: {qsize}".format(
                    cn=self.class_name,
                    id=id(self),
                    qsize=self.get_cached_number()))
            # stop all active consumers
            self.stop_consuming()
            # how to measure stop all consumers @todo
            # delete Rabbitmq Queue
            r = self.Queue_obj.delete()
            logger.debug(
                "{cn}[{id:#x}] delete Queue[{qid:#x}] return: {r}".format(
                    cn=self.class_name,
                    id=id(self),
                    qid=id(self.Queue_obj),
                    r=r))
        except ChannelError as e:
            if '404' not in e.message:
                logger.error("Error when {cn}[{id:#x}] close: {msg}".format(
                    cn=self.class_name,
                    id=id(self),
                    msg=traceback.format_exc()))
        KombuMessageQueue.close(self)