annotate py/tempserver.py @ 229:99255c501e02

test sensor
author Matt Johnston <matt@ucc.asn.au>
date Fri, 10 Apr 2015 23:55:07 +0800
parents d9e81a563923
children 185621f47040
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
1 #!/home/matt/templog/venv/bin/python
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import sys
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
6 import time
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
7 import signal
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
8 import asyncio
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
9
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
10 import lockfile.pidlockfile
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
11 import daemon
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 import utils
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
14 from utils import L,D,EX,W
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
15 import fridge
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
16 import config
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
17 import sensor_ds18b20
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
18 import params
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
19 import uploader
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
20
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 class Tempserver(object):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 def __init__(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 self.readings = []
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
25 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
26 self.fridge = None
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
27 self._wakeup = asyncio.Event()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
29 def __enter__(self):
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
30 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
31 self.fridge = fridge.Fridge(self)
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
32 self.uploader = uploader.Uploader(self)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
33 self.params.load()
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
34 self.set_sensors(sensor.make_sensor(self))
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
35 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
36 return self
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
38 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
39 L("Exiting, cleanup handler");
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
40 self.fridge.off()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
42 def run(self):
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
43
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
44 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
45 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
46
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
47 # 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
48 self.start_time = self.now()
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
49 tasks = (
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
50 self.fridge.run(),
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
51 self.sensors.run(),
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
52 self.uploader.run(),
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
53 )
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
54 loop = asyncio.get_event_loop()
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
55 try:
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
56 loop.run_until_complete(asyncio.wait(tasks))
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
57 # not reached
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
58 except KeyboardInterrupt:
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
59 pass
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
60
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
61 def now(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
62 return utils.monotonic_time()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
63
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
64 def set_sensors(self, sensors):
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
65 if hasattr(self, 'sensors'):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
66 self.sensors.kill()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
67 self.sensors = sensors
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
68 self.wort_name = sensors.wort_name()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
69 self.fridge_name = sensors.fridge_name()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 def take_readings(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 ret = self.readings
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 self.readings = []
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 return ret
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 def pushfront(self, readings):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 """ used if a caller of take_readings() fails """
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
78 self.readings = readings + self.readings
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
80 # a reading is a map of {sensorname: value}. temperatures
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
81 # are float degrees
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 def add_reading(self, reading):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 """ adds a reading at the current time """
155
d03157c7ad60 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
84 D("add_reading(%s)" % str(reading))
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
85 self.readings.append( (reading, self.now()))
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
86 self.current = (reading.get(self.wort_name, None),
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
87 reading.get(self.fridge_name, None))
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
88 if len(self.readings) > config.MAX_READINGS:
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
89 self.readings = self.readings[-config.MAX_READINGS:]
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 def current_temps(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
92 """ returns (wort_temp, fridge_temp) tuple """
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
93 return self.current
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
94
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
95 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
96 """ sleeps for timeout seconds, though wakes if the server's config is updated """
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
97 asyncio.wait_for(self._wakeup, timeout=timeout)
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
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 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
100 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
101 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
102 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
103 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
104 self._wakeup.clear()
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
105 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
106 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
107
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
108 def setup_logging():
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
109 logging.basicConfig(format='%(asctime)s %(message)s',
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
110 datefmt='%m/%d/%Y %I:%M:%S %p',
163
bf2a17873ba1 less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 162
diff changeset
111 level=logging.INFO)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
112
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
113 def start():
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
114 with Tempserver() as server:
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
115 server.run()
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
116
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
117 def main():
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
118 setup_logging()
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
119
174
c49d87bb81b9 fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
120 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
121 pidpath = os.path.join(heredir, 'tempserver.pid')
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
122 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
123 do_hup = '--hup' in sys.argv
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
124 try:
217
5eb7e2400c18 fix pid locking
Matt Johnston <matt@ucc.asn.au>
parents: 199
diff changeset
125 pidf.acquire(1)
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
126 pidf.release()
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
127 except (lockfile.AlreadyLocked, lockfile.LockTimeout) as e:
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
128 pid = pidf.read_pid()
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
129 if do_hup:
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
130 try:
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
131 os.kill(pid, signal.SIGHUP)
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
132 print>>sys.stderr, "Sent SIGHUP to process %d" % pid
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
133 sys.exit(0)
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
134 except OSError:
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
135 print>>sys.stderr, "Process %d isn't running?" % pid
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
136 sys.exit(1)
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
137
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
138 print>>sys.stderr, "Locked by PID %d" % pid
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
139
198
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
140 stale = False
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
141 if pid > 0:
198
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
142 if '--new' in sys.argv:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
143 try:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
144 os.kill(pid, 0)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
145 except OSError:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
146 stale = True
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
147
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
148 if not stale:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
149 print>>sys.stderr, "Stopping old tempserver pid %d" % pid
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
150 os.kill(pid, signal.SIGTERM)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
151 time.sleep(2)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
152 pidf.acquire(0)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
153 pidf.release()
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
154 else:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
155 try:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
156 os.kill(pid, 0)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
157 # must still be running PID
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
158 raise e
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
159 except OSError:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
160 stale = True
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
161
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
162 if stale:
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
163 # isn't still running, steal the lock
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
164 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
be579bcf3c7f Add --new flag
Matt Johnston <matt@ucc.asn.au>
parents: 174
diff changeset
165 pidf.break_lock()
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
166
199
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
167 if do_hup:
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
168 print>>sys.stderr, "Doesn't seem to be running"
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
169 sys.exit(1)
2affbaae408b add --hup
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
170
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
171 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
172 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
173 logf = open(logpath, 'a+')
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
174 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
175 start()
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
176 else:
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
177 with pidf:
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
178 start()
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
179
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
180 if __name__ == '__main__':
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
181 main()