annotate py/tempserver.py @ 471:4414bf8dddaa

use better pidlockfile
author Matt Johnston <matt@ucc.asn.au>
date Fri, 14 Jun 2013 23:32:06 +0800
parents a91adc95543d
children c49d87bb81b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
1 #!/home/matt/templog/venv/bin/python
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import sys
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
6
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 import gevent
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
8 import gevent.monkey
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
9 import lockfile.pidlockfile
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
10 import daemon
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 import utils
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
13 from utils import L,D,EX,W
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
14 import fridge
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
15 import config
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
16 import sensor_ds18b20
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
17 import params
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
18 import uploader
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
19
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 class Tempserver(object):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 def __init__(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 self.readings = []
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
24 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
25 self.fridge = None
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
26
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
27 # don't patch os, fork() is used by daemonize
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
28 gevent.monkey.patch_all(os=False, thread=False)
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
30 def __enter__(self):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
31 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
32 self.fridge = fridge.Fridge(self)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
33 self.uploader = uploader.Uploader(self)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
34 self.params.load()
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
35 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
36 return self
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
38 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
39 L("Exiting, cleanup handler");
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
40 self.fridge.off()
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
42 def run(self):
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
43
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
44 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
45 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
46
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
47 # 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
48 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
49 self.fridge.start()
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
50 self.sensors.start()
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
51 self.uploader.start()
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
52
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
53 # won't return.
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
54 while True:
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
55 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
56 gevent.sleep(60)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
57 except KeyboardInterrupt:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
58 break
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
59
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
60 def now(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
61 return utils.monotonic_time()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
62
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
63 def set_sensors(self, sensors):
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
64 if hasattr(self, 'sensors'):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
65 self.sensors.kill()
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
66 self.sensors = sensors
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
67 self.wort_name = sensors.wort_name()
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
68 self.fridge_name = sensors.fridge_name()
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 def take_readings(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 ret = self.readings
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 self.readings = []
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 return ret
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 def pushfront(self, readings):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 """ used if a caller of take_readings() fails """
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
77 self.readings = readings + self.readings
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
79 # a reading is a map of {sensorname: value}. temperatures
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
80 # are float degrees
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 def add_reading(self, reading):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 """ adds a reading at the current time """
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
83 D("add_reading(%s)" % str(reading))
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
84 self.readings.append( (reading, self.now()))
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
85 self.current = (reading.get(self.wort_name, None),
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
86 reading.get(self.fridge_name, None))
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
87 if len(self.readings) > config.MAX_READINGS:
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
88 self.readings = self.readings[-config.MAX_READINGS:]
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 def current_temps(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 442
diff changeset
91 """ returns (wort_temp, fridge_temp) tuple """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
92 return self.current
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
93
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
94 def setup_logging():
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
95 logging.basicConfig(format='%(asctime)s %(message)s',
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
96 datefmt='%m/%d/%Y %I:%M:%S %p',
462
a91adc95543d less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 461
diff changeset
97 level=logging.INFO)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
98
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
99 def start():
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
100 with Tempserver() as server:
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
101 server.run()
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
102
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
103 def main():
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
104 setup_logging()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
105
471
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
106 pidpath = os.path.join(os.path.dirname(__file__), 'tempserver.pid')
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
107 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
108 try:
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
109 pidf.acquire(0)
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
110 pidf.release()
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
111 except lockfile.AlreadyLocked, e:
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
112 pid = pidf.read_pid()
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
113 print>>sys.stderr, "Locked by PID %d" % pid
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
114 if pid > 0:
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
115 try:
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
116 os.kill(pid, 0)
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
117 # must still be running PID
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
118 raise e
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
119 except OSError:
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
120 # isn't still running, steal the lock
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
121 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
4414bf8dddaa use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
122 pidf.break_lock()
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
123
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
124 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
125 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
126 logf = open(logpath, 'a+')
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
127 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
128 start()
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
129 else:
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
130 with pidf:
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
131 start()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
132
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
133 if __name__ == '__main__':
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
134 main()