annotate py/configwaiter.py @ 288:95f704ce809d

start/end for debug
author Matt Johnston <matt@ucc.asn.au>
date Thu, 01 Feb 2018 23:49:48 +0800
parents 654caee52c83
children 78c542f03030
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
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
2 import aiohttp
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
4 import utils
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
5 from utils import L,D,EX,W,E
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
6 import config
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
7
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
8 class ConfigWaiter(object):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
9 """ 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
10
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
11 def __init__(self, server):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
12 self.server = server
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
13 self.epoch_tag = None
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
14 self.http_session = aiohttp.ClientSession()
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
16 @asyncio.coroutine
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
17 def run(self):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
18 # 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
19 yield from asyncio.sleep(10)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
20 while True:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
21 yield from self.do()
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
22
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
23 # avoid spinning too fast
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
24 yield from asyncio.sleep(1)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
26 @asyncio.coroutine
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
27 def do(self):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
28 try:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
29 if self.epoch_tag:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
30 headers = {'etag': self.epoch_tag}
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
31 else:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
32 headers = None
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
33
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
34 r = yield from asyncio.wait_for(
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
35 self.http_session.get(config.SETTINGS_URL, headers=headers),
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
36 300)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
37 D("waiter status %d" % r.status)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
38 if r.status == 200:
260
6fb9d5f654ff increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
39 rawresp = yield from asyncio.wait_for(r.text(), 600)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
41 resp = utils.json_load_round_float(rawresp)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
43 self.epoch_tag = resp['epoch_tag']
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
44 D("waiter got epoch tag %s" % self.epoch_tag)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
45 epoch = self.epoch_tag.split('-')[0]
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
46 if self.server.params.receive(resp['params'], epoch):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
47 self.server.reload_signal(True)
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
48 elif r.status == 304:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
49 pass
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
50 else:
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
51 # longer timeout to avoid spinning
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
52 yield from asyncio.sleep(30)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
263
654caee52c83 catch timeout
Matt Johnston <matt@ucc.asn.au>
parents: 260
diff changeset
54 except asyncio.TimeoutError:
654caee52c83 catch timeout
Matt Johnston <matt@ucc.asn.au>
parents: 260
diff changeset
55 D("configwaiter http timed out")
654caee52c83 catch timeout
Matt Johnston <matt@ucc.asn.au>
parents: 260
diff changeset
56 pass
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
57 except Exception as e:
260
6fb9d5f654ff increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
58 EX("Error watching config: %s" % str(e))
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62