annotate py/configwaiter.py @ 598:d4fbfb5c46ff rust

broken update of versions of things
author Matt Johnston <matt@ucc.asn.au>
date Tue, 07 Feb 2017 21:56:58 +0800
parents 78c542f03030
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
1 import asyncio
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
2 import datetime
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
3
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
4 import aiohttp
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
6 import utils
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
7 from utils import L,D,EX,W,E
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
8 import config
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
9
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
10 class ConfigWaiter(object):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
11 """ Waits for config updates from the server. http long polling """
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
13 def __init__(self, server):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
14 self.server = server
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
15 self.epoch_tag = None
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
16 self.http_session = aiohttp.ClientSession()
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
17 self.limitlog = utils.NotTooOften(datetime.timedelta(minutes=15))
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
19 @asyncio.coroutine
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
20 def run(self):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
21 # wait until someting has been uploaded (the uploader itself waits 5 seconds)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
22 yield from asyncio.sleep(10)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
23 while True:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
24 yield from self.do()
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
25
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
26 # avoid spinning too fast
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
27 yield from asyncio.sleep(1)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
29 @asyncio.coroutine
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
30 def do(self):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
31 try:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
32 if self.epoch_tag:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
33 headers = {'etag': self.epoch_tag}
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
34 else:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
35 headers = None
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
36
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
37 r = yield from asyncio.wait_for(
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
38 self.http_session.get(config.SETTINGS_URL, headers=headers),
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
39 300)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
40 D("waiter status %d" % r.status)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
41 if r.status == 200:
260
6fb9d5f654ff increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
42 rawresp = yield from asyncio.wait_for(r.text(), 600)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
44 resp = utils.json_load_round_float(rawresp)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
46 self.epoch_tag = resp['epoch_tag']
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
47 D("waiter got epoch tag %s" % self.epoch_tag)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
48 epoch = self.epoch_tag.split('-')[0]
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
49 if self.server.params.receive(resp['params'], epoch):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
50 self.server.reload_signal(True)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
51 elif r.status == 304:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
52 pass
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
53 else:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
54 # longer timeout to avoid spinning
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
55 text = yield from asyncio.wait_for(r.text(), 600)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
56 D("Bad server response. %d %s" % (r.status, text))
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
57 yield from asyncio.sleep(30)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
59 except aiohttp.errors.ClientError as e:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
60 self.limitlog.log("Error with configwaiter: %s" % str(e))
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
61 except asyncio.TimeoutError as e:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 263
diff changeset
62 self.limitlog.log("configwaiter http timed out: %s" % str(e))
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
63 except Exception as e:
260
6fb9d5f654ff increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
64 EX("Error watching config: %s" % str(e))