From c7ce4f2fa8ab159a3df4988313f0abdba119d258 Mon Sep 17 00:00:00 2001 From: "angelinatingaeva@yandex.ru" Date: Mon, 12 Feb 2024 02:11:42 +0500 Subject: [PATCH] refctor(rabbitmq): create fanout exchange_type --- rabbitmq/log.py | 16 ++++++++++++++++ rabbitmq/receive_log.py | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 rabbitmq/log.py create mode 100644 rabbitmq/receive_log.py diff --git a/rabbitmq/log.py b/rabbitmq/log.py new file mode 100644 index 0000000..6a2b458 --- /dev/null +++ b/rabbitmq/log.py @@ -0,0 +1,16 @@ +import sys + +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host="localhost"), +) +channel = connection.channel() + +channel.exchange_declare(exchange="logs", exchange_type="fanout") + +message = " ".join(sys.argv[1:]) or "info: Hello World!" +channel.basic_publish(exchange="logs", routing_key="", body=message) +print(f" [x] Sent {message}") + +connection.close() diff --git a/rabbitmq/receive_log.py b/rabbitmq/receive_log.py new file mode 100644 index 0000000..ee127b0 --- /dev/null +++ b/rabbitmq/receive_log.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +import os +import sys + +import pika + + +def main(): + connection = pika.BlockingConnection( + pika.ConnectionParameters(host="localhost"), + ) + channel = connection.channel() + + channel.exchange_declare(exchange="logs", exchange_type="fanout") + + result = channel.queue_declare(queue="", exclusive=True) + queue_name = result.method.queue + + channel.queue_bind(exchange="logs", queue=queue_name) + + def callback(ch, method, properties, body): + print(f" [x] {body.decode()}") + + print(" [*] Waiting for logs. To exit press CTRL+C") + channel.basic_consume( + queue=queue_name, + on_message_callback=callback, + auto_ack=True, + ) + + channel.start_consuming() + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + print("Interrupted") + try: + sys.exit(0) + except SystemExit: + os._exit(0) \ No newline at end of file