Commit 176ff440 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Change updating of clients after publishing

parent 001dd869
...@@ -17,11 +17,13 @@ def publish_deployment(sender, instance=None, created=False, **kwargs): ...@@ -17,11 +17,13 @@ def publish_deployment(sender, instance=None, created=False, **kwargs):
message = json.dumps(DeploymentSerializer(instance).data) message = json.dumps(DeploymentSerializer(instance).data)
if PubSubConnection().publish_by_service( confirmed, online_sites = PubSubConnection().publish_by_service(
instance.service, message): instance.service, message)
if confirmed:
# delivery successful # delivery successful
instance.service.site.client_updated() for site in online_sites:
print('Client got update for service {}'.format(instance.service)) print('Client for site {} got update for service {}'.format(
else: site,
print('Client failed to receive update for service {}'.format( instance.service,
instance.service)) ))
site.client_updated()
...@@ -34,26 +34,31 @@ class PubSubConnection: ...@@ -34,26 +34,31 @@ class PubSubConnection:
def service_routing_key(self, service): def service_routing_key(self, service):
return 'service.' + service.name return 'service.' + service.name
def is_client_connected(self, client): def online_clients(self, service):
return False rabbitmq = RabbitMQInstance()
return [s
for s in service.site.all()
if rabbitmq.is_client_connected(s)]
def publish_by_service(self, service, message): def publish_by_service(self, service, message):
for site in service.site.all(): online_clients = self.online_clients(service)
self.publish_by_service_and_site(service, site, message) if len(online_clients) > 0:
print("Online clients for service {}: {}".format(
def publish_by_service_and_site(self, service, site, message): service,
if not RabbitMQInstance().is_client_connected(site): online_clients,
print('Client for site {} is not online'.format(site)) ))
return False else:
print("No clients online for service {}".format(
print('Client for site {} is online'.format(site)) service
))
return
self.connect() self.connect()
print('Sent deployment update for service {} sent to client of site {}' print('Sent deployment update for service {}'.format(
.format(service, site) service
) ))
# True if the client acked the message # True if all the clients acked the message
delivery_confirmed = self.channel.basic_publish( delivery_confirmed = self.channel.basic_publish(
exchange=self.exchange_name, exchange=self.exchange_name,
routing_key=self.service_routing_key(service), routing_key=self.service_routing_key(service),
...@@ -62,4 +67,4 @@ class PubSubConnection: ...@@ -62,4 +67,4 @@ class PubSubConnection:
) )
self.disconnect() self.disconnect()
return delivery_confirmed return delivery_confirmed, online_clients
...@@ -66,7 +66,7 @@ class Site(models.Model): ...@@ -66,7 +66,7 @@ class Site(models.Model):
return self.name return self.name
def client_updated(self): def client_updated(self):
print("Client of site {} got updated".format(self.name)) print("Client for site {} got updated".format(self.name))
self.last_fetch = make_aware(datetime.now()) self.last_fetch = make_aware(datetime.now())
self.save() self.save()
......
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