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

perf(scripts): improve load balancing #2165

Merged
merged 10 commits into from
Oct 14, 2024
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() {
mabw-rte marked this conversation as resolved.
Show resolved Hide resolved
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 &
mabw-rte marked this conversation as resolved.
Show resolved Hide resolved
pids+=($!) # Store background process IDs
done
for pid in ${pids[*]};
do
wait $pid # Wait for each background process to finish
done
else
mabw-rte marked this conversation as resolved.
Show resolved Hide resolved
export PYTHONPATH=$BASE_DIR
python3 $BASE_DIR/antarest/main.py -c $ANTAREST_CONF --module "$1"
fi
fi
Loading