diff py/tempserver.py @ 219:16a83e2c97a0

sleep on a semaphore so it can start/stop immediately when there's a SIGHUP from the web UI. makes it seem more magical.
author Matt Johnston <matt@ucc.asn.au>
date Fri, 19 Dec 2014 21:52:50 +0800
parents 5eb7e2400c18
children d9e81a563923
line wrap: on
line diff
--- a/py/tempserver.py	Fri Dec 19 21:39:18 2014 +0800
+++ b/py/tempserver.py	Fri Dec 19 21:52:50 2014 +0800
@@ -25,6 +25,7 @@
         self.readings = []
         self.current = (None, None)
         self.fridge = None
+        self._wakeup = gevent.event.Event()
 
         # don't patch os, fork() is used by daemonize
         gevent.monkey.patch_all(os=False, thread=False)
@@ -35,6 +36,7 @@
         self.uploader = uploader.Uploader(self)
         self.params.load()
         self.set_sensors(sensor_ds18b20.DS18B20s(self))
+        gevent.signal(signal.SIGHUP, self._reload_signal)
         return self
 
     def __exit__(self, exc_type, exc_value, traceback):
@@ -93,6 +95,19 @@
         """ returns (wort_temp, fridge_temp) tuple """
         return self.current
 
+    def sleep(self, timeout):
+        """ sleeps for timeout seconds, though wakes if the server's config is updated """
+        self._wakeup.wait(timeout)
+        
+    def _reload_signal(self):
+        try:
+            self.params.load()
+            L("Reloaded.")
+            self._wakeup.set()
+            self._wakeup.clear()
+        except self.Error, e:
+            W("Problem reloading: %s" % str(e))
+
 def setup_logging():
     logging.basicConfig(format='%(asctime)s %(message)s', 
             datefmt='%m/%d/%Y %I:%M:%S %p',