annotate py/configwaiter.py @ 581:b4d6f1ae0b9e

make overshoot_factor floating point add a proper error message for noparamsyet
author Matt Johnston <matt@ucc.asn.au>
date Sun, 29 Nov 2015 09:30:36 +0800
parents 55cae4f27e75
children 78c542f03030
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
1 import asyncio
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
2 import aiohttp
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
4 import utils
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
5 from utils import L,D,EX,W,E
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
6 import config
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
7
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
8 class ConfigWaiter(object):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
9 """ Waits for config updates from the server. http long polling """
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
11 def __init__(self, server):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
12 self.server = server
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
13 self.epoch_tag = None
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
14 self.http_session = aiohttp.ClientSession()
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
16 @asyncio.coroutine
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
17 def run(self):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
18 # wait until someting has been uploaded (the uploader itself waits 5 seconds)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
19 yield from asyncio.sleep(10)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
20 while True:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
21 yield from self.do()
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
22
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
23 # avoid spinning too fast
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
24 yield from asyncio.sleep(1)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
26 @asyncio.coroutine
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
27 def do(self):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
28 try:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
29 if self.epoch_tag:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
30 headers = {'etag': self.epoch_tag}
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
31 else:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
32 headers = None
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
33
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
34 r = yield from asyncio.wait_for(
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
35 self.http_session.get(config.SETTINGS_URL, headers=headers),
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
36 300)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
37 D("waiter status %d" % r.status)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
38 if r.status == 200:
557
830ee5663703 increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
39 rawresp = yield from asyncio.wait_for(r.text(), 600)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
41 resp = utils.json_load_round_float(rawresp)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
43 self.epoch_tag = resp['epoch_tag']
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
44 D("waiter got epoch tag %s" % self.epoch_tag)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
45 epoch = self.epoch_tag.split('-')[0]
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
46 if self.server.params.receive(resp['params'], epoch):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
47 self.server.reload_signal(True)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
48 elif r.status == 304:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
49 pass
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
50 else:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
51 # longer timeout to avoid spinning
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
52 yield from asyncio.sleep(30)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
562
55cae4f27e75 catch timeout
Matt Johnston <matt@ucc.asn.au>
parents: 557
diff changeset
54 except asyncio.TimeoutError:
55cae4f27e75 catch timeout
Matt Johnston <matt@ucc.asn.au>
parents: 557
diff changeset
55 D("configwaiter http timed out")
55cae4f27e75 catch timeout
Matt Johnston <matt@ucc.asn.au>
parents: 557
diff changeset
56 pass
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
57 except Exception as e:
557
830ee5663703 increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
58 EX("Error watching config: %s" % str(e))
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62