Exemple #1
0
class QueueConsumerWorker:
    def __init__(self):
        self.channels = {
            "queue1": "method_for_queue1",
            "queue2": "method_for_queue1"
        }
        self.broker = Broker()
        self.broker.declare_queues([ele for ele in self.channels])

    def method_for_queue1(self, message):
        logger.info(f"called method associate with queue1")
        try:
            return ServiceClass(
            ).some_awesome_method_to_deal_with_messages_from_queue_one(message)

        except Exception as e:
            logger.error(f"Some s**t happened")
            return False

    def method_for_queue2(self, message):
        logger.info(f"called method associate with queue2")
        try:
            return ServiceClass(
            ).some_awesome_method_to_deal_with_messages_from_queue_one(message)

        except Exception as e:
            logger.error(f"Some s**t happened")
            return False

    def run(self) -> bool:
        try:
            for channel, function in self.channels.items():
                message = self.broker.get_next_message(channel)
                if message is not None:
                    logger.info(
                        f"Queue {channel} - Received message {message}")
                    if not getattr(self, function)(message):
                        self.broker.send_message(message, f"TOPIC/{channel}")
                        #operation failed, return message to queue
                        return 1

            return 0
        except Exception as e:
            logger.error(e, exc_info=True)
            return 1  # return cause
Exemple #2
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This code simulates some content producer who is adding information to the queue.
"""
import json
from datetime import datetime
from time import sleep

from broker.broker import Broker

if __name__ == "__main__":
    channels = ["queue1"]
    broker = Broker()
    broker.declare_queues(channels)

    while True:
        data = {"date_time": str(datetime.now())}
        broker.send_message(message=data,
                            topic="TOPIC/queue1",
                            routing_key=None)
        sleep(5)