-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·45 lines (37 loc) · 1.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import pickle
import pika
import config
import metrics.meter as meter
import socket
class PostMan(object):
EXCHANGE = "rtop"
TYPE = 'fanout'
def __init__(self, instance_name, host, port=5672, login="guest", password="guest", emit_delay=20):
self.instance_name = instance_name
self.host = host
self.port = port
self.username = login
self.password = password
self.connection = None
self.channel = None
self.isActive = True
self.emit_delay = emit_delay
self.stat_producer = meter.metric_producer(self.emit_delay)
def _connection_params(self):
credentials = pika.PlainCredentials(self.username, self.password)
return pika.ConnectionParameters(host=self.host,
port=self.port,
credentials=credentials)
def _connect(self):
self.connection = pika.BlockingConnection(self._connection_params())
self.channel = self.connection.channel()
self.channel.exchange_declare(exchange=self.EXCHANGE, type=self.TYPE)
def emit_loop(self):
self._connect()
for stat in self.stat_producer:
stat['instance'] = self.instance_name
print stat
self.channel.basic_publish(exchange=self.EXCHANGE, body=pickle.dumps(stat), routing_key='',
properties=pika.BasicProperties(content_type="text/plain"))
postman = PostMan(socket.gethostname(), config.mq_host, login=config.mq_username, password=config.mq_password)
postman.emit_loop()