diff --git a/bin/nest-server b/bin/nest-server index 4c226a37c7..65203ee139 100755 --- a/bin/nest-server +++ b/bin/nest-server @@ -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 ] [-o] [-p ]" + echo "Usage: nest-server log|status|start|stop|restart [-d] [-h ] [-o] [-p ] [-t ] [-w ]" echo "" echo "Commands:" echo " log display the server output log" @@ -23,6 +25,8 @@ usage() { echo " -h use hostname/IP address for the server [default: 127.0.0.1]" echo " -o print NEST outputs to the console" echo " -p use port for opening the socket [default: 52425]" + echo " -t workers silent for more than this many seconds are killed and restarted [default: 30]" + echo " -w the number of worker processes for handling requests [default: 1]" } log() { @@ -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() { @@ -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() { @@ -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