Mercurial > templog
annotate py/tempserver.py @ 233:19569cb5ed46
better arg parser. seems close to ready
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 11 Apr 2015 23:32:59 +0800 |
parents | a01b7bccccd3 |
children | fd29ae905d1b |
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 |
145 | 21 |
141 | 22 |
23 class Tempserver(object): | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
24 def __init__(self, test_mode): |
141 | 25 self.readings = [] |
145 | 26 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
|
27 self.fridge = None |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
28 self._wakeup = asyncio.Event() |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
29 self._test_mode = test_mode |
141 | 30 |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
31 def __enter__(self): |
145 | 32 self.params = params.Params() |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
33 self.fridge = fridge.Fridge(self) |
160 | 34 self.uploader = uploader.Uploader(self) |
145 | 35 self.params.load() |
229 | 36 self.set_sensors(sensor.make_sensor(self)) |
228 | 37 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
|
38 return self |
141 | 39 |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
40 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
|
41 L("Exiting, cleanup handler"); |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
42 self.fridge.off() |
141 | 43 |
145 | 44 def run(self): |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
45 |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
46 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
|
47 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
|
48 |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
49 # 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
|
50 self.start_time = self.now() |
228 | 51 tasks = ( |
52 self.fridge.run(), | |
53 self.sensors.run(), | |
54 self.uploader.run(), | |
55 ) | |
232 | 56 |
228 | 57 loop = asyncio.get_event_loop() |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
58 try: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
59 result_tasks = loop.run_until_complete(asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
60 # use the results so that exceptions get thrown |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
61 [t.result() for x in result_tasks for t in x] |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
62 except KeyboardInterrupt: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
63 print('ctrl-c') |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
64 pass |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
65 finally: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
66 loop.close() |
145 | 67 |
68 def now(self): | |
69 return utils.monotonic_time() | |
70 | |
144 | 71 def set_sensors(self, sensors): |
148 | 72 if hasattr(self, 'sensors'): |
144 | 73 self.sensors.kill() |
74 self.sensors = sensors | |
75 self.wort_name = sensors.wort_name() | |
76 self.fridge_name = sensors.fridge_name() | |
141 | 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 """ | |
160 | 85 self.readings = readings + self.readings |
141 | 86 |
144 | 87 # a reading is a map of {sensorname: value}. temperatures |
88 # are float degrees | |
141 | 89 def add_reading(self, reading): |
90 """ adds a reading at the current time """ | |
155 | 91 D("add_reading(%s)" % str(reading)) |
145 | 92 self.readings.append( (reading, self.now())) |
144 | 93 self.current = (reading.get(self.wort_name, None), |
94 reading.get(self.fridge_name, None)) | |
160 | 95 if len(self.readings) > config.MAX_READINGS: |
96 self.readings = self.readings[-config.MAX_READINGS:] | |
141 | 97 |
98 def current_temps(self): | |
144 | 99 """ returns (wort_temp, fridge_temp) tuple """ |
232 | 100 D("current: %s" % str(self.current)) |
148 | 101 return self.current |
145 | 102 |
232 | 103 @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
|
104 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
|
105 """ sleeps for timeout seconds, though wakes if the server's config is updated """ |
232 | 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 | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
109 try: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
110 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
|
111 except asyncio.TimeoutError: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
112 pass |
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
|
113 |
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
|
114 def _reload_signal(self): |
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
|
115 try: |
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
|
116 self.params.load() |
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 L("Reloaded.") |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
118 self._wakeup.set() |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
119 self._wakeup.clear() |
229 | 120 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
|
121 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
|
122 |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
123 def test_mode(self): |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
124 return self._test_mode |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
125 |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
126 def setup_logging(debug = False): |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
127 level = logging.INFO |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
128 if debug: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
129 level = logging.DEBUG |
145 | 130 logging.basicConfig(format='%(asctime)s %(message)s', |
131 datefmt='%m/%d/%Y %I:%M:%S %p', | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
132 level=level) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
133 #logging.getLogger("asyncio").setLevel(logging.DEBUG) |
145 | 134 |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
135 def start(test_mode): |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
136 with Tempserver(test_mode) as server: |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
137 server.run() |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
138 |
145 | 139 def main(): |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
140 parser = argparse.ArgumentParser() |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
141 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
|
142 parser.add_argument('--new', action='store_true') |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
143 parser.add_argument('--daemon', action='store_true') |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
144 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
|
145 parser.add_argument('-t', '--test', action='store_true') |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
146 args = parser.parse_args() |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
147 |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
148 setup_logging(args.debug) |
145 | 149 |
174
c49d87bb81b9
fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents:
172
diff
changeset
|
150 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
|
151 pidpath = os.path.join(heredir, 'tempserver.pid') |
172 | 152 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
|
153 |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
154 |
172 | 155 try: |
217 | 156 pidf.acquire(1) |
172 | 157 pidf.release() |
229 | 158 except (lockfile.AlreadyLocked, lockfile.LockTimeout) as e: |
172 | 159 pid = pidf.read_pid() |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
160 if args.hup: |
199 | 161 try: |
162 os.kill(pid, signal.SIGHUP) | |
230 | 163 print("Sent SIGHUP to process %d" % pid, file=sys.stderr) |
199 | 164 sys.exit(0) |
165 except OSError: | |
230 | 166 print("Process %d isn't running?" % pid, file=sys.stderr) |
199 | 167 sys.exit(1) |
168 | |
230 | 169 print("Locked by PID %d" % pid, file=sys.stderr) |
199 | 170 |
198 | 171 stale = False |
172 | 172 if pid > 0: |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
173 if args.new: |
198 | 174 try: |
175 os.kill(pid, 0) | |
176 except OSError: | |
177 stale = True | |
178 | |
179 if not stale: | |
230 | 180 print("Stopping old tempserver pid %d" % pid, file=sys.stderr) |
198 | 181 os.kill(pid, signal.SIGTERM) |
182 time.sleep(2) | |
183 pidf.acquire(0) | |
184 pidf.release() | |
185 else: | |
186 try: | |
187 os.kill(pid, 0) | |
188 # must still be running PID | |
189 raise e | |
190 except OSError: | |
191 stale = True | |
192 | |
193 if stale: | |
194 # isn't still running, steal the lock | |
230 | 195 print("Unlinking stale lockfile %s for pid %d" % (pidpath, pid), file=sys.stderr) |
198 | 196 pidf.break_lock() |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
197 |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
198 if args.hup: |
230 | 199 print("Doesn't seem to be running", file=sys.stderr) |
199 | 200 sys.exit(1) |
201 | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
202 if args.daemon: |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
203 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
|
204 logf = open(logpath, 'a+') |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
205 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
206 start(args.test) |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
207 else: |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
208 with pidf: |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
232
diff
changeset
|
209 start(args.test) |
145 | 210 |
211 if __name__ == '__main__': | |
212 main() |