annotate py/tempserver.py @ 526:1f25c9d97111

Only print interesting legend labels
author Matt Johnston <matt@ucc.asn.au>
date Sat, 28 Feb 2015 00:14:21 +0800
parents 0b5ff341d124
children d9e81a563923
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
1 #!/home/matt/templog/venv/bin/python
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import sys
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import os
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
5 import logging
498
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
6 import time
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
7 import signal
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
8
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 import gevent
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
10 import gevent.monkey
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
11 import lockfile.pidlockfile
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
12 import daemon
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 import utils
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
15 from utils import L,D,EX,W
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
16 import fridge
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
17 import config
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
18 import sensor_ds18b20
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
19 import params
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
20 import uploader
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
21
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 class Tempserver(object):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 def __init__(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 self.readings = []
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
26 self.current = (None, None)
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
27 self.fridge = None
518
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
28 self._wakeup = gevent.event.Event()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
29
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
30 # don't patch os, fork() is used by daemonize
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
31 gevent.monkey.patch_all(os=False, thread=False)
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
33 def __enter__(self):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
34 self.params = params.Params()
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
35 self.fridge = fridge.Fridge(self)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
36 self.uploader = uploader.Uploader(self)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
37 self.params.load()
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
38 self.set_sensors(sensor_ds18b20.DS18B20s(self))
518
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
39 gevent.signal(signal.SIGHUP, self._reload_signal)
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
40 return self
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
42 def __exit__(self, exc_type, exc_value, traceback):
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
43 L("Exiting, cleanup handler");
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
44 self.fridge.off()
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
46 def run(self):
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
47
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
48 if self.fridge is None:
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
49 raise Exception("Tempserver.run() must be within 'with Tempserver() as server'")
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
50
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
51 # XXX do these go here or in __enter_() ?
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
52 self.start_time = self.now()
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
53 self.fridge.start()
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
54 self.sensors.start()
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
55 self.uploader.start()
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
56
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
57 # won't return.
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
58 while True:
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
59 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
60 gevent.sleep(60)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
61 except KeyboardInterrupt:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
62 break
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
63
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
64 def now(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
65 return utils.monotonic_time()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
66
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
67 def set_sensors(self, sensors):
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
68 if hasattr(self, 'sensors'):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
69 self.sensors.kill()
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
70 self.sensors = sensors
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
71 self.wort_name = sensors.wort_name()
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
72 self.fridge_name = sensors.fridge_name()
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 def take_readings(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 ret = self.readings
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 self.readings = []
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 return ret
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 def pushfront(self, readings):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 """ used if a caller of take_readings() fails """
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
81 self.readings = readings + self.readings
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
83 # a reading is a map of {sensorname: value}. temperatures
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
84 # are float degrees
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 def add_reading(self, reading):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 """ adds a reading at the current time """
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
87 D("add_reading(%s)" % str(reading))
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
88 self.readings.append( (reading, self.now()))
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
89 self.current = (reading.get(self.wort_name, None),
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
90 reading.get(self.fridge_name, None))
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
91 if len(self.readings) > config.MAX_READINGS:
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
92 self.readings = self.readings[-config.MAX_READINGS:]
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 def current_temps(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
95 """ returns (wort_temp, fridge_temp) tuple """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
96 return self.current
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
97
518
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
98 def sleep(self, timeout):
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
99 """ sleeps for timeout seconds, though wakes if the server's config is updated """
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
100 self._wakeup.wait(timeout)
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
101
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
102 def _reload_signal(self):
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
103 try:
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
104 self.params.load()
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
105 L("Reloaded.")
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
106 self._wakeup.set()
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
107 self._wakeup.clear()
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
108 except self.Error, e:
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
109 W("Problem reloading: %s" % str(e))
0b5ff341d124 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 514
diff changeset
110
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
111 def setup_logging():
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
112 logging.basicConfig(format='%(asctime)s %(message)s',
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
113 datefmt='%m/%d/%Y %I:%M:%S %p',
462
a91adc95543d less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 461
diff changeset
114 level=logging.INFO)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
115
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
116 def start():
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
117 with Tempserver() as server:
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
118 server.run()
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
119
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
120 def main():
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
121 setup_logging()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
122
473
bd29ddb360a5 fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents: 471
diff changeset
123 heredir = os.path.abspath(os.path.dirname(__file__))
bd29ddb360a5 fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents: 471
diff changeset
124 pidpath = os.path.join(heredir, 'tempserver.pid')
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
125 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
126 do_hup = '--hup' in sys.argv
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
127 try:
514
0c29d1a16c8c fix pid locking
Matt Johnston <matt@ucc.asn.au>
parents: 499
diff changeset
128 pidf.acquire(1)
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
129 pidf.release()
514
0c29d1a16c8c fix pid locking
Matt Johnston <matt@ucc.asn.au>
parents: 499
diff changeset
130 except (lockfile.AlreadyLocked, lockfile.LockTimeout), e:
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
131 pid = pidf.read_pid()
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
132 if do_hup:
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
133 try:
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
134 os.kill(pid, signal.SIGHUP)
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
135 print>>sys.stderr, "Sent SIGHUP to process %d" % pid
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
136 sys.exit(0)
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
137 except OSError:
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
138 print>>sys.stderr, "Process %d isn't running?" % pid
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
139 sys.exit(1)
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
140
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
141 print>>sys.stderr, "Locked by PID %d" % pid
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
142
498
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
143 stale = False
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
144 if pid > 0:
498
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
145 if '--new' in sys.argv:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
146 try:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
147 os.kill(pid, 0)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
148 except OSError:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
149 stale = True
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
150
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
151 if not stale:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
152 print>>sys.stderr, "Stopping old tempserver pid %d" % pid
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
153 os.kill(pid, signal.SIGTERM)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
154 time.sleep(2)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
155 pidf.acquire(0)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
156 pidf.release()
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
157 else:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
158 try:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
159 os.kill(pid, 0)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
160 # must still be running PID
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
161 raise e
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
162 except OSError:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
163 stale = True
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
164
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
165 if stale:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
166 # isn't still running, steal the lock
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
167 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
168 pidf.break_lock()
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
169
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
170 if do_hup:
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
171 print>>sys.stderr, "Doesn't seem to be running"
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
172 sys.exit(1)
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
173
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
174 if '--daemon' in sys.argv:
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
175 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
176 logf = open(logpath, 'a+')
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
177 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
178 start()
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
179 else:
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
180 with pidf:
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
181 start()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
182
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
183 if __name__ == '__main__':
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
184 main()