Mercurial > templog
annotate py/tempserver.py @ 623:03167105f6de rust
ignore missing local.conf
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 16 Apr 2017 22:50:20 +0800 |
parents | 08c3cf3d0ded |
children | 02aff9ff8d24 |
rev | line source |
---|---|
148 | 1 #!/home/matt/templog/venv/bin/python |
141 | 2 |
3 import sys | |
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 | 6 import time |
7 import signal | |
228 | 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 | 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 | 13 |
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 | 16 import fridge |
17 import config | |
231 | 18 import sensor |
145 | 19 import params |
160 | 20 import uploader |
259 | 21 import configwaiter |
145 | 22 |
141 | 23 |
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 | 26 self.readings = [] |
145 | 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 | 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 | 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 | 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 | 38 self.params.load() |
229 | 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 | 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 | 46 |
145 | 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 | 54 runloops = [ |
228 | 55 self.fridge.run(), |
56 self.sensors.run(), | |
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 | 59 ] |
232 | 60 |
228 | 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 | 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 | 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 | 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 | 69 |
70 def now(self): | |
71 return utils.monotonic_time() | |
72 | |
144 | 73 def set_sensors(self, sensors): |
148 | 74 if hasattr(self, 'sensors'): |
144 | 75 self.sensors.kill() |
76 self.sensors = sensors | |
77 self.wort_name = sensors.wort_name() | |
78 self.fridge_name = sensors.fridge_name() | |
141 | 79 |
80 def take_readings(self): | |
81 ret = self.readings | |
82 self.readings = [] | |
83 return ret | |
84 | |
85 def pushfront(self, readings): | |
86 """ used if a caller of take_readings() fails """ | |
160 | 87 self.readings = readings + self.readings |
141 | 88 |
144 | 89 # a reading is a map of {sensorname: value}. temperatures |
90 # are float degrees | |
141 | 91 def add_reading(self, reading): |
92 """ adds a reading at the current time """ | |
155 | 93 D("add_reading(%s)" % str(reading)) |
145 | 94 self.readings.append( (reading, self.now())) |
144 | 95 self.current = (reading.get(self.wort_name, None), |
96 reading.get(self.fridge_name, None)) | |
160 | 97 if len(self.readings) > config.MAX_READINGS: |
98 self.readings = self.readings[-config.MAX_READINGS:] | |
141 | 99 |
100 def current_temps(self): | |
144 | 101 """ returns (wort_temp, fridge_temp) tuple """ |
232 | 102 D("current: %s" % str(self.current)) |
148 | 103 return self.current |
145 | 104 |
232 | 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 | 108 # XXX fixme - we should wake on _wakeup but asyncio Condition with wait_for is a bit broken? |
109 # https://groups.google.com/forum/#!topic/python-tulip/eSm7rZAe9LM | |
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 159 try: |
217 | 160 pidf.acquire(1) |
172 | 161 pidf.release() |
229 | 162 except (lockfile.AlreadyLocked, lockfile.LockTimeout) as e: |
172 | 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 | 165 try: |
166 os.kill(pid, signal.SIGHUP) | |
230 | 167 print("Sent SIGHUP to process %d" % pid, file=sys.stderr) |
199 | 168 sys.exit(0) |
169 except OSError: | |
230 | 170 print("Process %d isn't running?" % pid, file=sys.stderr) |
199 | 171 sys.exit(1) |
172 | |
230 | 173 print("Locked by PID %d" % pid, file=sys.stderr) |
199 | 174 |
198 | 175 stale = False |
172 | 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 | 178 try: |
179 os.kill(pid, 0) | |
180 except OSError: | |
181 stale = True | |
182 | |
183 if not stale: | |
230 | 184 print("Stopping old tempserver pid %d" % pid, file=sys.stderr) |
198 | 185 os.kill(pid, signal.SIGTERM) |
186 time.sleep(2) | |
187 pidf.acquire(0) | |
188 pidf.release() | |
189 else: | |
190 try: | |
191 os.kill(pid, 0) | |
192 # must still be running PID | |
193 raise e | |
194 except OSError: | |
195 stale = True | |
196 | |
197 if stale: | |
198 # isn't still running, steal the lock | |
230 | 199 print("Unlinking stale lockfile %s for pid %d" % (pidpath, pid), file=sys.stderr) |
198 | 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 | 203 print("Doesn't seem to be running", file=sys.stderr) |
199 | 204 sys.exit(1) |
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 | 214 |
215 if __name__ == '__main__': | |
216 main() |