annotate py/configwaiter.py @ 579:4a6e75ed2ed4

fix gpio chown script
author Matt Johnston <matt@ucc.asn.au>
date Mon, 30 Nov 2015 21:40:45 +0800
parents 59cb8449ef97
children
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
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
2 import datetime
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
3
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
4 import aiohttp
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
6 import utils
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
7 from utils import L,D,EX,W,E
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
8 import config
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
9
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
10 class ConfigWaiter(object):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
11 """ 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
12
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
13 def __init__(self, server):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
14 self.server = server
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
15 self.epoch_tag = None
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
16 self.http_session = aiohttp.ClientSession()
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
17 self.limitlog = utils.NotTooOften(datetime.timedelta(minutes=15))
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
19 @asyncio.coroutine
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
20 def run(self):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
21 # 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
22 yield from asyncio.sleep(10)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
23 while True:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
24 yield from self.do()
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
25
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
26 # avoid spinning too fast
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
27 yield from asyncio.sleep(1)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
29 @asyncio.coroutine
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
30 def do(self):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
31 try:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
32 if self.epoch_tag:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
33 headers = {'etag': self.epoch_tag}
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
34 else:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
35 headers = None
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
36
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
37 r = yield from asyncio.wait_for(
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
38 self.http_session.get(config.SETTINGS_URL, headers=headers),
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
39 300)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
40 D("waiter status %d" % r.status)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
41 if r.status == 200:
557
830ee5663703 increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
42 rawresp = yield from asyncio.wait_for(r.text(), 600)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
44 resp = utils.json_load_round_float(rawresp)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
46 self.epoch_tag = resp['epoch_tag']
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
47 D("waiter got epoch tag %s" % self.epoch_tag)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
48 epoch = self.epoch_tag.split('-')[0]
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
49 if self.server.params.receive(resp['params'], epoch):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
50 self.server.reload_signal(True)
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
51 elif r.status == 304:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
52 pass
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
53 else:
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
54 # longer timeout to avoid spinning
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
55 text = yield from asyncio.wait_for(r.text(), 600)
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
56 D("Bad server response. %d %s" % (r.status, text))
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
57 yield from asyncio.sleep(30)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
59 except aiohttp.errors.ClientError as e:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
60 self.limitlog.log("Error with configwaiter: %s" % str(e))
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
61 except asyncio.TimeoutError as e:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 562
diff changeset
62 self.limitlog.log("configwaiter http timed out: %s" % str(e))
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
63 except Exception as e:
557
830ee5663703 increase waiter timeout
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
64 EX("Error watching config: %s" % str(e))