diff py/tempserver.py @ 461:1eb68df9f8ab

Add daemon mode with locking, add "disabled" parameter
author Matt Johnston <matt@ucc.asn.au>
date Fri, 11 Jan 2013 23:41:56 +0800
parents c34083c078db
children bf2a17873ba1
line wrap: on
line diff
--- a/py/tempserver.py	Thu Jan 10 19:46:32 2013 +0800
+++ b/py/tempserver.py	Fri Jan 11 23:41:56 2013 +0800
@@ -6,6 +6,8 @@
 
 import gevent
 import gevent.monkey
+import lockfile
+import daemon
 
 import utils
 from utils import L,D,EX,W
@@ -94,13 +96,25 @@
             datefmt='%m/%d/%Y %I:%M:%S %p',
             level=logging.DEBUG)
 
+def start():
+    with Tempserver() as server:
+        server.run()
+
 def main():
     setup_logging()
 
+    pidpath = os.path.join(os.path.dirname(__file__), 'tempserver-lock')
+    pidf = lockfile.FileLock(pidpath, threaded=False)
+    pidf.acquire(0)
+
     if '--daemon' in sys.argv:
-        utils.cheap_daemon()
-    with Tempserver() as server:
-        server.run()
+        logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')
+        logf = open(logpath, 'a+')
+        with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):
+            start()
+    else:
+        with pidf:
+            start()
 
 if __name__ == '__main__':
     main()