Mercurial > templog
annotate py/tempserver.py @ 551:9499bd2f344b
long polling config updates
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 08 Jun 2015 22:29:46 +0800 |
parents | 815f811f6a65 |
children | 26eee8591f61 |
rev | line source |
---|---|
447 | 1 #!/home/matt/templog/venv/bin/python |
439 | 2 |
3 import sys | |
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 | 6 import time |
7 import signal | |
527 | 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 | 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 | 13 |
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 | 16 import fridge |
17 import config | |
530 | 18 import sensor |
444 | 19 import params |
459 | 20 import uploader |
444 | 21 |
439 | 22 |
23 class Tempserver(object): | |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
24 def __init__(self, test_mode): |
439 | 25 self.readings = [] |
444 | 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 |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
28 self._wakeup = asyncio.Event() |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
29 self._test_mode = test_mode |
439 | 30 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
31 def __enter__(self): |
444 | 32 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
|
33 self.fridge = fridge.Fridge(self) |
459 | 34 self.uploader = uploader.Uploader(self) |
551
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
533
diff
changeset
|
35 self.configwaiter = configwaiter.ConfigWaiter(self) |
444 | 36 self.params.load() |
528 | 37 self.set_sensors(sensor.make_sensor(self)) |
551
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
533
diff
changeset
|
38 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
|
39 return self |
439 | 40 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
41 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
|
42 L("Exiting, cleanup handler"); |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
43 self.fridge.off() |
439 | 44 |
444 | 45 def run(self): |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
46 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
47 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
|
48 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
|
49 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
50 # 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
|
51 self.start_time = self.now() |
533 | 52 runloops = [ |
527 | 53 self.fridge.run(), |
54 self.sensors.run(), | |
55 self.uploader.run(), | |
551
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
533
diff
changeset
|
56 self.configwaiter.run(), |
533 | 57 ] |
531 | 58 |
527 | 59 loop = asyncio.get_event_loop() |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
60 try: |
533 | 61 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
|
62 except KeyboardInterrupt: |
533 | 63 print('\nctrl-c') |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
64 finally: |
533 | 65 # 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
|
66 loop.close() |
444 | 67 |
68 def now(self): | |
69 return utils.monotonic_time() | |
70 | |
443 | 71 def set_sensors(self, sensors): |
447 | 72 if hasattr(self, 'sensors'): |
443 | 73 self.sensors.kill() |
74 self.sensors = sensors | |
75 self.wort_name = sensors.wort_name() | |
76 self.fridge_name = sensors.fridge_name() | |
439 | 77 |
78 def take_readings(self): | |
79 ret = self.readings | |
80 self.readings = [] | |
81 return ret | |
82 | |
83 def pushfront(self, readings): | |
84 """ used if a caller of take_readings() fails """ | |
459 | 85 self.readings = readings + self.readings |
439 | 86 |
443 | 87 # a reading is a map of {sensorname: value}. temperatures |
88 # are float degrees | |
439 | 89 def add_reading(self, reading): |
90 """ adds a reading at the current time """ | |
454 | 91 D("add_reading(%s)" % str(reading)) |
444 | 92 self.readings.append( (reading, self.now())) |
443 | 93 self.current = (reading.get(self.wort_name, None), |
94 reading.get(self.fridge_name, None)) | |
459 | 95 if len(self.readings) > config.MAX_READINGS: |
96 self.readings = self.readings[-config.MAX_READINGS:] | |
439 | 97 |
98 def current_temps(self): | |
443 | 99 """ returns (wort_temp, fridge_temp) tuple """ |
531 | 100 D("current: %s" % str(self.current)) |
447 | 101 return self.current |
444 | 102 |
531 | 103 @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
|
104 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
|
105 """ sleeps for timeout seconds, though wakes if the server's config is updated """ |
531 | 106 # XXX fixme - we should wake on _wakeup but asyncio Condition with wait_for is a bit broken? |
107 # https://groups.google.com/forum/#!topic/python-tulip/eSm7rZAe9LM | |
108 # 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
|
109 try: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
110 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
|
111 except asyncio.TimeoutError: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
112 pass |
533 | 113 |
551
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
533
diff
changeset
|
114 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
|
115 try: |
551
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
533
diff
changeset
|
116 if not no_file: |
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
533
diff
changeset
|
117 self.params.load() |
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
533
diff
changeset
|
118 L("Reloaded.") |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
119 self._wakeup.set() |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
120 self._wakeup.clear() |
528 | 121 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
|
122 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
|
123 |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
124 def test_mode(self): |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
125 return self._test_mode |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
126 |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
127 def setup_logging(debug = False): |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
128 level = logging.INFO |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
129 if debug: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
130 level = logging.DEBUG |
444 | 131 logging.basicConfig(format='%(asctime)s %(message)s', |
132 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
|
133 level=level) |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
134 #logging.getLogger("asyncio").setLevel(logging.DEBUG) |
444 | 135 |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
136 def start(test_mode): |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
137 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
|
138 server.run() |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
139 |
444 | 140 def main(): |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
141 parser = argparse.ArgumentParser() |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
142 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
|
143 parser.add_argument('--new', 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('--daemon', action='store_true') |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
145 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
|
146 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
|
147 args = parser.parse_args() |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
148 |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
149 setup_logging(args.debug) |
444 | 150 |
473
bd29ddb360a5
fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents:
471
diff
changeset
|
151 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
|
152 pidpath = os.path.join(heredir, 'tempserver.pid') |
471 | 153 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
|
154 |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
155 |
471 | 156 try: |
514 | 157 pidf.acquire(1) |
471 | 158 pidf.release() |
528 | 159 except (lockfile.AlreadyLocked, lockfile.LockTimeout) as e: |
471 | 160 pid = pidf.read_pid() |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
161 if args.hup: |
499 | 162 try: |
163 os.kill(pid, signal.SIGHUP) | |
529 | 164 print("Sent SIGHUP to process %d" % pid, file=sys.stderr) |
499 | 165 sys.exit(0) |
166 except OSError: | |
529 | 167 print("Process %d isn't running?" % pid, file=sys.stderr) |
499 | 168 sys.exit(1) |
169 | |
529 | 170 print("Locked by PID %d" % pid, file=sys.stderr) |
499 | 171 |
498 | 172 stale = False |
471 | 173 if pid > 0: |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
174 if args.new: |
498 | 175 try: |
176 os.kill(pid, 0) | |
177 except OSError: | |
178 stale = True | |
179 | |
180 if not stale: | |
529 | 181 print("Stopping old tempserver pid %d" % pid, file=sys.stderr) |
498 | 182 os.kill(pid, signal.SIGTERM) |
183 time.sleep(2) | |
184 pidf.acquire(0) | |
185 pidf.release() | |
186 else: | |
187 try: | |
188 os.kill(pid, 0) | |
189 # must still be running PID | |
190 raise e | |
191 except OSError: | |
192 stale = True | |
193 | |
194 if stale: | |
195 # isn't still running, steal the lock | |
529 | 196 print("Unlinking stale lockfile %s for pid %d" % (pidpath, pid), file=sys.stderr) |
498 | 197 pidf.break_lock() |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
198 |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
199 if args.hup: |
529 | 200 print("Doesn't seem to be running", file=sys.stderr) |
499 | 201 sys.exit(1) |
202 | |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
203 if args.daemon: |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
204 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
|
205 logf = open(logpath, 'a+') |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
206 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
|
207 start(args.test) |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
208 else: |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
209 with pidf: |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
531
diff
changeset
|
210 start(args.test) |
444 | 211 |
212 if __name__ == '__main__': | |
213 main() |