annotate py/tempserver.py @ 638:a9f353f488d0 rust

fix channels
author Matt Johnston <matt@ucc.asn.au>
date Sat, 09 Nov 2019 11:35:59 +0800
parents 08c3cf3d0ded
children 02aff9ff8d24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
1 #!/home/matt/templog/venv/bin/python
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import sys
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import os
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
5 import logging
198
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
6 import time
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
7 import signal
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
8 import asyncio
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
9 import argparse
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
10
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
11 import lockfile.pidlockfile
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
12 import daemon
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 import utils
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
15 from utils import L,D,EX,W
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
16 import fridge
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
17 import config
231
e39ed85d87a5 fix a few python3 issues
Matt Johnston <matt@ucc.asn.au>
parents: 230
diff changeset
18 import sensor
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
19 import params
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
20 import uploader
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
21 import configwaiter
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
22
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 class Tempserver(object):
266
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
25 def __init__(self, test_mode, nowait):
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 self.readings = []
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
27 self.current = (None, None)
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
28 self.fridge = None
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
29 self._wakeup = asyncio.Event()
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
30 self._test_mode = test_mode
266
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
31 self._nowait = nowait
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
33 def __enter__(self):
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
34 self.params = params.Params()
266
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
35 self.fridge = fridge.Fridge(self, self._nowait)
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
36 self.uploader = uploader.Uploader(self)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 234
diff changeset
37 self.configwaiter = configwaiter.ConfigWaiter(self)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
38 self.params.load()
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
39 self.set_sensors(sensor.make_sensor(self))
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 234
diff changeset
40 asyncio.get_event_loop().add_signal_handler(signal.SIGHUP, self.reload_signal)
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
41 return self
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
43 def __exit__(self, exc_type, exc_value, traceback):
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
44 L("Exiting, cleanup handler");
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
45 self.fridge.off()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
47 def run(self):
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
48
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
49 if self.fridge is None:
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
50 raise Exception("Tempserver.run() must be within 'with Tempserver() as server'")
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
51
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
52 # XXX do these go here or in __enter_() ?
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
53 self.start_time = self.now()
234
fd29ae905d1b Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
54 runloops = [
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
55 self.fridge.run(),
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
56 self.sensors.run(),
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
57 self.uploader.run(),
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 234
diff changeset
58 self.configwaiter.run(),
234
fd29ae905d1b Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
59 ]
232
a01b7bccccd3 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
60
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
61 loop = asyncio.get_event_loop()
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
62 try:
234
fd29ae905d1b Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
63 loop.run_until_complete(asyncio.gather(*runloops))
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
64 except KeyboardInterrupt:
234
fd29ae905d1b Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
65 print('\nctrl-c')
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
66 finally:
234
fd29ae905d1b Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
67 # loop.close() seems necessary otherwise get warnings about signal handlers
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
68 loop.close()
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
69
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
70 def now(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
71 return utils.monotonic_time()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
72
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
73 def set_sensors(self, sensors):
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
74 if hasattr(self, 'sensors'):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
75 self.sensors.kill()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
76 self.sensors = sensors
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
77 self.wort_name = sensors.wort_name()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
78 self.fridge_name = sensors.fridge_name()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 def take_readings(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 ret = self.readings
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 self.readings = []
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 return ret
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 def pushfront(self, readings):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 """ used if a caller of take_readings() fails """
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
87 self.readings = readings + self.readings
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
89 # a reading is a map of {sensorname: value}. temperatures
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
90 # are float degrees
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 def add_reading(self, reading):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 """ adds a reading at the current time """
155
d03157c7ad60 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
93 D("add_reading(%s)" % str(reading))
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
94 self.readings.append( (reading, self.now()))
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
95 self.current = (reading.get(self.wort_name, None),
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
96 reading.get(self.fridge_name, None))
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
97 if len(self.readings) > config.MAX_READINGS:
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
98 self.readings = self.readings[-config.MAX_READINGS:]
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 def current_temps(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
101 """ returns (wort_temp, fridge_temp) tuple """
232
a01b7bccccd3 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
102 D("current: %s" % str(self.current))
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
103 return self.current
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
104
232
a01b7bccccd3 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
105 @asyncio.coroutine
219
16a83e2c97a0 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 217
diff changeset
106 def sleep(self, timeout):
16a83e2c97a0 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 217
diff changeset
107 """ sleeps for timeout seconds, though wakes if the server's config is updated """
232
a01b7bccccd3 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
108 # XXX fixme - we should wake on _wakeup but asyncio Condition with wait_for is a bit broken?
a01b7bccccd3 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
109 # https://groups.google.com/forum/#!topic/python-tulip/eSm7rZAe9LM
a01b7bccccd3 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
110 # For now we just sleep, ignore the _wakeup
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
111 try:
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
112 yield from asyncio.wait_for(self._wakeup.wait(), timeout=timeout)
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
113 except asyncio.TimeoutError:
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
114 pass
234
fd29ae905d1b Fixed some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
115
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 234
diff changeset
116 def reload_signal(self, no_file = False):
219
16a83e2c97a0 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 217
diff changeset
117 try:
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 234
diff changeset
118 if not no_file:
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 234
diff changeset
119 self.params.load()
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 234
diff changeset
120 L("Reloaded.")
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
121 self._wakeup.set()
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
122 self._wakeup.clear()
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
123 except Error as e:
219
16a83e2c97a0 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 217
diff changeset
124 W("Problem reloading: %s" % str(e))
16a83e2c97a0 sleep on a semaphore so it can start/stop immediately when there's a SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 217
diff changeset
125
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
126 def test_mode(self):
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
127 return self._test_mode
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
128
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
129 def setup_logging(debug = False):
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
130 level = logging.INFO
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
131 if debug:
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
132 level = logging.DEBUG
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
133 logging.basicConfig(format='%(asctime)s %(message)s',
267
08c3cf3d0ded fix wrong date format. how'd that slip in.
Matt Johnston <matt@ucc.asn.au>
parents: 266
diff changeset
134 datefmt='%d/%m/%Y %I:%M:%S %p',
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
135 level=level)
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
136 #logging.getLogger("asyncio").setLevel(logging.DEBUG)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
137
266
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
138 def start(test_mode, nowait):
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
139 with Tempserver(test_mode, nowait) as server:
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
140 server.run()
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
141
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
142 def main():
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
143 parser = argparse.ArgumentParser()
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
144 parser.add_argument('--hup', action='store_true')
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
145 parser.add_argument('--new', action='store_true')
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
146 parser.add_argument('-D', '--daemon', action='store_true')
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
147 parser.add_argument('-d', '--debug', action='store_true')
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
148 parser.add_argument('-t', '--test', action='store_true')
266
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
149 parser.add_argument('--nowait', action='store_true')
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
150 args = parser.parse_args()
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
151
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
152 setup_logging(args.debug)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
153
174
c49d87bb81b9 fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
154 heredir = os.path.abspath(os.path.dirname(__file__))
c49d87bb81b9 fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
155 pidpath = os.path.join(heredir, 'tempserver.pid')
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
156 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
157
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
158
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
159 try:
217
5eb7e2400c18 fix pid locking
Matt Johnston <matt@ucc.asn.au>
parents: 199
diff changeset
160 pidf.acquire(1)
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
161 pidf.release()
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
162 except (lockfile.AlreadyLocked, lockfile.LockTimeout) as e:
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
163 pid = pidf.read_pid()
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
164 if args.hup:
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
165 try:
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
166 os.kill(pid, signal.SIGHUP)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 229
diff changeset
167 print("Sent SIGHUP to process %d" % pid, file=sys.stderr)
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
168 sys.exit(0)
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
169 except OSError:
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 229
diff changeset
170 print("Process %d isn't running?" % pid, file=sys.stderr)
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
171 sys.exit(1)
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
172
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 229
diff changeset
173 print("Locked by PID %d" % pid, file=sys.stderr)
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
174
198
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
175 stale = False
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
176 if pid > 0:
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
177 if args.new:
198
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
178 try:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
179 os.kill(pid, 0)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
180 except OSError:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
181 stale = True
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
182
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
183 if not stale:
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 229
diff changeset
184 print("Stopping old tempserver pid %d" % pid, file=sys.stderr)
198
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
185 os.kill(pid, signal.SIGTERM)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
186 time.sleep(2)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
187 pidf.acquire(0)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
188 pidf.release()
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
189 else:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
190 try:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
191 os.kill(pid, 0)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
192 # must still be running PID
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
193 raise e
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
194 except OSError:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
195 stale = True
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
196
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
197 if stale:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
198 # isn't still running, steal the lock
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 229
diff changeset
199 print("Unlinking stale lockfile %s for pid %d" % (pidpath, pid), file=sys.stderr)
198
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
200 pidf.break_lock()
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
201
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
202 if args.hup:
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 229
diff changeset
203 print("Doesn't seem to be running", file=sys.stderr)
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
204 sys.exit(1)
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
205
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 232
diff changeset
206 if args.daemon:
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
207 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
208 logf = open(logpath, 'a+')
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
209 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):
266
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
210 start(args.test, args.nowait)
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
211 else:
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
212 with pidf:
266
20c89630be6c emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
213 start(args.test, args.nowait)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
214
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
215 if __name__ == '__main__':
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
216 main()