diff options
Diffstat (limited to 'runner')
| -rw-r--r-- | runner/config.py | 8 | ||||
| -rw-r--r-- | runner/consume.py | 68 | ||||
| -rw-r--r-- | runner/publish.py | 42 | ||||
| -rw-r--r-- | runner/test.py | 21 |
4 files changed, 139 insertions, 0 deletions
diff --git a/runner/config.py b/runner/config.py new file mode 100644 index 0000000..75b3c28 --- /dev/null +++ b/runner/config.py @@ -0,0 +1,8 @@ +USERNAME = 'guest' +PASSWORD = 'guest' +HOST = '172.17.0.2' +PORT = 5672 +EXCHANGE = 'exchange' +EXCHANGE_TYPE = 'direct' +QUEUE = 'text' +ROUTING_KEY = 'bacon' diff --git a/runner/consume.py b/runner/consume.py new file mode 100644 index 0000000..0e4d79a --- /dev/null +++ b/runner/consume.py @@ -0,0 +1,68 @@ +import pika +import traceback +import sys +from io import StringIO + +from config import ( + USERNAME, + PASSWORD, + HOST, + PORT, + EXCHANGE, + EXCHANGE_TYPE, + QUEUE, + ROUTING_KEY, +) + +def execute(snippet): + old_stdout = sys.stdout + redirected_output = sys.stdout = StringIO() + failed = False + try: + exec(snippet) + except Exception as e: + failed = e + finally: + sys.stdout = old_stdout + + if failed: + return failed + return redirected_output.getvalue() + + +def message_handler(ch, method, properties, body): + msg = body.decode('utf-8') + + # Execute code snippets here + print(f"incoming: {msg}", flush=True) + result = execute(msg) + print(result, flush=True) + + ch.basic_ack(delivery_tag = method.delivery_tag) + +def rabbitmq_consume(): + credentials = pika.PlainCredentials(USERNAME, PASSWORD) + connection = pika.BlockingConnection(pika.ConnectionParameters(HOST, PORT, '/', credentials)) + + channel = connection.channel() + channel.queue_declare(queue=QUEUE, durable=False) + channel.basic_qos(prefetch_count=1) + channel.basic_consume(message_handler, queue=QUEUE) + + try: + print(f"""Connecting to + host: {HOST} + port: {PORT} + exchange: {EXCHANGE} + queue: {QUEUE}""", flush=True) + + channel.start_consuming() + + except Exception: + exc = traceback.format_exc() + print(exc, flush=True) + + finally: + connection.close() + +rabbitmq_consume() diff --git a/runner/publish.py b/runner/publish.py new file mode 100644 index 0000000..fc18d03 --- /dev/null +++ b/runner/publish.py @@ -0,0 +1,42 @@ +import pika +from config import ( + USERNAME, + PASSWORD, + HOST, + PORT, + EXCHANGE, + EXCHANGE_TYPE, + QUEUE, + ROUTING_KEY, +) + +def send(message): + credentials = pika.PlainCredentials(USERNAME, PASSWORD) + connection = pika.BlockingConnection(pika.ConnectionParameters(HOST, PORT, '/', credentials)) + properties = pika.BasicProperties(content_type='text/plain', delivery_mode=1) + + channel = connection.channel() + channel.queue_declare(queue=QUEUE, durable=False) + channel.exchange_declare(exchange=EXCHANGE, exchange_type=EXCHANGE_TYPE) + channel.queue_bind(exchange=EXCHANGE, queue=QUEUE, routing_key=ROUTING_KEY) + + result = channel.basic_publish( + exchange=EXCHANGE, + routing_key=ROUTING_KEY, + body=message, + properties=properties + ) + + if result: + print(f"""Connecting to + host: {HOST} + port: {PORT} + exchange: {EXCHANGE} + queue: {QUEUE}""", flush=True) + print(f"Sent: '{message}'") + else: + print("not delivered") + + connection.close() + +send('print "bacon is delicious"') diff --git a/runner/test.py b/runner/test.py new file mode 100644 index 0000000..08f0441 --- /dev/null +++ b/runner/test.py @@ -0,0 +1,21 @@ +import sys +from io import StringIO + +def execute(snippet): + old_stdout = sys.stdout + redirected_output = sys.stdout = StringIO() + try: + exec(snippet) + except: + raise + finally: + sys.stdout = old_stdout + + return redirected_output.getvalue() + + +code = """ +i = [0,1,2] +for j in i: + print(j) +""" |