annotate py/configwaiter.py @ 558:9f659ece4e63

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