-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug_spy.py
executable file
·82 lines (60 loc) · 2.57 KB
/
debug_spy.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
import sys
import MySQLdb as sql
import pika
import string
# Debug spy connects to EVERYTHING and dumps to console to assist debugging
def keyval_get(chunk, key, defaultValue=""):
parts = string.split(chunk)
for X in range(0, len(parts)):
y = string.split(parts[X], '=')
if (y[0] == key) & (len(y) > 1):
return y[len(y)-1]
return defaultValue
DBHOST = os.environ['STREAMBOSS_DBHOST']
DBUSER = os.environ['STREAMBOSS_DBUSER']
DBPASS = os.environ['STREAMBOSS_DBPASS']
DBDB = os.environ['STREAMBOSS_DBNAME']
RMQHOST = os.environ['STREAMBOSS_RABBITMQ_HOST']
E_STREAM_NAMETAKEN = -100;
STREAMSTATE = {'COLD' : 0, 'COOL' : 1, 'WARM' : 2, 'HOT' : 3}
rmq_conn = pika.BlockingConnection(pika.ConnectionParameters(host=RMQHOST))
channel = rmq_conn.channel()
myqueue = channel.queue_declare('')
db_conn = sql.connect(DBHOST, DBUSER, DBPASS, DBDB)
curs = db_conn.cursor()
nRows = curs.execute("SELECT * FROM datastreams")
# subscribe to announcer queue
channel.queue_bind(exchange='stream_announce', queue=myqueue.method.queue)
for j in range(0, nRows):
dbvals = curs.fetchone()
print "Subscribing to %s..." % (dbvals[2])
channel.queue_bind(exchange='streams', queue=myqueue.method.queue, routing_key=dbvals[2]);
if nRows == 0:
print "No streams currently exist to subscribe to"
def loggerfunc(ch, method, properties, body):
if method.exchange == "stream_announce":
print "Got msg on queue %s with key %s: %s" % (method.exchange, method.routing_key, body)
if body[0] == 'N': # New stream: subscribe
thekey = keyval_get(body, "key", "INVALID")
if thekey == "INVALID":
print "Malformed [N] message on announce queue!"
else:
channel.queue_bind(exchange='streams', queue=myqueue.method.queue, routing_key=thekey);
print "New stream announced; Subscribing to %s" % (thekey)
if body[0] == 'D': # Dying stream, unsubscribe
parts = string.split(body);
if len(parts) == 2:
thekey = parts[1]
channel.queue_unbind(exchange='streams', queue=myqueue.method.queue, routing_key=thekey);
print "Stream death announced; Unsubscribing from %s" % (thekey)
else:
print "Malformed [D] message on announce queue: "+body
else:
print "Got msg from stream %s: %s" % (method.routing_key, body)
channel.basic_consume(loggerfunc, queue=myqueue.method.queue, no_ack=True)
try:
channel.start_consuming()
except:
channel.stop_consuming()
rmq_conn.close()