Mercurial > templog
comparison py/uploader.py @ 299:358c50004679
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 27 Feb 2017 23:20:07 +0800 |
parents | 78c542f03030 |
children | 6bacd8ca9f8f 424f1446f214 |
comparison
equal
deleted
inserted
replaced
286:61269311ed3d | 299:358c50004679 |
---|---|
13 import utils | 13 import utils |
14 | 14 |
15 class Uploader(object): | 15 class Uploader(object): |
16 def __init__(self, server): | 16 def __init__(self, server): |
17 self.server = server | 17 self.server = server |
18 self.limitlog = utils.NotTooOften(600) | |
18 | 19 |
19 @asyncio.coroutine | 20 @asyncio.coroutine |
20 def run(self): | 21 def run(self): |
21 # wait for the first read | 22 # wait for the first read |
22 yield from asyncio.sleep(5) | 23 yield from asyncio.sleep(5) |
41 tosend['start_time'] = self.server.start_time | 42 tosend['start_time'] = self.server.start_time |
42 tosend['uptime'] = utils.uptime() | 43 tosend['uptime'] = utils.uptime() |
43 | 44 |
44 return tosend | 45 return tosend |
45 | 46 |
47 class BadServerResponse(Exception): | |
48 pass | |
49 | |
46 @asyncio.coroutine | 50 @asyncio.coroutine |
47 def send(self, tosend): | 51 def send(self, tosend): |
48 js = json.dumps(tosend) | 52 js = json.dumps(tosend) |
49 if self.server.test_mode(): | 53 if self.server.test_mode(): |
50 D("Would upload %s to %s" % (js, config.UPDATE_URL)) | 54 D("Would upload %s to %s" % (js, config.UPDATE_URL)) |
53 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest() | 57 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest() |
54 send_data = {'data': js_enc.decode(), 'hmac': mac} | 58 send_data = {'data': js_enc.decode(), 'hmac': mac} |
55 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60) | 59 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60) |
56 result = yield from asyncio.wait_for(r.text(), 60) | 60 result = yield from asyncio.wait_for(r.text(), 60) |
57 if r.status == 200 and result != 'OK': | 61 if r.status == 200 and result != 'OK': |
58 raise Exception("Server returned %s" % result) | 62 raise BadServerResponse("Server returned %s" % result) |
59 | 63 |
60 @asyncio.coroutine | 64 @asyncio.coroutine |
61 def do(self): | 65 def do(self): |
62 try: | 66 try: |
63 readings = self.server.take_readings() | 67 readings = self.server.take_readings() |
65 D("tosend >>>%s<<<" % str(tosend)) | 69 D("tosend >>>%s<<<" % str(tosend)) |
66 nreadings = len(readings) | 70 nreadings = len(readings) |
67 yield from self.send(tosend) | 71 yield from self.send(tosend) |
68 readings = None | 72 readings = None |
69 D("Sent updated %d readings" % nreadings) | 73 D("Sent updated %d readings" % nreadings) |
74 except aiohttp.errors.ClientError as e: | |
75 self.limitlog.log("Error with uploader: %s" % str(e)) | |
76 except asyncio.TimeoutError as e: | |
77 self.limitlog.log("uploader http timed out: %s" % str(e)) | |
78 except self.BadServerResponse as e: | |
79 self.limitlog.log("Bad reply with uploader: %s" % str(e)) | |
70 except Exception as e: | 80 except Exception as e: |
71 EX("Error in uploader: %s" % str(e)) | 81 EX("Error in uploader: %s" % str(e)) |
72 finally: | 82 finally: |
73 if readings is not None: | 83 if readings is not None: |
74 self.server.pushfront(readings) | 84 self.server.pushfront(readings) |