Mercurial > templog
comparison py/configwaiter.py @ 259:26eee8591f61
long polling works
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 09 Jun 2015 23:27:44 +0800 |
parents | 0a1b642e3086 |
children | 6fb9d5f654ff |
comparison
equal
deleted
inserted
replaced
256:6d06795aefbb | 259:26eee8591f61 |
---|---|
1 import asyncio | |
2 import aiohttp | |
3 | |
4 import utils | |
5 from utils import L,D,EX,W,E | |
6 import config | |
7 | |
1 class ConfigWaiter(object): | 8 class ConfigWaiter(object): |
2 """ Waits for config updates from the server. http long polling """ | 9 """ Waits for config updates from the server. http long polling """ |
3 | 10 |
4 def __init__(self, server): | 11 def __init__(self, server): |
5 self.server = server | 12 self.server = server |
6 self.epoch_tag = None | 13 self.epoch_tag = None |
7 self.http_session = aiohttp.ClientSession() | 14 self.http_session = aiohttp.ClientSession() |
8 | 15 |
9 @asyncio.coroutine | 16 @asyncio.coroutine |
10 def run(self): | 17 def run(self): |
11 # wait until someting has been uploaded (the uploader itself waits 5 seconds) | 18 # wait until someting has been uploaded (the uploader itself waits 5 seconds) |
12 yield from asyncio.sleep(10) | 19 yield from asyncio.sleep(10) |
13 while True: | 20 while True: |
14 yield from self.do() | 21 yield from self.do() |
15 | 22 |
16 # avoid spinning too fast | 23 # avoid spinning too fast |
17 yield from server.sleep(1) | 24 yield from asyncio.sleep(1) |
18 | 25 |
19 @asyncio.coroutine | 26 @asyncio.coroutine |
20 def do(self): | 27 def do(self): |
21 try: | 28 try: |
22 if self.epoch_tag: | 29 if self.epoch_tag: |
23 headers = {'etag': self.epoch_tag} | 30 headers = {'etag': self.epoch_tag} |
24 else: | 31 else: |
25 headers = None | 32 headers = None |
26 | 33 |
27 r = yield from asyncio.wait_for( | 34 r = yield from asyncio.wait_for( |
28 self.http_session.get(config.SETTINGS_URL, headers=headers), | 35 self.http_session.get(config.SETTINGS_URL, headers=headers), |
29 300) | 36 300) |
30 if r.status == 200: | 37 D("waiter status %d" % r.status) |
31 resp = yield from asyncio.wait_for(r.json(), 300) | 38 if r.status == 200: |
39 rawresp = yield from asyncio.wait_for(r.text(), 300) | |
32 | 40 |
33 self.epoch_tag = resp['epoch_tag'] | 41 resp = utils.json_load_round_float(rawresp) |
34 epoch = self.epoch_tag.split('-')[0] | |
35 if self.server.params.receive(resp['params'], epoch): | |
36 self.server.reload_signal(True) | |
37 | 42 |
38 except Exception as e: | 43 self.epoch_tag = resp['epoch_tag'] |
39 E("Error watching config: %s" % str(e)) | 44 D("waiter got epoch tag %s" % self.epoch_tag) |
45 epoch = self.epoch_tag.split('-')[0] | |
46 if self.server.params.receive(resp['params'], epoch): | |
47 self.server.reload_signal(True) | |
48 elif r.status == 304: | |
49 pass | |
50 else: | |
51 # longer timeout to avoid spinning | |
52 yield from asyncio.sleep(30) | |
53 | |
54 except Exception as e: | |
55 E("Error watching config: %s" % str(e)) | |
40 | 56 |
41 | 57 |
42 | 58 |
43 | 59 |