Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance gunicorn options in NEST Server #3380

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions bin/nest-server
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ HOST="${NEST_SERVER_HOST:-127.0.0.1}"
LOGFILE="${NEST_SERVER_LOGFILE:-/tmp/nest-server.log}"
PORT="${NEST_SERVER_PORT:-52425}"
STDOUT="${NEST_SERVER_STDOUT:-0}"
TIMEOUT="${NEST_SERVER_TIMEOUT:-30}"
WORKERS="${NEST_SERVER_WORKERS:-1}"

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

log() {
Expand All @@ -38,13 +42,11 @@ pid() {
set-gunicorn_opts() {
# Set opts for gunicorn.
GUNICORN_OPTS="--bind ${HOST}:${PORT}"
if [ "${DAEMON}" -eq 1 ]; then
GUNICORN_OPTS="${GUNICORN_OPTS} --daemon"
fi
if [ "${STDOUT}" -eq 0 ]; then
GUNICORN_OPTS="${GUNICORN_OPTS} --capture-output"
fi
GUNICORN_OPTS="${GUNICORN_OPTS} --log-file ${LOGFILE}"
[[ "${DAEMON}" -eq 1 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --daemon"
[[ "${STDOUT}" -eq 0 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --capture-output"
[[ "${TIMEOUT}" -ne 30 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --timeout ${TIMEOUT}"
[[ "${WORKERS}" -gt 1 ]] && GUNICORN_OPTS="${GUNICORN_OPTS} --workers ${WORKERS}"
}

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

set-gunicorn_opts
# shellcheck disable=SC2086
exec gunicorn nest.server:app ${GUNICORN_OPTS}
fi
}

status() {
# List all processes of NEST Server.
PS_AUX="$(ps aux | grep "[g]unicorn nest.server.app")"
printf "USER\t\t\tPID\t\tHTTP-SOCKET\n"
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" }'
PS_AUX="$(pgrep -af "gunicorn nest.server.app")"
printf "PID\t\tHTTP-SOCKET\t\tLOGFILE\n"
echo "${PS_AUX}" | head -n 1 | awk \
'{ for(i=1;i<=NF;i++) {if ( i == 1 || i == 6 || i == 8 ) printf $i"\t\t"}; printf "\n" }'
}

stop() {
Expand All @@ -84,12 +86,15 @@ stop() {
}

CMD=$1; shift
while getopts "dh:op:" opt; do
while getopts "dh:op:t:w:" opt; do
case $opt in
d) DAEMON=1 ;;
h) HOST=$OPTARG ;;
o) STDOUT=1 ;;
p) PORT=$OPTARG ;;
t) TIMEOUT=$OPTARG ;;
w) WORKERS=$OPTARG ;;
*) echo "Invalid option"
esac
done

Expand Down
Loading