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