253
|
1 class ConfigWaiter(object): |
|
2 """ Waits for config updates from the server. http long polling """ |
|
3 |
|
4 def __init__(self, server): |
|
5 self.server = server |
|
6 self.epoch_tag = None |
|
7 self.http_session = aiohttp.ClientSession() |
|
8 |
|
9 @asyncio.coroutine |
|
10 def run(self): |
|
11 # wait until someting has been uploaded (the uploader itself waits 5 seconds) |
|
12 yield from asyncio.sleep(10) |
|
13 while True: |
|
14 yield from self.do() |
|
15 |
|
16 # avoid spinning too fast |
|
17 yield from server.sleep(1) |
|
18 |
|
19 @asyncio.coroutine |
|
20 def do(self): |
|
21 try: |
|
22 if self.epoch_tag: |
|
23 headers = {'etag': self.epoch_tag} |
|
24 else: |
|
25 headers = None |
|
26 |
|
27 r = yield from asyncio.wait_for( |
|
28 self.http_session.get(config.SETTINGS_URL, headers=headers), |
|
29 300) |
|
30 if r.status == 200: |
|
31 resp = yield from asyncio.wait_for(r.json(), 300) |
|
32 |
|
33 self.epoch_tag = resp['epoch_tag'] |
|
34 epoch = self.epoch_tag.split('-')[0] |
|
35 if self.server.params.receive(resp['params'], epoch): |
|
36 self.server.reload_signal(True) |
|
37 |
|
38 except Exception as e: |
|
39 E("Error watching config: %s" % str(e)) |
|
40 |
|
41 |
|
42 |
|
43 |