models.py 1.06 KB
Newer Older
1

2
3
4
5
6
7
from django.dispatch import receiver
from django.db.models.signals import post_save
from .. import models
from .serializers import DeploymentSerializer
import pika
import json
8
9


Lukas Burgey's avatar
Lukas Burgey committed
10
11
12
13
class Deployments:
    pass


14
15
16
17
@receiver(post_save, sender=models.Deployment)
def publish_deployment(sender, instance=None, created=False, **kwargs):
    if instance is None:
        return
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    connection = pika.BlockingConnection(
            pika.ConnectionParameters(host='localhost'))

    channel = connection.channel()
    exchange_name = 'deployments'

    channel.exchange_declare(exchange=exchange_name,
                             exchange_type='topic')

    message = json.dumps(DeploymentSerializer(instance).data)

    # we only use the service as routing key
    routing_keys = ['service.' + instance.service.name]

    for routing_key in routing_keys:
        channel.basic_publish(exchange=exchange_name,
                              routing_key=routing_key,
                              body=message)

        print(" [x] Sent %r:%r" % (routing_key, message))

    connection.close()