comparison py/tempserver.py @ 162:d73077e8cd67

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 256505f98c4d
children bf2a17873ba1
comparison
equal deleted inserted replaced
161:ad22db765ba1 162:d73077e8cd67
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
10 import daemon
9 11
10 import utils 12 import utils
11 from utils import L,D,EX,W 13 from utils import L,D,EX,W
12 import fridge 14 import fridge
13 import config 15 import config
92 def setup_logging(): 94 def setup_logging():
93 logging.basicConfig(format='%(asctime)s %(message)s', 95 logging.basicConfig(format='%(asctime)s %(message)s',
94 datefmt='%m/%d/%Y %I:%M:%S %p', 96 datefmt='%m/%d/%Y %I:%M:%S %p',
95 level=logging.DEBUG) 97 level=logging.DEBUG)
96 98
99 def start():
100 with Tempserver() as server:
101 server.run()
102
97 def main(): 103 def main():
98 setup_logging() 104 setup_logging()
99 105
106 pidpath = os.path.join(os.path.dirname(__file__), 'tempserver-lock')
107 pidf = lockfile.FileLock(pidpath, threaded=False)
108 pidf.acquire(0)
109
100 if '--daemon' in sys.argv: 110 if '--daemon' in sys.argv:
101 utils.cheap_daemon() 111 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')
102 with Tempserver() as server: 112 logf = open(logpath, 'a+')
103 server.run() 113 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):
114 start()
115 else:
116 with pidf:
117 start()
104 118
105 if __name__ == '__main__': 119 if __name__ == '__main__':
106 main() 120 main()