Mercurial > templog
annotate py/tempserver.py @ 227:efb5cad2e98b
Only print interesting legend labels
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 28 Feb 2015 00:14:21 +0800 |
parents | 16a83e2c97a0 |
children | d9e81a563923 |
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 | |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
8 |
141 | 9 import gevent |
145 | 10 import gevent.monkey |
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 | |
18 import sensor_ds18b20 | |
19 import params | |
160 | 20 import uploader |
145 | 21 |
141 | 22 |
23 class Tempserver(object): | |
24 def __init__(self): | |
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 |
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
|
28 self._wakeup = gevent.event.Event() |
145 | 29 |
30 # don't patch os, fork() is used by daemonize | |
31 gevent.monkey.patch_all(os=False, thread=False) | |
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() |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
35 self.fridge = fridge.Fridge(self) |
160 | 36 self.uploader = uploader.Uploader(self) |
145 | 37 self.params.load() |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
38 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
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
|
39 gevent.signal(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
|
40 return self |
141 | 41 |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
42 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
|
43 L("Exiting, cleanup handler"); |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
44 self.fridge.off() |
141 | 45 |
145 | 46 def run(self): |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
47 |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
48 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
|
49 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
|
50 |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
51 # 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
|
52 self.start_time = self.now() |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
53 self.fridge.start() |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
54 self.sensors.start() |
160 | 55 self.uploader.start() |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
56 |
145 | 57 # won't return. |
58 while True: | |
157 | 59 try: |
60 gevent.sleep(60) | |
61 except KeyboardInterrupt: | |
62 break | |
145 | 63 |
64 def now(self): | |
65 return utils.monotonic_time() | |
66 | |
144 | 67 def set_sensors(self, sensors): |
148 | 68 if hasattr(self, 'sensors'): |
144 | 69 self.sensors.kill() |
70 self.sensors = sensors | |
71 self.wort_name = sensors.wort_name() | |
72 self.fridge_name = sensors.fridge_name() | |
141 | 73 |
74 def take_readings(self): | |
75 ret = self.readings | |
76 self.readings = [] | |
77 return ret | |
78 | |
79 def pushfront(self, readings): | |
80 """ used if a caller of take_readings() fails """ | |
160 | 81 self.readings = readings + self.readings |
141 | 82 |
144 | 83 # a reading is a map of {sensorname: value}. temperatures |
84 # are float degrees | |
141 | 85 def add_reading(self, reading): |
86 """ adds a reading at the current time """ | |
155 | 87 D("add_reading(%s)" % str(reading)) |
145 | 88 self.readings.append( (reading, self.now())) |
144 | 89 self.current = (reading.get(self.wort_name, None), |
90 reading.get(self.fridge_name, None)) | |
160 | 91 if len(self.readings) > config.MAX_READINGS: |
92 self.readings = self.readings[-config.MAX_READINGS:] | |
141 | 93 |
94 def current_temps(self): | |
144 | 95 """ returns (wort_temp, fridge_temp) tuple """ |
148 | 96 return self.current |
145 | 97 |
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
|
98 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
|
99 """ sleeps for timeout seconds, though wakes if the server's config is updated """ |
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
|
100 self._wakeup.wait(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
|
101 |
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
|
102 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
|
103 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
|
104 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
|
105 L("Reloaded.") |
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 self._wakeup.set() |
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 self._wakeup.clear() |
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
|
108 except self.Error, 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
|
109 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
|
110 |
145 | 111 def setup_logging(): |
112 logging.basicConfig(format='%(asctime)s %(message)s', | |
113 datefmt='%m/%d/%Y %I:%M:%S %p', | |
163 | 114 level=logging.INFO) |
145 | 115 |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
116 def start(): |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
117 with Tempserver() as server: |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
118 server.run() |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
119 |
145 | 120 def main(): |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
121 setup_logging() |
145 | 122 |
174
c49d87bb81b9
fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents:
172
diff
changeset
|
123 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
|
124 pidpath = os.path.join(heredir, 'tempserver.pid') |
172 | 125 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False) |
199 | 126 do_hup = '--hup' in sys.argv |
172 | 127 try: |
217 | 128 pidf.acquire(1) |
172 | 129 pidf.release() |
217 | 130 except (lockfile.AlreadyLocked, lockfile.LockTimeout), e: |
172 | 131 pid = pidf.read_pid() |
199 | 132 if do_hup: |
133 try: | |
134 os.kill(pid, signal.SIGHUP) | |
135 print>>sys.stderr, "Sent SIGHUP to process %d" % pid | |
136 sys.exit(0) | |
137 except OSError: | |
138 print>>sys.stderr, "Process %d isn't running?" % pid | |
139 sys.exit(1) | |
140 | |
172 | 141 print>>sys.stderr, "Locked by PID %d" % pid |
199 | 142 |
198 | 143 stale = False |
172 | 144 if pid > 0: |
198 | 145 if '--new' in sys.argv: |
146 try: | |
147 os.kill(pid, 0) | |
148 except OSError: | |
149 stale = True | |
150 | |
151 if not stale: | |
152 print>>sys.stderr, "Stopping old tempserver pid %d" % pid | |
153 os.kill(pid, signal.SIGTERM) | |
154 time.sleep(2) | |
155 pidf.acquire(0) | |
156 pidf.release() | |
157 else: | |
158 try: | |
159 os.kill(pid, 0) | |
160 # must still be running PID | |
161 raise e | |
162 except OSError: | |
163 stale = True | |
164 | |
165 if stale: | |
166 # isn't still running, steal the lock | |
167 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid) | |
168 pidf.break_lock() | |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
169 |
199 | 170 if do_hup: |
171 print>>sys.stderr, "Doesn't seem to be running" | |
172 sys.exit(1) | |
173 | |
145 | 174 if '--daemon' in sys.argv: |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
175 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
|
176 logf = open(logpath, 'a+') |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
177 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
178 start() |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
179 else: |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
180 with pidf: |
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
181 start() |
145 | 182 |
183 if __name__ == '__main__': | |
184 main() |