-
Notifications
You must be signed in to change notification settings - Fork 6
/
pooling.sh
executable file
·147 lines (127 loc) · 3.66 KB
/
pooling.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
#This file is part of iPipet.
#copyright (c) 2014 Dina Zielinski ([email protected])
#iPipet is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, either version 3 of the License, or any later version.
#iPipet is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU Affero General Public License
#along with iPipet. If not, see <http://www.gnu.org/licenses/agpl-3.0.html>.
##
## A user-land script, based gunicorn init.d script here:
## https://gist.github.com/suda/748450
##
## 1. Adapted to run one specific application.
## 2. Adapted to run as a non-system user (not ideal, but works)
## 3. Adapter to write logs and PID to the script's directory (not idael, but works)
## 4. minimal support for error reporting - not yet suitible for "/etc/init.d/" usage.
die()
{
BASE=$(basename "$0")
echo "$BASE: error: $@" >&2
exit 1
}
DESC="iPipet"
## The application script name (e.g. "pooling.py")
## This will determine the name of the PID and LOG files, as well.
FLASK_APP_FILE=pooling.py
## The name of the variable containing the flask application ('app' in most cases)
FLASK_APP_VAR_NAME=app
## IP To bind to (commonly: 0.0.0.0 or 127.0.0.1)
BIND_IP=127.0.0.1
BIND_TCP_PORT=5105
## Number of thread for gunicorn
WORKERS=2
## Find the full path of the current directory
## (note: readlink -e only works on Linuxes with GNU Coreutils)
BASEDIR=$(readlink -e $(dirname "$0")) || exit 1
[ -e "$BASEDIR/$FLASK_APP_FILE" ] || die "Flask script '$BASEDIR/$FLASK_APP_FILE' not found"
FLASK_NAME=$(basename "$FLASK_APP_FILE" .py) || exit 1
PIDFILE=$BASEDIR/$FLASK_NAME.pid
ACCESSLOG=$BASEDIR/$FLASK_NAME.access.log
ERRORLOG=$BASEDIR/$FLASK_NAME.error.log
GUNICORN=$(which gunicorn) || die "'gunicorn' program not found"
## For non-root users, 'start-stop-daemon' might not be in the $PATH,
## so find it.
START_STOP_DAEMON=/sbin/start-stop-daemon
[ -x "$START_STOP_DAEMON" ] || die "'start-stop-daemon' not found ($START_STOP_DAEMON)"
start () {
"$START_STOP_DAEMON" --start \
--pidfile "$PIDFILE" \
--chdir "$BASEDIR" \
--exec "$GUNICORN" \
-- \
--bind "$BIND_IP:$BIND_TCP_PORT" \
--workers "$WORKERS" \
--daemon \
--pid "$PIDFILE" \
--preload \
--log-level error \
--access-logfile "$ACCESSLOG" \
--error-logfile "$ERRORLOG" \
"$FLASK_NAME:$FLASK_APP_VAR_NAME"
## Ugly Hack
## 'gunicorn' returns 0 even if the flask application failed to load.
## So wait a bit, then check the status
sleep 2
status
}
stop () {
"$START_STOP_DAEMON" --stop \
--pidfile "$PIDFILE"
}
status () {
"$START_STOP_DAEMON" --status \
--pidfile "$PIDFILE"
}
reload () {
"$START_STOP_DAEMON" --stop \
--signal HUP \
--pidfile "$PIDFILE"
}
case "$1" in
start)
printf "Starting $DESC ..."
start
case "$?" in
0) printf "OK\n" ;;
*) printf "Failed\n" ;;
esac
;;
stop)
printf "Stopping $DESC ..."
stop
case "$?" in
0|1) printf "OK\n" ;;
2) printf "Failed\n" ;;
esac
;;
status)
printf "Status-check $DESC ..."
status
case "$?" in
0) printf "Running.\n" ;;
1) printf "Not running (but PID file exists)\n" ;;
3) printf "Not running\n" ;;
4) printf "Failed to check status\n" ;;
esac
;;
reload)
echo "Reloading $DESC"
reload
;;
restart)
echo "Restarting $DESC"
stop
sleep 1
start
;;
*)
echo "Usage: $(basename $0) {start|stop|status|restart|reload}" >&2
exit 1
;;
esac
exit 0