From 01fa54e6b64d62686979256fb373624dc2bee08b Mon Sep 17 00:00:00 2001 From: marcsello Date: Mon, 5 Oct 2020 22:19:25 +0200 Subject: [PATCH] Fixed transmitting on the same channel --- .../classifier_cache.py | 0 cnn_classification_service/main.py | 29 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 cnn_classification_service/classifier_cache.py diff --git a/cnn_classification_service/classifier_cache.py b/cnn_classification_service/classifier_cache.py new file mode 100644 index 0000000..e69de29 diff --git a/cnn_classification_service/main.py b/cnn_classification_service/main.py index 84b7d5f..5d0fdec 100644 --- a/cnn_classification_service/main.py +++ b/cnn_classification_service/main.py @@ -11,21 +11,28 @@ import sentry_sdk from magic_doer import run_everything -def message_callback(ch, method, properties, body): - msg = json.loads(body.decode('utf-8')) - results = run_everything(msg) +def message_callback(channel, method, properties, body): + try: + msg = json.loads(body.decode('utf-8')) + except (UnicodeDecodeError, json.JSONDecodeError) as e: + logging.warning(f"Invalid message recieved: {e}") + return - # TODO: Ez azért elég gettó, de legalább csatlakozik - connection = pika.BlockingConnection(pika.connection.URLParameters(os.environ['PIKA_URL'])) - channel = connection.channel() - channel.exchange_declare(exchange=os.environ['PIKA_OUTPUT_EXCHANGE'], exchange_type='fanout') - channel.basic_publish(exchange=os.environ['PIKA_OUTPUT_EXCHANGE'], routing_key='classification-result', - body=json.dumps(results).encode("utf-8")) + results = run_everything(msg) # <- This is where the magic happens + + channel.basic_publish( + exchange=os.environ['PIKA_OUTPUT_EXCHANGE'], + routing_key='classification-result', + body=json.dumps(results).encode("utf-8") + ) def main(): - logging.basicConfig(stream=sys.stdout, format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s", - level=logging.DEBUG if '--debug' in sys.argv else logging.INFO) + logging.basicConfig( + stream=sys.stdout, + format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s", + level=logging.DEBUG if '--debug' in sys.argv else logging.INFO + ) SENTRY_DSN = os.environ.get("SENTRY_DSN") if SENTRY_DSN: