changeset 157:6d799bee3508

reload config on SIGHUP
author Matt Johnston <matt@ucc.asn.au>
date Thu, 03 Jan 2013 20:12:07 +0800
parents bed65c321b46
children a6612745b4a3 683cfd134f6a
files py/params.py py/tempserver.py
diffstat 2 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/py/params.py	Thu Jan 03 20:11:54 2013 +0800
+++ b/py/params.py	Thu Jan 03 20:12:07 2013 +0800
@@ -1,6 +1,10 @@
 # -*- coding: utf-8 -*-
 import collections
 import json
+import signal
+
+import gevent
+
 import config
 from utils import W,L,E,EX
 
@@ -17,6 +21,7 @@
 
     def __init__(self):
         self.update(_FIELD_DEFAULTS)
+        gevent.signal(signal.SIGHUP, self.reload_signal)
 
     def __getattr__(self, k):
         return self[k]
@@ -33,7 +38,11 @@
             except IOError, e:
                 W("Missing parameter file, using defaults. %s", e)
                 return
-        u = json.load(f)
+        try:
+            u = json.load(f)
+        except Exception, e:
+            raise self.Error(e)
+
         for k in u:
             if k not in self:
                 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???')))
@@ -45,3 +54,10 @@
         json.dump(self, f, sort_keys=True, indent=4)
         f.write('\n')
         f.flush()
+
+    def reload_signal(self):
+        try:
+            self.load()
+            L("Reloaded params")
+        except self.Error, e:
+            W("Problem reloading: %s" % str(e))
--- a/py/tempserver.py	Thu Jan 03 20:11:54 2013 +0800
+++ b/py/tempserver.py	Thu Jan 03 20:12:07 2013 +0800
@@ -47,7 +47,10 @@
 
         # won't return.
         while True:
-            gevent.sleep(60)
+            try:
+                gevent.sleep(60)
+            except KeyboardInterrupt:
+                break
 
     def now(self):
         return utils.monotonic_time()