Commit ef2b577f authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Refactor pubsub connection

parent eb982786
......@@ -2,8 +2,8 @@
from django.dispatch import receiver
from .. import models
from .serializers import DeploymentSerializer
import pika
import json
from .pubsub import PubSubConnection
class Deployments:
......@@ -15,22 +15,6 @@ def publish_deployment(sender, instance=None, created=False, **kwargs):
if instance is None:
return
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)
routing_key = 'service.' + instance.service.name
channel.basic_publish(exchange=exchange_name,
routing_key=routing_key,
body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()
PubSubConnection().publish_by_service(instance.service, message)
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
exchange_name = 'deployments'
channel.exchange_declare(
exchange=exchange_name, exchange_type='topic')
class PubSubConnection:
def __init__(self):
self.host = 'localhost'
self.exchange_name = 'deployments'
def connect(self):
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host=self.host))
self.channel = self.connection.channel()
self.channel.exchange_declare(
exchange=self.exchange_name,
exchange_type='topic')
def disconnect(self):
self.connection.close()
def service_routing_key(self, service):
return 'service.' + service.name
def publish_by_service(self, service, message):
self.connect()
self.channel.basic_publish(
exchange=self.exchange_name,
routing_key=self.service_routing_key(service),
body=message)
print(' [x] Sent {}:{}'.format(
self.service_routing_key(service), message))
self.disconnect()
......@@ -208,6 +208,7 @@ class SSHPublicKey(models.Model):
self.deleted = True
self.save()
# delete implies withdrawing the key from all clients
for deployment in self.deployments.all():
deployment.withdraw_key(self)
......@@ -230,7 +231,7 @@ class SSHPublicKey(models.Model):
# @receiver(m2m_changed, sender=SSHPublicKey)
# def delete_withdrawn_ssh_key(
# sender, instance=None, created=False, **kwargs):
#
#
# if (instance.deleted
# and not instance.deployments.exists()
# and not instance.withdrawn_deployments.exists()):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment