Mercurial > templog
diff py/configwaiter.py @ 293:d15dda1b1f76
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 06 Jul 2019 18:29:45 +0800 |
parents | 654caee52c83 |
children | 78c542f03030 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/py/configwaiter.py Sat Jul 06 18:29:45 2019 +0800 @@ -0,0 +1,62 @@ +import asyncio +import aiohttp + +import utils +from utils import L,D,EX,W,E +import config + +class ConfigWaiter(object): + """ Waits for config updates from the server. http long polling """ + + def __init__(self, server): + self.server = server + self.epoch_tag = None + self.http_session = aiohttp.ClientSession() + + @asyncio.coroutine + def run(self): + # wait until someting has been uploaded (the uploader itself waits 5 seconds) + yield from asyncio.sleep(10) + while True: + yield from self.do() + + # avoid spinning too fast + yield from asyncio.sleep(1) + + @asyncio.coroutine + def do(self): + try: + if self.epoch_tag: + headers = {'etag': self.epoch_tag} + else: + headers = None + + r = yield from asyncio.wait_for( + self.http_session.get(config.SETTINGS_URL, headers=headers), + 300) + D("waiter status %d" % r.status) + if r.status == 200: + rawresp = yield from asyncio.wait_for(r.text(), 600) + + resp = utils.json_load_round_float(rawresp) + + self.epoch_tag = resp['epoch_tag'] + D("waiter got epoch tag %s" % self.epoch_tag) + epoch = self.epoch_tag.split('-')[0] + if self.server.params.receive(resp['params'], epoch): + self.server.reload_signal(True) + elif r.status == 304: + pass + else: + # longer timeout to avoid spinning + yield from asyncio.sleep(30) + + except asyncio.TimeoutError: + D("configwaiter http timed out") + pass + except Exception as e: + EX("Error watching config: %s" % str(e)) + + + +