Mercurial > templog
comparison py/tempserver.py @ 500:136b1343e640
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 24 Mar 2014 20:49:00 +0800 |
parents | 4a8a1e886a8c |
children | 5eb7e2400c18 |
comparison
equal
deleted
inserted
replaced
496:88204c0db5fa | 500:136b1343e640 |
---|---|
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 |
104 setup_logging() | 106 setup_logging() |
105 | 107 |
106 heredir = os.path.abspath(os.path.dirname(__file__)) | 108 heredir = os.path.abspath(os.path.dirname(__file__)) |
107 pidpath = os.path.join(heredir, 'tempserver.pid') | 109 pidpath = os.path.join(heredir, 'tempserver.pid') |
108 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False) | 110 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False) |
111 do_hup = '--hup' in sys.argv | |
109 try: | 112 try: |
110 pidf.acquire(0) | 113 pidf.acquire(0) |
111 pidf.release() | 114 pidf.release() |
112 except lockfile.AlreadyLocked, e: | 115 except lockfile.AlreadyLocked, e: |
113 pid = pidf.read_pid() | 116 pid = pidf.read_pid() |
117 if do_hup: | |
118 try: | |
119 os.kill(pid, signal.SIGHUP) | |
120 print>>sys.stderr, "Sent SIGHUP to process %d" % pid | |
121 sys.exit(0) | |
122 except OSError: | |
123 print>>sys.stderr, "Process %d isn't running?" % pid | |
124 sys.exit(1) | |
125 | |
114 print>>sys.stderr, "Locked by PID %d" % pid | 126 print>>sys.stderr, "Locked by PID %d" % pid |
127 | |
128 stale = False | |
115 if pid > 0: | 129 if pid > 0: |
116 try: | 130 if '--new' in sys.argv: |
117 os.kill(pid, 0) | 131 try: |
118 # must still be running PID | 132 os.kill(pid, 0) |
119 raise e | 133 except OSError: |
120 except OSError: | 134 stale = True |
121 # isn't still running, steal the lock | 135 |
122 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid) | 136 if not stale: |
123 pidf.break_lock() | 137 print>>sys.stderr, "Stopping old tempserver pid %d" % pid |
138 os.kill(pid, signal.SIGTERM) | |
139 time.sleep(2) | |
140 pidf.acquire(0) | |
141 pidf.release() | |
142 else: | |
143 try: | |
144 os.kill(pid, 0) | |
145 # must still be running PID | |
146 raise e | |
147 except OSError: | |
148 stale = True | |
149 | |
150 if stale: | |
151 # isn't still running, steal the lock | |
152 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid) | |
153 pidf.break_lock() | |
154 | |
155 if do_hup: | |
156 print>>sys.stderr, "Doesn't seem to be running" | |
157 sys.exit(1) | |
124 | 158 |
125 if '--daemon' in sys.argv: | 159 if '--daemon' in sys.argv: |
126 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') | 160 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') |
127 logf = open(logpath, 'a+') | 161 logf = open(logpath, 'a+') |
128 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): | 162 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): |