changeset 518:0b5ff341d124

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 c399293de160
children 838e868a046e
files py/fridge.py py/params.py py/sensor_ds18b20.py py/tempserver.py py/uploader.py
diffstat 5 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/py/fridge.py	Fri Dec 19 21:39:18 2014 +0800
+++ b/py/fridge.py	Fri Dec 19 21:52:50 2014 +0800
@@ -52,7 +52,7 @@
             L("Fridge is disabled")
         while True:
             self.do()
-            gevent.sleep(config.FRIDGE_SLEEP)
+            self.server.sleep(config.FRIDGE_SLEEP)
 
     def do(self):
         """ this is the main fridge control logic """
--- a/py/params.py	Fri Dec 19 21:39:18 2014 +0800
+++ b/py/params.py	Fri Dec 19 21:52:50 2014 +0800
@@ -26,7 +26,6 @@
 
     def __init__(self):
         self.update(_FIELD_DEFAULTS)
-        gevent.signal(signal.SIGHUP, self.reload_signal)
 
     def __getattr__(self, k):
         return self[k]
@@ -70,10 +69,3 @@
         s = StringIO.StringIO()
         self.save(s)
         return s.getvalue()
-
-    def reload_signal(self):
-        try:
-            self.load()
-            L("Reloaded.")
-        except self.Error, e:
-            W("Problem reloading: %s" % str(e))
--- a/py/sensor_ds18b20.py	Fri Dec 19 21:39:18 2014 +0800
+++ b/py/sensor_ds18b20.py	Fri Dec 19 21:52:50 2014 +0800
@@ -35,7 +35,7 @@
     def _run(self):
         while True:
             self.do()
-            gevent.sleep(config.SENSOR_SLEEP)
+            self.server.sleep(config.SENSOR_SLEEP)
 
     def read_wait(self, f):
         # handles a blocking file read with a gevent threadpool. A
--- 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',
--- a/py/uploader.py	Fri Dec 19 21:39:18 2014 +0800
+++ b/py/uploader.py	Fri Dec 19 21:52:50 2014 +0800
@@ -23,7 +23,7 @@
         gevent.sleep(5)
         while True:
             self.do()
-            gevent.sleep(config.UPLOAD_SLEEP)
+            self.server.sleep(config.UPLOAD_SLEEP)
 
     def get_tosend(self, readings):
         tosend = {}