annotate py/tempserver.py @ 581:b4d6f1ae0b9e

make overshoot_factor floating point add a proper error message for noparamsyet
author Matt Johnston <matt@ucc.asn.au>
date Sun, 29 Nov 2015 09:30:36 +0800
parents c5f8375b12a2
children 20c89630be6c
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
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
8 import asyncio
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
9 import argparse
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
10
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
530
73257bae83ea fix a few python3 issues
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
18 import sensor
444
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
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
21 import configwaiter
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
22
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 class Tempserver(object):
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
25 def __init__(self, test_mode):
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 self.readings = []
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
27 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
28 self.fridge = None
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
29 self._wakeup = asyncio.Event()
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
30 self._test_mode = test_mode
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
32 def __enter__(self):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
33 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
34 self.fridge = fridge.Fridge(self)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
35 self.uploader = uploader.Uploader(self)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
36 self.configwaiter = configwaiter.ConfigWaiter(self)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
37 self.params.load()
528
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
38 self.set_sensors(sensor.make_sensor(self))
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
39 asyncio.get_event_loop().add_signal_handler(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()
533
815f811f6a65 Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
53 runloops = [
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
54 self.fridge.run(),
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
55 self.sensors.run(),
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
56 self.uploader.run(),
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
57 self.configwaiter.run(),
533
815f811f6a65 Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
58 ]
531
fd0fd9f947a1 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
59
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
60 loop = asyncio.get_event_loop()
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
61 try:
533
815f811f6a65 Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
62 loop.run_until_complete(asyncio.gather(*runloops))
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
63 except KeyboardInterrupt:
533
815f811f6a65 Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
64 print('\nctrl-c')
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
65 finally:
533
815f811f6a65 Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
66 # loop.close() seems necessary otherwise get warnings about signal handlers
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
67 loop.close()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
68
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
69 def now(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
70 return utils.monotonic_time()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
71
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
72 def set_sensors(self, sensors):
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
73 if hasattr(self, 'sensors'):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
74 self.sensors.kill()
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
75 self.sensors = sensors
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
76 self.wort_name = sensors.wort_name()
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
77 self.fridge_name = sensors.fridge_name()
439
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 take_readings(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 ret = self.readings
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 self.readings = []
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 return ret
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 def pushfront(self, readings):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 """ used if a caller of take_readings() fails """
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
86 self.readings = readings + self.readings
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
88 # 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
89 # are float degrees
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 def add_reading(self, reading):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 """ adds a reading at the current time """
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
92 D("add_reading(%s)" % str(reading))
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
93 self.readings.append( (reading, self.now()))
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
94 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
95 reading.get(self.fridge_name, None))
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
96 if len(self.readings) > config.MAX_READINGS:
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
97 self.readings = self.readings[-config.MAX_READINGS:]
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 def current_temps(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
100 """ returns (wort_temp, fridge_temp) tuple """
531
fd0fd9f947a1 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
101 D("current: %s" % str(self.current))
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
102 return self.current
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
103
531
fd0fd9f947a1 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
104 @asyncio.coroutine
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
105 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
106 """ sleeps for timeout seconds, though wakes if the server's config is updated """
531
fd0fd9f947a1 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
107 # XXX fixme - we should wake on _wakeup but asyncio Condition with wait_for is a bit broken?
fd0fd9f947a1 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
108 # https://groups.google.com/forum/#!topic/python-tulip/eSm7rZAe9LM
fd0fd9f947a1 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
109 # For now we just sleep, ignore the _wakeup
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
110 try:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
111 yield from asyncio.wait_for(self._wakeup.wait(), timeout=timeout)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
112 except asyncio.TimeoutError:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
113 pass
533
815f811f6a65 Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
114
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
115 def reload_signal(self, no_file = False):
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
116 try:
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
117 if not no_file:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
118 self.params.load()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 533
diff changeset
119 L("Reloaded.")
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
120 self._wakeup.set()
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
121 self._wakeup.clear()
528
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
122 except Error as e:
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
123 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
124
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
125 def test_mode(self):
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
126 return self._test_mode
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
127
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
128 def setup_logging(debug = False):
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
129 level = logging.INFO
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
130 if debug:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
131 level = logging.DEBUG
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
132 logging.basicConfig(format='%(asctime)s %(message)s',
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
133 datefmt='%m/%d/%Y %I:%M:%S %p',
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
134 level=level)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
135 #logging.getLogger("asyncio").setLevel(logging.DEBUG)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
136
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
137 def start(test_mode):
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
138 with Tempserver(test_mode) as server:
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
139 server.run()
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
140
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
141 def main():
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
142 parser = argparse.ArgumentParser()
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
143 parser.add_argument('--hup', action='store_true')
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
144 parser.add_argument('--new', action='store_true')
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
145 parser.add_argument('-D', '--daemon', action='store_true')
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
146 parser.add_argument('-d', '--debug', action='store_true')
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
147 parser.add_argument('-t', '--test', action='store_true')
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
148 args = parser.parse_args()
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
149
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
150 setup_logging(args.debug)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
151
473
bd29ddb360a5 fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents: 471
diff changeset
152 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
153 pidpath = os.path.join(heredir, 'tempserver.pid')
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
154 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
155
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
156
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
157 try:
514
0c29d1a16c8c fix pid locking
Matt Johnston <matt@ucc.asn.au>
parents: 499
diff changeset
158 pidf.acquire(1)
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
159 pidf.release()
528
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
160 except (lockfile.AlreadyLocked, lockfile.LockTimeout) as e:
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
161 pid = pidf.read_pid()
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
162 if args.hup:
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
163 try:
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
164 os.kill(pid, signal.SIGHUP)
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 528
diff changeset
165 print("Sent SIGHUP to process %d" % pid, file=sys.stderr)
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
166 sys.exit(0)
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
167 except OSError:
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 528
diff changeset
168 print("Process %d isn't running?" % pid, file=sys.stderr)
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
169 sys.exit(1)
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
170
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 528
diff changeset
171 print("Locked by PID %d" % pid, file=sys.stderr)
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
172
498
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
173 stale = False
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
174 if pid > 0:
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
175 if args.new:
498
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
176 try:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
177 os.kill(pid, 0)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
178 except OSError:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
179 stale = True
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
180
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
181 if not stale:
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 528
diff changeset
182 print("Stopping old tempserver pid %d" % pid, file=sys.stderr)
498
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
183 os.kill(pid, signal.SIGTERM)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
184 time.sleep(2)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
185 pidf.acquire(0)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
186 pidf.release()
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
187 else:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
188 try:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
189 os.kill(pid, 0)
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
190 # must still be running PID
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
191 raise e
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
192 except OSError:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
193 stale = True
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
194
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
195 if stale:
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
196 # isn't still running, steal the lock
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 528
diff changeset
197 print("Unlinking stale lockfile %s for pid %d" % (pidpath, pid), file=sys.stderr)
498
ccc2915b2dd9 Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 473
diff changeset
198 pidf.break_lock()
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
199
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
200 if args.hup:
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 528
diff changeset
201 print("Doesn't seem to be running", file=sys.stderr)
499
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
202 sys.exit(1)
4a8a1e886a8c add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 498
diff changeset
203
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
204 if args.daemon:
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
205 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
206 logf = open(logpath, 'a+')
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
207 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
208 start(args.test)
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
209 else:
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
210 with pidf:
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 531
diff changeset
211 start(args.test)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
212
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
213 if __name__ == '__main__':
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
214 main()