aboutsummaryrefslogtreecommitdiffstats
path: root/webapp/rmq.py
blob: 80d418af884fb450805db6c0cbf219a6549dc5ff (plain) (blame)
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
import os
import pika

USERNAME      = 'guest'
PASSWORD      = 'guest'
HOST          = os.environ.get('RMQ_HOST', '172.17.0.2')
PORT          = 5672
EXCHANGE      = 'exchange'
EXCHANGE_TYPE = 'direct'
QUEUE         = 'text'
ROUTING_KEY   = 'bacon'

try:
    import docker
    client = docker.from_env()
    containers = client.containers.get('snekbox_pdrmq_1')
    print("Attempting to get rabbitmq host automatically")
    HOST = list(containers.attrs.get('NetworkSettings').get('Networks').values())[0]['IPAddress']
    print(f"found {HOST}")
except:
    pass

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\nhost:     {HOST}\nport:     {PORT}\nexchange: {EXCHANGE}\nqueue:    {QUEUE}""", flush=True)
        print(f"Sent: '{message}'")
    else:
        print("not delivered")

    connection.close()

#send('print("bacon is delicious")')