add cpu split logic and only wait for postgres if we're going to connec to it

This commit is contained in:
msukkari 2025-02-21 17:13:35 -08:00
parent dc42a76e03
commit ced6c527ba
3 changed files with 45 additions and 16 deletions

View file

@ -87,6 +87,9 @@ ENV DATABASE_URL="postgresql://postgres@localhost:5432/sourcebot"
ENV REDIS_URL="redis://localhost:6379" ENV REDIS_URL="redis://localhost:6379"
ENV SRC_TENANT_ENFORCEMENT_MODE=strict ENV SRC_TENANT_ENFORCEMENT_MODE=strict
# @nocheckin: we likely want to have a better solution for the self host case
ENV BACKEND_CORES_FACTOR="0.5"
ARG SOURCEBOT_VERSION=unknown ARG SOURCEBOT_VERSION=unknown
ENV SOURCEBOT_VERSION=$SOURCEBOT_VERSION ENV SOURCEBOT_VERSION=$SOURCEBOT_VERSION
RUN echo "Sourcebot Version: $SOURCEBOT_VERSION" RUN echo "Sourcebot Version: $SOURCEBOT_VERSION"
@ -112,7 +115,7 @@ ENV POSTHOG_PAPIK=$POSTHOG_PAPIK
ENV STRIPE_PUBLISHABLE_KEY="" ENV STRIPE_PUBLISHABLE_KEY=""
# Configure dependencies # Configure dependencies
RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl perl jq redis postgresql postgresql-contrib openssl RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl perl jq redis postgresql postgresql-contrib openssl util-linux
# Configure zoekt # Configure zoekt
COPY vendor/zoekt/install-ctags-alpine.sh . COPY vendor/zoekt/install-ctags-alpine.sh .

View file

@ -181,25 +181,52 @@ echo "{\"version\": \"$SOURCEBOT_VERSION\", \"install_id\": \"$SOURCEBOT_INSTALL
# Start the database and wait for it to be ready before starting any other service # Start the database and wait for it to be ready before starting any other service
su postgres -c "postgres -D $DB_DATA_DIR" & if [ "$DATABASE_URL" = "postgresql://postgres@localhost:5432/sourcebot" ]; then
until pg_isready -h localhost -p 5432 -U postgres; do su postgres -c "postgres -D $DB_DATA_DIR" &
until pg_isready -h localhost -p 5432 -U postgres; do
echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m" echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m"
sleep 1 sleep 1
done done
# Check if the database already exists, and create it if it dne # Check if the database already exists, and create it if it dne
EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'") EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'")
if [ "$EXISTING_DB" = "1" ]; then if [ "$EXISTING_DB" = "1" ]; then
echo "Database '$DB_NAME' already exists; skipping creation." echo "Database '$DB_NAME' already exists; skipping creation."
else else
echo "Creating database '$DB_NAME'..." echo "Creating database '$DB_NAME'..."
psql -U postgres -c "CREATE DATABASE \"$DB_NAME\"" psql -U postgres -c "CREATE DATABASE \"$DB_NAME\""
fi
fi fi
# Run a Database migration # Run a Database migration
echo -e "\e[34m[Info] Running database migration...\e[0m" echo -e "\e[34m[Info] Running database migration...\e[0m"
yarn workspace @sourcebot/db prisma:migrate:prod yarn workspace @sourcebot/db prisma:migrate:prod
# Get total CPU cores
TOTAL_CORES=$(nproc)
echo -e "\e[34m[Info] Total CPU cores available: $TOTAL_CORES\e[0m"
# Default to 50/50 split if BACKEND_CORES_FACTOR not set
BACKEND_CORES_FACTOR=${BACKEND_CORES_FACTOR:-0.5}
# Calculate number of cores for backend (rounded down)
BACKEND_CORES=$(printf "%.0f" $(echo "$TOTAL_CORES * $BACKEND_CORES_FACTOR" | bc))
# Ensure at least 1 core for each
if [ $BACKEND_CORES -lt 1 ]; then
BACKEND_CORES=1
fi
if [ $BACKEND_CORES -ge $TOTAL_CORES ]; then
BACKEND_CORES=$(($TOTAL_CORES - 1))
fi
# Generate CPU lists
export BACKEND_CPU_LIST=$(seq -s, 0 $(($BACKEND_CORES - 1)))
export FRONTEND_CPU_LIST=$(seq -s, $BACKEND_CORES $(($TOTAL_CORES - 1)))
echo -e "\e[34m[Info] Backend will use CPUs: $BACKEND_CPU_LIST\e[0m"
echo -e "\e[34m[Info] Frontend will use CPUs: $FRONTEND_CPU_LIST\e[0m"
# Run supervisord # Run supervisord
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf exec supervisord -c /etc/supervisor/conf.d/supervisord.conf

View file

@ -4,7 +4,7 @@ logfile=/dev/null
logfile_maxbytes=0 logfile_maxbytes=0
[program:zoekt] [program:zoekt]
command=./prefix-output.sh zoekt-webserver -index %(ENV_DATA_CACHE_DIR)s/index -rpc command=taskset -c %(ENV_BACKEND_CPU_LIST)s ./prefix-output.sh zoekt-webserver -index %(ENV_DATA_CACHE_DIR)s/index -rpc
autostart=true autostart=true
autorestart=true autorestart=true
startretries=3 startretries=3
@ -13,7 +13,7 @@ stdout_logfile_maxbytes=0
redirect_stderr=true redirect_stderr=true
[program:web] [program:web]
command=./prefix-output.sh node packages/web/server.js command=taskset -c %(ENV_FRONTEND_CPU_LIST)s ./prefix-output.sh node packages/web/server.js
autostart=true autostart=true
autorestart=true autorestart=true
startretries=3 startretries=3
@ -21,7 +21,6 @@ stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0 stdout_logfile_maxbytes=0
redirect_stderr=true redirect_stderr=true
[program:backend] [program:backend]
command=./prefix-output.sh node packages/backend/dist/index.js --cacheDir %(ENV_DATA_CACHE_DIR)s command=./prefix-output.sh node packages/backend/dist/index.js --cacheDir %(ENV_DATA_CACHE_DIR)s
autostart=true autostart=true