Skip to content

Commit cbc8f8b

Browse files
committed
More error proof run and stop scripts. Gracefull shutdown.
1 parent 2464d8a commit cbc8f8b

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

MpdActor.py

+3
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ def playByNameFrom(self, name, playFrom):
4040

4141
except mpd.CommandError, e:
4242
logging.getLogger('zbap').error('Exception: %s' % e.message)
43+
44+
def pause(self):
45+
self.client.pause()

WebActor.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pykka, web
1+
import pykka, web, logging
22

33
tagActor = None
44

@@ -24,8 +24,11 @@ def runInThread(function):
2424
t.start()
2525

2626
def runWebApp():
27-
app = web.application(urls, globals())
28-
app.run()
27+
try:
28+
app = web.application(urls, globals())
29+
app.run()
30+
except Exception:
31+
logging.getLogger('zbap').info('Shutting down web server.')
2932

3033
class WebActor(pykka.ThreadingActor):
3134
def __init__(self, tagAct):

Zbap.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pykka, logging
1+
import pykka, logging, signal
22

33
from time import sleep
44

@@ -9,8 +9,16 @@
99
from NfcActor import NfcActor
1010

1111
sleepSeconds = 0.5
12+
pleaseContinue = True
1213

13-
if __name__ == "__main__":
14+
def quitGracefully(*args):
15+
global pleaseContinue
16+
logging.getLogger('zbap').info('Received SIGINT.')
17+
pleaseContinue = False
18+
19+
def run():
20+
global pleaseContinue
21+
signal.signal(signal.SIGINT, quitGracefully)
1422

1523
logging.basicConfig(filename='zbap.log', format="%(asctime)s [%(module)s.%(funcName)s] %(levelname)s: %(message)s")
1624
logging.getLogger('pykka').setLevel(logging.DEBUG)
@@ -27,12 +35,18 @@
2735
stateActor.playFromLastState()
2836

2937
try:
30-
while True:
38+
while pleaseContinue:
3139
stateActor.tick()
3240
nfcActor.tick()
3341

3442
sleep(sleepSeconds)
3543
except KeyboardInterrupt:
36-
print "Exiting main loop."
44+
logging.getLogger('zbap').info('Received KeyboardInterrupt.')
45+
46+
logging.getLogger('zbap').info('Stopping...')
3747

48+
mpdActor.pause()
3849
pykka.ActorRegistry.stop_all()
50+
51+
if __name__ == "__main__":
52+
run()

run.sh

+10
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
#!/bin/bash
2+
3+
CURRENT_PID=`pgrep -f "python Zbap.py"`
4+
if [ -n "${CURRENT_PID}" ]; then
5+
echo "There is already ZBAP running with PID:$CURRENT_PID"
6+
exit
7+
fi
8+
9+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
10+
11+
cd $CURRENT_DIR
212
python Zbap.py &

stop.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
#!/bin/bash
2-
ps a | grep "python Zbap.py" | cut -d" " -f 2 | xargs kill -9
2+
CURRENT_PID=`pgrep -f "python Zbap.py"`
3+
if [ -n "${CURRENT_PID}" ]; then
4+
kill -SIGINT $CURRENT_PID
5+
else
6+
echo "Zbap is not running."
7+
fi

0 commit comments

Comments
 (0)