1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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()
|