Skip to content

Commit d0cd6bf

Browse files
authored
Merge pull request #3380 from babsey/nest-server-gunicorn-opts
Enhance gunicorn options in NEST Server
2 parents 88c47b9 + 251b0f5 commit d0cd6bf

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

bin/nest-server

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ HOST="${NEST_SERVER_HOST:-127.0.0.1}"
55
LOGFILE="${NEST_SERVER_LOGFILE:-/tmp/nest-server.log}"
66
PORT="${NEST_SERVER_PORT:-52425}"
77
STDOUT="${NEST_SERVER_STDOUT:-0}"
8+
TIMEOUT="${NEST_SERVER_TIMEOUT:-30}"
9+
WORKERS="${NEST_SERVER_WORKERS:-1}"
810

911
usage() {
1012
echo "NEST Server"
1113
echo "-----------"
12-
echo "Usage: nest-server log|status|start|stop|restart [-d] [-h <HOST>] [-o] [-p <PORT>]"
14+
echo "Usage: nest-server log|status|start|stop|restart [-d] [-h <HOST>] [-o] [-p <PORT>] [-t <TIMEOUT>] [-w <WORKERS>]"
1315
echo ""
1416
echo "Commands:"
1517
echo " log display the server output log"
@@ -23,6 +25,8 @@ usage() {
2325
echo " -h <HOST> use hostname/IP address <HOST> for the server [default: 127.0.0.1]"
2426
echo " -o print NEST outputs to the console"
2527
echo " -p <PORT> use port <PORT> for opening the socket [default: 52425]"
28+
echo " -t <TIMEOUT> workers silent for more than this many seconds are killed and restarted [default: 30]"
29+
echo " -w <WORKERS> the number of worker processes for handling requests [default: 1]"
2630
}
2731

2832
log() {
@@ -38,13 +42,11 @@ pid() {
3842
set-gunicorn_opts() {
3943
# Set opts for gunicorn.
4044
GUNICORN_OPTS="--bind ${HOST}:${PORT}"
41-
if [ "${DAEMON}" -eq 1 ]; then
42-
GUNICORN_OPTS="${GUNICORN_OPTS} --daemon"
43-
fi
44-
if [ "${STDOUT}" -eq 0 ]; then
45-
GUNICORN_OPTS="${GUNICORN_OPTS} --capture-output"
46-
fi
4745
GUNICORN_OPTS="${GUNICORN_OPTS} --log-file ${LOGFILE}"
46+
[[ "${DAEMON}" -eq 1 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --daemon"
47+
[[ "${STDOUT}" -eq 0 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --capture-output"
48+
[[ "${TIMEOUT}" -ne 30 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --timeout ${TIMEOUT}"
49+
[[ "${WORKERS}" -gt 1 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --workers ${WORKERS}"
4850
}
4951

5052
start() {
@@ -55,21 +57,21 @@ start() {
5557
echo "NEST Server is now running at http://${HOST}:${PORT}."
5658
if [ "${DAEMON}" -eq 0 ]; then
5759
echo "Use CTRL + C to stop this service."
58-
if [ "${STDOUT}" -eq 1 ]; then
59-
echo "-----------------------------------------------------"
60-
fi
60+
[[ "${STDOUT}" -eq 1 ]] && echo "-----------------------------------------------------"
6161
fi
6262

6363
set-gunicorn_opts
64+
# shellcheck disable=SC2086
6465
exec gunicorn nest.server:app ${GUNICORN_OPTS}
6566
fi
6667
}
6768

6869
status() {
6970
# List all processes of NEST Server.
70-
PS_AUX="$(ps aux | grep "[g]unicorn nest.server.app")"
71-
printf "USER\t\t\tPID\t\tHTTP-SOCKET\n"
72-
echo "${PS_AUX}" | head -n 1 | awk '{ for(i=1;i<=NF;i++) {if ( i == 1 || i == 2 || i == 15 ) printf $i"\t\t"}; printf "\n" }'
71+
PS_AUX="$(pgrep -af "gunicorn nest.server.app")"
72+
printf "PID\t\tHTTP-SOCKET\t\tLOGFILE\n"
73+
echo "${PS_AUX}" | head -n 1 | awk \
74+
'{ for(i=1;i<=NF;i++) {if ( i == 1 || i == 6 || i == 8 ) printf $i"\t\t"}; printf "\n" }'
7375
}
7476

7577
stop() {
@@ -84,12 +86,15 @@ stop() {
8486
}
8587

8688
CMD=$1; shift
87-
while getopts "dh:op:" opt; do
89+
while getopts "dh:op:t:w:" opt; do
8890
case $opt in
8991
d) DAEMON=1 ;;
9092
h) HOST=$OPTARG ;;
9193
o) STDOUT=1 ;;
9294
p) PORT=$OPTARG ;;
95+
t) TIMEOUT=$OPTARG ;;
96+
w) WORKERS=$OPTARG ;;
97+
*) echo "Invalid option"
9398
esac
9499
done
95100

0 commit comments

Comments
 (0)