annotate py/configwaiter.py @ 253:0a1b642e3086

long polling config updates
author Matt Johnston <matt@ucc.asn.au>
date Mon, 08 Jun 2015 22:29:46 +0800
parents
children 26eee8591f61
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 class ConfigWaiter(object):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 """ Waits for config updates from the server. http long polling """
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 def __init__(self, server):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 self.server = server
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 self.epoch_tag = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 self.http_session = aiohttp.ClientSession()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 @asyncio.coroutine
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 def run(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 # wait until someting has been uploaded (the uploader itself waits 5 seconds)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 yield from asyncio.sleep(10)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 while True:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 yield from self.do()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 # avoid spinning too fast
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 yield from server.sleep(1)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 @asyncio.coroutine
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 def do(self):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 try:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 if self.epoch_tag:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 headers = {'etag': self.epoch_tag}
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 else:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 headers = None
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 r = yield from asyncio.wait_for(
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 self.http_session.get(config.SETTINGS_URL, headers=headers),
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 300)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 if r.status == 200:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 resp = yield from asyncio.wait_for(r.json(), 300)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 self.epoch_tag = resp['epoch_tag']
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 epoch = self.epoch_tag.split('-')[0]
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 if self.server.params.receive(resp['params'], epoch):
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 self.server.reload_signal(True)
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 except Exception as e:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 E("Error watching config: %s" % str(e))
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43