Example #1
0
    def consume_mails_per_hour(self):
        queue_manager = QueueManager(self.queue_service_url)
        queue_channel = queue_manager.channel()
        queue_channel.queue_declare(queue=QueueManager.QUEUE_NAME,
                                    arguments={'x-max-priority': 255})

        for i in range(0, self.mails_per_hour):
            method, properties, body = queue_channel.basic_get(
                QueueManager.QUEUE_NAME, auto_ack=True)
            if body is None:
                continue

            payload = QueuePayload(**json.loads(body))

            QUEUE_CONSUMER_BY_TEMPLATE.labels(payload.template.value).inc()
            QUEUE_CONSUMER_BY_PRIORITY.labels(properties.priority).inc()

            self.mail_manager.send_mail(payload.recipient, payload.mail_params,
                                        payload.template)
        queue_manager.close()
                        dest='number_of_messages',
                        default=1,
                        help='The number of messages to generate',
                        type=int)
    args = parser.parse_args()

    producer = Producer()
    for i in range(0, args.number_of_messages):
        template = random.randint(1, 5)
        if template == 1:
            payload = producer.produce_confirmation_to_mentor()
        elif template == 2:
            payload = producer.produce_confirmation_to_mentored()
        elif template == 3:
            payload = producer.produce_notify_mentor_of_mentored_leaving()
        elif template == 4:
            payload = producer.produce_notify_subscribed_users_not_selected()
        else:
            payload = producer.produce_notify_subscribed_users_of_mentoring_canceled(
            )
        channel.basic_publish(exchange='',
                              routing_key=QueueManager.QUEUE_NAME,
                              body=json.dumps(payload.dict(), default=str),
                              properties=pika.BasicProperties(
                                  priority=payload.get_priority_value()))
        print(
            f'Message with priority {payload.get_priority_value()} and template {payload.template} sent!'
        )

    queue_manager.close()