-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogfromqueue.py
59 lines (54 loc) · 2.11 KB
/
logfromqueue.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
from redis import Redis
import sys
from os.path import join
from random import randint
from time import sleep
LOGFILEPATH = "processed"
if __name__ == "__main__":
if len(sys.argv) == 2:
if sys.argv[1] == "--dryrun":
while(True):
print "Not going to listen to queue - dry run only"
sleep(1000)
# log and consume
r = Redis()
with open(join(LOGFILEPATH, "%s.log" % sys.argv[1]), "a+") as logfile:
while(True):
line = r.lpop("q:%s" % sys.argv[1])
if line:
line = line.decode('utf-8')
if line.endswith("\n"):
logfile.write(line)
else:
logfile.writelines((line, "\n"))
else:
# Might as well flush the IO
logfile.flush()
# Check again after a 5-10 seconds wait (to stop spikes)
sleep(randint(0,3))
elif len(sys.argv) > 2:
# log and fanout
r = Redis()
logfilename = sys.argv[1]
queues = sys.argv[2:]
with open(join(LOGFILEPATH, "%s.log" % sys.argv[1]), "a+") as logfile:
while(True):
line = r.lpop("q:%s" % sys.argv[1])
if line:
if line.endswith("\n"):
logfile.write(line)
else:
logfile.writelines((line, "\n"))
for queue in queues:
r.lpush("q:%s" % queue, line)
else:
# Flushing IO
logfile.flush()
# Check again after a 5-10 seconds wait (to stop spikes)
sleep(randint(0,3))
else:
print "Usage: logfromqueue.py log_name [daisy-chain_to_queues_-_space_delimited]"
print "For example,"
print "'logfromqueue.py foo' - listen to 'q:foo' and log msgs sent to it to 'foo.log'"
print "'logfromqueue.py foo bar' - as above, but propagate the msg to 'q:bar' as well"
sys.exit(2)