diff py/tempserver.py @ 172:5821c5fab919

use better pidlockfile
author Matt Johnston <matt@ucc.asn.au>
date Fri, 14 Jun 2013 23:32:06 +0800
parents bf2a17873ba1
children c49d87bb81b9
line wrap: on
line diff
--- a/py/tempserver.py	Thu Apr 25 21:03:53 2013 +0800
+++ b/py/tempserver.py	Fri Jun 14 23:32:06 2013 +0800
@@ -6,7 +6,7 @@
 
 import gevent
 import gevent.monkey
-import lockfile
+import lockfile.pidlockfile
 import daemon
 
 import utils
@@ -103,9 +103,23 @@
 def main():
     setup_logging()
 
-    pidpath = os.path.join(os.path.dirname(__file__), 'tempserver-lock')
-    pidf = lockfile.FileLock(pidpath, threaded=False)
-    pidf.acquire(0)
+    pidpath = os.path.join(os.path.dirname(__file__), 'tempserver.pid')
+    pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
+    try:
+        pidf.acquire(0)
+        pidf.release()
+    except lockfile.AlreadyLocked, e:
+        pid = pidf.read_pid()
+        print>>sys.stderr, "Locked by PID %d" % pid
+        if pid > 0:
+            try:
+                os.kill(pid, 0)
+                # must still be running PID
+                raise e
+            except OSError:
+                # isn't still running, steal the lock
+                print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
+                pidf.break_lock()
 
     if '--daemon' in sys.argv:
         logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')