comparison py/tempserver.py @ 498:ccc2915b2dd9

Add --new flag
author Matt Johnston <matt@ucc.asn.au>
date Tue, 04 Mar 2014 22:12:29 +0800
parents bd29ddb360a5
children 2affbaae408b
comparison
equal deleted inserted replaced
497:455f19b64e0f 498:ccc2915b2dd9
1 #!/home/matt/templog/venv/bin/python 1 #!/home/matt/templog/venv/bin/python
2 2
3 import sys 3 import sys
4 import os 4 import os
5 import logging 5 import logging
6 import time
7 import signal
6 8
7 import gevent 9 import gevent
8 import gevent.monkey 10 import gevent.monkey
9 import lockfile.pidlockfile 11 import lockfile.pidlockfile
10 import daemon 12 import daemon
110 pidf.acquire(0) 112 pidf.acquire(0)
111 pidf.release() 113 pidf.release()
112 except lockfile.AlreadyLocked, e: 114 except lockfile.AlreadyLocked, e:
113 pid = pidf.read_pid() 115 pid = pidf.read_pid()
114 print>>sys.stderr, "Locked by PID %d" % pid 116 print>>sys.stderr, "Locked by PID %d" % pid
117 stale = False
115 if pid > 0: 118 if pid > 0:
116 try: 119 if '--new' in sys.argv:
117 os.kill(pid, 0) 120 try:
118 # must still be running PID 121 os.kill(pid, 0)
119 raise e 122 except OSError:
120 except OSError: 123 stale = True
121 # isn't still running, steal the lock 124
122 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid) 125 if not stale:
123 pidf.break_lock() 126 print>>sys.stderr, "Stopping old tempserver pid %d" % pid
127 os.kill(pid, signal.SIGTERM)
128 time.sleep(2)
129 pidf.acquire(0)
130 pidf.release()
131 else:
132 try:
133 os.kill(pid, 0)
134 # must still be running PID
135 raise e
136 except OSError:
137 stale = True
138
139 if stale:
140 # isn't still running, steal the lock
141 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
142 pidf.break_lock()
124 143
125 if '--daemon' in sys.argv: 144 if '--daemon' in sys.argv:
126 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') 145 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')
127 logf = open(logpath, 'a+') 146 logf = open(logpath, 'a+')
128 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): 147 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):