Skip to content

Commit

Permalink
perf(scripts): improve load balancing (#2165)
Browse files Browse the repository at this point in the history
gunicorn does not perform a real load balancing.
We add a new option to the start.sh script that starts
multiple workers listening on different ports to let users
perform load balancing upstream if they prefer that option,
for example using nginx.
  • Loading branch information
mabw-rte authored Oct 14, 2024
1 parent 5421a83 commit 610c8d7
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion scripts/start.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
#!/bin/bash
#
# Starter script used in docker image.
#
# Usages are:
#
# 1. ./start.sh
# Starts antares web server with gunicorn.
# Number of workers is defined by GUNICORN_WORKERS env variable,
# or equal to 2*cpu + 1.
#
# 2. ./start.sh <module>
# Where module is for example "watcher".
# Starts one of the backend services (watcher, garbage collector, ...)
#
# 3. ./start.sh --no-gunicorn
# Starts multiple workers on different ports. You are expected to
# configure some load balancing upstream, in that case.
# Number of workers is defined by ANTARES_NB_WORKERS env variable,
# or equal to 2*cpu + 1.

set -e

CUR_DIR=$(cd "$(dirname "$0")" && pwd)
BASE_DIR=$(dirname "$CUR_DIR")

min() {
echo $(( $1 < $2 ? $1 : $2 ))
}

workers=$(min 30 ${ANTARES_NB_WORKERS:-$((2*$(nproc) + 1))}) # default (2*nproc + 1) and max is 30

# Check for --no-gunicorn or --multiple-ports argument
use_uvicorn=false
for arg in "$@"
do
if [[ $arg == "--no-gunicorn" || $arg == "--multiple-ports" ]]; then
use_uvicorn=true
break
fi
done

if [ -z "$1" ] ; then
sh $CUR_DIR/pre-start.sh
gunicorn --config $BASE_DIR/conf/gunicorn.py --worker-class=uvicorn.workers.UvicornWorker antarest.wsgi:app
elif [ "$use_uvicorn" = true ]; then
sh $CUR_DIR/pre-start.sh
pids=() # Initialize empty array to store background process IDs
for ((i=0; i<workers; i++))
do
uvicorn antarest.wsgi:app --host 0.0.0.0 --port $((5000 + $i)) --log-level info --timeout-keep-alive 600 &
pids+=($!) # Store background process IDs
done
for pid in ${pids[*]};
do
wait $pid # Wait for each background process to finish
done
else
export PYTHONPATH=$BASE_DIR
python3 $BASE_DIR/antarest/main.py -c $ANTAREST_CONF --module "$1"
fi
fi

0 comments on commit 610c8d7

Please sign in to comment.