comparison 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
comparison
equal deleted inserted replaced
170:78255c49bf9a 172:5821c5fab919
4 import os 4 import os
5 import logging 5 import logging
6 6
7 import gevent 7 import gevent
8 import gevent.monkey 8 import gevent.monkey
9 import lockfile 9 import lockfile.pidlockfile
10 import daemon 10 import daemon
11 11
12 import utils 12 import utils
13 from utils import L,D,EX,W 13 from utils import L,D,EX,W
14 import fridge 14 import fridge
101 server.run() 101 server.run()
102 102
103 def main(): 103 def main():
104 setup_logging() 104 setup_logging()
105 105
106 pidpath = os.path.join(os.path.dirname(__file__), 'tempserver-lock') 106 pidpath = os.path.join(os.path.dirname(__file__), 'tempserver.pid')
107 pidf = lockfile.FileLock(pidpath, threaded=False) 107 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
108 pidf.acquire(0) 108 try:
109 pidf.acquire(0)
110 pidf.release()
111 except lockfile.AlreadyLocked, e:
112 pid = pidf.read_pid()
113 print>>sys.stderr, "Locked by PID %d" % pid
114 if pid > 0:
115 try:
116 os.kill(pid, 0)
117 # must still be running PID
118 raise e
119 except OSError:
120 # isn't still running, steal the lock
121 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
122 pidf.break_lock()
109 123
110 if '--daemon' in sys.argv: 124 if '--daemon' in sys.argv:
111 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') 125 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')
112 logf = open(logpath, 'a+') 126 logf = open(logpath, 'a+')
113 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): 127 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):