diff py/configwaiter.py @ 259:26eee8591f61

long polling works
author Matt Johnston <matt@ucc.asn.au>
date Tue, 09 Jun 2015 23:27:44 +0800
parents 0a1b642e3086
children 6fb9d5f654ff
line wrap: on
line diff
--- a/py/configwaiter.py	Mon Jun 08 22:33:04 2015 +0800
+++ b/py/configwaiter.py	Tue Jun 09 23:27:44 2015 +0800
@@ -1,42 +1,58 @@
-class ConfigWaiter(object):
-	""" Waits for config updates from the server. http long polling """
+import asyncio
+import aiohttp
 
-	def __init__(self, server):
-		self.server = server
-		self.epoch_tag = None
-		self.http_session = aiohttp.ClientSession()
+import utils
+from utils import L,D,EX,W,E
+import config
+
+class ConfigWaiter(object):
+    """ Waits for config updates from the server. http long polling """
 
-	@asyncio.coroutine
-	def run(self):
-		# wait until someting has been uploaded (the uploader itself waits 5 seconds)
-		yield from asyncio.sleep(10)
-		while True:
-			yield from self.do()
+    def __init__(self, server):
+        self.server = server
+        self.epoch_tag = None
+        self.http_session = aiohttp.ClientSession()
 
-			# avoid spinning too fast
-			yield from server.sleep(1)
+    @asyncio.coroutine
+    def run(self):
+        # wait until someting has been uploaded (the uploader itself waits 5 seconds)
+        yield from asyncio.sleep(10)
+        while True:
+            yield from self.do()
+
+            # avoid spinning too fast
+            yield from asyncio.sleep(1)
 
-	@asyncio.coroutine
-	def do(self):
-		try:
-			if self.epoch_tag:
-				headers = {'etag': self.epoch_tag}
-			else:
-				headers = None
+    @asyncio.coroutine
+    def do(self):
+        try:
+            if self.epoch_tag:
+                headers = {'etag': self.epoch_tag}
+            else:
+                headers = None
+
+            r = yield from asyncio.wait_for(
+                self.http_session.get(config.SETTINGS_URL, headers=headers), 
+                300)
+            D("waiter status %d" % r.status)
+            if r.status == 200:
+                rawresp = yield from asyncio.wait_for(r.text(), 300)
 
-	        r = yield from asyncio.wait_for(
-	        	self.http_session.get(config.SETTINGS_URL, headers=headers), 
-	        	300)
-	        if r.status == 200:
-		        resp = yield from asyncio.wait_for(r.json(), 300)
+                resp = utils.json_load_round_float(rawresp)
 
-		        self.epoch_tag = resp['epoch_tag']
-		        epoch = self.epoch_tag.split('-')[0]
-		        if self.server.params.receive(resp['params'], epoch):
-		        	self.server.reload_signal(True)
+                self.epoch_tag = resp['epoch_tag']
+                D("waiter got epoch tag %s" % self.epoch_tag)
+                epoch = self.epoch_tag.split('-')[0]
+                if self.server.params.receive(resp['params'], epoch):
+                    self.server.reload_signal(True)
+            elif r.status == 304:
+                pass
+            else:
+                # longer timeout to avoid spinning
+                yield from asyncio.sleep(30)
 
-		 except Exception as e:
-		 	E("Error watching config: %s" % str(e))
+        except Exception as e:
+            E("Error watching config: %s" % str(e))