1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
17 class Notification(object):
19 This class can be used by the dashboard and the labs to exchange notifications about booking
20 events and pod status. It utilizes rabbitmq to communicate.
22 Notifications are associated to an event and to a topic.
24 [ 'booking_start', 'booking_stop', 'pod_status' ]
25 The topic is usually a POD name, ie:
29 def __init__(self, dashboard_url, verbose=False):
30 self.rabbitmq_broker = dashboard_url
31 self.verbose = verbose
34 self.connection = pika.BlockingConnection(pika.ConnectionParameters(
35 host=self.rabbitmq_broker))
36 self.channel = self.connection.channel()
38 self.channel.exchange_declare(exchange='notifications', type='topic')
40 self.result = self.channel.queue_declare(exclusive=True)
41 self.queue_name = self.result.method.queue
43 def register(self, function, event, regex):
45 Registers a function to be called for the specified event.
46 :param function: the function to register
47 :param event: the event type
48 :param regex: a regex to specify for wich topics the function will be called. Some
49 possible Expressions can be:
50 'Intel POD 2' : Intel POD 2
51 'Intel POD .*' : All Intel Pods
55 if event not in self._registry:
56 self._registry[event] = [(function, regex)]
58 self._registry[event].append((function, regex))
62 Start receiving notifications. This is a blocking operation, if a notification is received,
63 the registered functions will be called.
66 print('Start receiving Notifications. Keys: ', self._registry.keys())
67 self._receive_message(self._registry.keys())
69 def send(self, event, topic, content):
71 Send an event notification.
72 :param event: the event type
73 :param topic: the pod name
74 :param content: a JSON-serializable dictionary
81 self._send_message(message)
83 def _send_message(self, event):
84 routing_key = event['type']
85 message = json.dumps(event)
86 self.channel.basic_publish(exchange='notifications',
87 routing_key=routing_key,
89 properties=pika.BasicProperties(
90 content_type='application/json'
93 print(" [x] Sent %r:%r" % (routing_key, message))
95 def _receive_message(self, binding_keys):
96 for key in binding_keys:
97 self.channel.queue_bind(exchange='notifications',
98 queue=self.queue_name,
100 self.channel.basic_consume(self._message_callback,
101 queue=self.queue_name,
103 self.channel.start_consuming()
105 def _message_callback(self, ch, method, properties, body):
107 print(" [x] Got %r:%r" % (method.routing_key, body))
108 if method.routing_key not in self._registry:
110 for func, regex in self._registry[method.routing_key]:
111 message = json.loads(body.decode())
112 match = re.match(regex, message['topic'])