2020-07-27 17:58:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import pika
|
|
|
|
import json
|
|
|
|
|
|
|
|
from sentry_sdk.integrations.logging import LoggingIntegration
|
2020-10-20 01:00:04 +02:00
|
|
|
from sentry_sdk import start_transaction
|
2020-07-27 17:58:48 +02:00
|
|
|
import sentry_sdk
|
|
|
|
|
2020-10-06 00:41:54 +02:00
|
|
|
from magic_doer import MagicDoer
|
2020-07-27 17:58:48 +02:00
|
|
|
|
|
|
|
|
2020-10-05 22:19:25 +02:00
|
|
|
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
|
2020-07-28 18:10:19 +02:00
|
|
|
|
2020-10-20 01:00:04 +02:00
|
|
|
with start_transaction(op="cnn-classification-service", name="classify-soundfile"):
|
|
|
|
results = MagicDoer.run_everything(msg) # <- This is where the magic happens
|
2020-10-05 22:19:25 +02:00
|
|
|
|
|
|
|
channel.basic_publish(
|
|
|
|
exchange=os.environ['PIKA_OUTPUT_EXCHANGE'],
|
|
|
|
routing_key='classification-result',
|
|
|
|
body=json.dumps(results).encode("utf-8")
|
|
|
|
)
|
2020-07-27 17:58:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-10-05 22:19:25 +02:00
|
|
|
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
|
|
|
|
)
|
2020-07-27 17:58:48 +02:00
|
|
|
|
|
|
|
SENTRY_DSN = os.environ.get("SENTRY_DSN")
|
|
|
|
if SENTRY_DSN:
|
|
|
|
sentry_logging = LoggingIntegration(
|
|
|
|
level=logging.DEBUG, # Capture info and above as breadcrumbs
|
|
|
|
event_level=logging.ERROR # Send errors as events
|
|
|
|
)
|
|
|
|
sentry_sdk.init(
|
|
|
|
dsn=SENTRY_DSN,
|
|
|
|
integrations=[sentry_logging],
|
2020-10-19 22:30:00 +02:00
|
|
|
traces_sample_rate=1.0,
|
2020-07-27 17:58:48 +02:00
|
|
|
send_default_pii=True,
|
|
|
|
release=os.environ.get('RELEASE_ID', 'test'),
|
2020-10-19 22:30:00 +02:00
|
|
|
environment=os.environ.get('RELEASEMODE', 'dev'),
|
|
|
|
_experiments={"auto_enabling_integrations": True}
|
2020-07-27 17:58:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
logging.info("Connecting to MQ service...")
|
|
|
|
connection = pika.BlockingConnection(pika.connection.URLParameters(os.environ['PIKA_URL']))
|
|
|
|
channel = connection.channel()
|
2020-10-24 00:29:47 +02:00
|
|
|
channel.exchange_declare(exchange=os.environ['PIKA_INPUT_EXCHANGE'], exchange_type='direct')
|
2020-07-27 17:58:48 +02:00
|
|
|
|
|
|
|
queue_declare_result = channel.queue_declare(queue='', exclusive=True)
|
|
|
|
queue_name = queue_declare_result.method.queue
|
|
|
|
|
2020-10-24 00:29:47 +02:00
|
|
|
channel.queue_bind(exchange=os.environ['PIKA_INPUT_EXCHANGE'],routing_key='feature', queue=queue_name)
|
2020-10-06 00:41:54 +02:00
|
|
|
|
2020-07-27 17:58:48 +02:00
|
|
|
channel.basic_consume(queue=queue_name, on_message_callback=message_callback, auto_ack=True)
|
|
|
|
|
|
|
|
logging.info("Connection complete! Listening to messages...")
|
|
|
|
try:
|
|
|
|
channel.start_consuming()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.info("SIGINT Received! Stopping stuff...")
|
|
|
|
channel.stop_consuming()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-28 18:10:19 +02:00
|
|
|
main()
|