Mercurial > templog
annotate py/tempserver.py @ 498:ccc2915b2dd9
Add --new flag
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 04 Mar 2014 22:12:29 +0800 |
parents | bd29ddb360a5 |
children | 2affbaae408b |
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 | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
8 |
439 | 9 import gevent |
444 | 10 import gevent.monkey |
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 | |
18 import sensor_ds18b20 | |
19 import params | |
459 | 20 import uploader |
444 | 21 |
439 | 22 |
23 class Tempserver(object): | |
24 def __init__(self): | |
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 |
444 | 28 |
29 # don't patch os, fork() is used by daemonize | |
30 gevent.monkey.patch_all(os=False, thread=False) | |
439 | 31 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
32 def __enter__(self): |
444 | 33 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
|
34 self.fridge = fridge.Fridge(self) |
459 | 35 self.uploader = uploader.Uploader(self) |
444 | 36 self.params.load() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
37 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
38 return self |
439 | 39 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
40 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
|
41 L("Exiting, cleanup handler"); |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
42 self.fridge.off() |
439 | 43 |
444 | 44 def run(self): |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
45 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
46 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
|
47 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
|
48 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
49 # 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
|
50 self.start_time = self.now() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
51 self.fridge.start() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
52 self.sensors.start() |
459 | 53 self.uploader.start() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
54 |
444 | 55 # won't return. |
56 while True: | |
456 | 57 try: |
58 gevent.sleep(60) | |
59 except KeyboardInterrupt: | |
60 break | |
444 | 61 |
62 def now(self): | |
63 return utils.monotonic_time() | |
64 | |
443 | 65 def set_sensors(self, sensors): |
447 | 66 if hasattr(self, 'sensors'): |
443 | 67 self.sensors.kill() |
68 self.sensors = sensors | |
69 self.wort_name = sensors.wort_name() | |
70 self.fridge_name = sensors.fridge_name() | |
439 | 71 |
72 def take_readings(self): | |
73 ret = self.readings | |
74 self.readings = [] | |
75 return ret | |
76 | |
77 def pushfront(self, readings): | |
78 """ used if a caller of take_readings() fails """ | |
459 | 79 self.readings = readings + self.readings |
439 | 80 |
443 | 81 # a reading is a map of {sensorname: value}. temperatures |
82 # are float degrees | |
439 | 83 def add_reading(self, reading): |
84 """ adds a reading at the current time """ | |
454 | 85 D("add_reading(%s)" % str(reading)) |
444 | 86 self.readings.append( (reading, self.now())) |
443 | 87 self.current = (reading.get(self.wort_name, None), |
88 reading.get(self.fridge_name, None)) | |
459 | 89 if len(self.readings) > config.MAX_READINGS: |
90 self.readings = self.readings[-config.MAX_READINGS:] | |
439 | 91 |
92 def current_temps(self): | |
443 | 93 """ returns (wort_temp, fridge_temp) tuple """ |
447 | 94 return self.current |
444 | 95 |
96 def setup_logging(): | |
97 logging.basicConfig(format='%(asctime)s %(message)s', | |
98 datefmt='%m/%d/%Y %I:%M:%S %p', | |
462 | 99 level=logging.INFO) |
444 | 100 |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
101 def start(): |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
102 with Tempserver() as server: |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
103 server.run() |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
104 |
444 | 105 def main(): |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
106 setup_logging() |
444 | 107 |
473
bd29ddb360a5
fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents:
471
diff
changeset
|
108 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
|
109 pidpath = os.path.join(heredir, 'tempserver.pid') |
471 | 110 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False) |
111 try: | |
112 pidf.acquire(0) | |
113 pidf.release() | |
114 except lockfile.AlreadyLocked, e: | |
115 pid = pidf.read_pid() | |
116 print>>sys.stderr, "Locked by PID %d" % pid | |
498 | 117 stale = False |
471 | 118 if pid > 0: |
498 | 119 if '--new' in sys.argv: |
120 try: | |
121 os.kill(pid, 0) | |
122 except OSError: | |
123 stale = True | |
124 | |
125 if not stale: | |
126 print>>sys.stderr, "Stopping old tempserver pid %d" % pid | |
127 os.kill(pid, signal.SIGTERM) | |
128 time.sleep(2) | |
129 pidf.acquire(0) | |
130 pidf.release() | |
131 else: | |
132 try: | |
133 os.kill(pid, 0) | |
134 # must still be running PID | |
135 raise e | |
136 except OSError: | |
137 stale = True | |
138 | |
139 if stale: | |
140 # isn't still running, steal the lock | |
141 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid) | |
142 pidf.break_lock() | |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
143 |
444 | 144 if '--daemon' in sys.argv: |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
145 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
|
146 logf = open(logpath, 'a+') |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
147 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
148 start() |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
149 else: |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
150 with pidf: |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
151 start() |
444 | 152 |
153 if __name__ == '__main__': | |
154 main() |