-
Notifications
You must be signed in to change notification settings - Fork 24
/
startApi.sh
executable file
·81 lines (66 loc) · 1.91 KB
/
startApi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
PYTHONBIN=python
kill_child_processes() {
#kill $SERVER_PID
uwsgi --stop /tmp/omniapi.pid
kill $WEBSOCKET_PID
rm -f $LOCK_FILE
exit 1
}
# Ctrl-C trap. Catches INT and service stop signal
trap kill_child_processes INT TERM SIGINT SIGTERM
echo "Service Starting: $(TZ='UTC' date)"
echo "Establishing environment variables..."
APPDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LOGDIR=$APPDIR/logs
LOCK_FILE=$APPDIR/omniapi.lock
# Export directories for API scripts to use
export LOGDIR
echo "Starting Service watch loop..."
while true
do
# check lock (not to run multiple times)
if [ ! -f $LOCK_FILE ]; then
# lock
touch $LOCK_FILE
#Update debug level
if [ "$1" = "-debug" ]; then
DEBUGLEVEL=$2
else
DEBUGLEVEL=0
fi
export DEBUGLEVEL
ps cax | grep uwsgi > /dev/null
if [ $? -eq 0 ]; then
echo "uwsgi api is running."
else
echo "Starting uwsgi daemon..."
cd $APPDIR/api
uwsgi -s 127.0.0.1:1088 -p 10 -M --vhost --enable-threads --log-x-forwarded-for --logto $LOGDIR/apps.log --pidfile /tmp/omniapi.pid --stats /tmp/stats.socket &
#get snapshot of directory files
APISHA=`ls -lR $APPDIR/api/*.py | sha1sum`
fi
#check if api files have changed
CHECKSHA=`ls -lR $APPDIR/api/*.py | sha1sum`
#Trigger api reload if changed
if [ "$APISHA" != "$CHECKSHA" ]; then
uwsgi --reload /tmp/omniapi.pid
APISHA=$CHECKSHA
echo Api Reloaded
fi
ps aux | grep -v grep | grep "python websocket.py" > /dev/null
if [ $? -eq 0 ]; then
echo "websocket api is running."
else
echo "Starting websocket daemon..."
cd $APPDIR/api
$PYTHONBIN websocket.py > $LOGDIR/websocket.log 2>&1 &
WEBSOCKET_PID=$!
fi
# unlock
rm -f $LOCK_FILE
fi
#echo "Done, sleeping..."
# Wait a minute, and do it all again.
sleep 60
done