models.py 949 Bytes
Newer Older
1

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


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


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

18
19
20
21
22
23
24
25
26
27
28
    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)

29
30
31
32
    routing_key = 'service.' + instance.service.name
    channel.basic_publish(exchange=exchange_name,
                          routing_key=routing_key,
                          body=message)
33

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

    connection.close()