-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis_listener.sh
executable file
·64 lines (51 loc) · 1.77 KB
/
redis_listener.sh
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
#!/usr/bin/env python
import threading
import time
import subprocess
import argparse
import os
import redis
# environment defaults
host=os.getenv("REDIS_HOST", default='127.0.0.1')
port=os.getenv("REDIS_PORT", default='6379')
channel=os.getenv("REDIS_CHANNEL", default='VEGADNS-CHANGES')
script=os.getenv("UPDATE_SCRIPT", default='/etc/update-data.sh')
parser = argparse.ArgumentParser(
description='Listen for VegaDNS update notifications from Redis Pub/Sub'
)
parser.add_argument('--host', dest='host', action='store',
default=host, help='Redis host')
parser.add_argument('--port', dest='port', action='store',
default=port, help='Redis port')
parser.add_argument('--channel', dest='channel', action='store',
default=channel, help='Redis channel')
parser.add_argument('--script', dest='script', action='store',
default=script,
help='Location of update-data.sh script')
args = parser.parse_args()
class Listener(threading.Thread):
def __init__(self, r, args):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(args.channel)
def work(self, item):
try:
print subprocess.check_output(
[args.script],
stderr=subprocess.STDOUT
)
except Exception, e:
print e
def run(self):
for item in self.pubsub.listen():
self.work(item)
if __name__ == "__main__":
r = redis.Redis(host=args.host, port=args.port)
client = Listener(r, args)
client.daemon = True
print "starting redis listener"
client.start()
# Run in foreground and wait for signals
while True:
time.sleep(1)