document-translation / start-services.sh
dmartincy's picture
Improve service handling
24c2656
raw
history blame
3.32 kB
#!/bin/sh
set -e
# Start Nginx.
echo "Starting nginx..."
/usr/sbin/nginx -c /etc/nginx/nginx.conf
if [ $? -ne 0 ]; then
echo "Failed to start nginx"
exit 1
fi
# Verify nginx is running.
if ! ps aux | grep nginx | grep -v grep > /dev/null; then
echo "Nginx failed to start"
exit 1
fi
echo "Nginx started successfully"
# Start PostgreSQL.
echo "Starting PostgreSQL..."
if [ ! -f "$PGDATA/PG_VERSION" ]; then
mkdir -p $PGDATA
echo "Current user: $(whoami)"
initdb -D $PGDATA --username=postgres --pwfile=<(echo "password")
fi
pg_ctl -D $PGDATA start
# Create the db-user (but let Prisma handle the database)
echo "Creating database user..."
PGPASSWORD=password PGUSER=postgres createuser -s "db-user" || echo "User db-user already exists"
PGPASSWORD=password PGUSER=postgres psql -c 'ALTER USER "db-user" WITH PASSWORD '\''password'\'';' postgres
# Wait for PostgreSQL (using db-user).
echo "Waiting for PostgreSQL..."
until pg_isready -U db-user; do
echo "Waiting for PostgreSQL to be ready..."
sleep 2
if [ $SECONDS -gt 30 ]; then
echo "Timeout waiting for PostgreSQL"
exit 1
fi
done
echo "PostgreSQL is ready!"
# Start Llamafiler.
echo "Starting llamafiler services..."
mkdir -p /tmp/llamafiler
echo "Starting chat model..."
TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/gemma-2b.gguf --listen 0.0.0.0:8082 &
GEMMA_PID=$!
echo "Starting embedding model..."
TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/embeddings.gguf --listen 0.0.0.0:8081 &
EMBEDDINGS_PID=$!
# Wait for the models to be ready with a timeout
echo "Waiting for models to be ready..."
TIMEOUT=300 # 5 minutes timeout
START_TIME=$SECONDS
wait_for_services() {
curl -s --fail -X POST http://127.0.0.1:7861/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "gemma-2b", "messages":[{"role":"user","content":"hi"}]}' >/dev/null 2>&1 && \
curl -s --fail -X POST http://127.0.0.1:7861/v1/embeddings \
-H "Content-Type: application/json" \
-d '{"input":"test"}' >/dev/null 2>&1
}
until wait_for_services; do
ELAPSED=$((SECONDS - START_TIME))
if [ $ELAPSED -gt $TIMEOUT ]; then
echo "Timeout waiting for services after ${TIMEOUT} seconds"
exit 1
fi
# Check if processes are still running
if ! kill -0 $GEMMA_PID 2>/dev/null; then
echo "Gemma model process died."
exit 1
fi
if ! kill -0 $EMBEDDINGS_PID 2>/dev/null; then
echo "Embeddings model process died."
exit 1
fi
echo "Waiting for services... (${ELAPSED}s elapsed, PIDs: Gemma=$GEMMA_PID, Embeddings=$EMBEDDINGS_PID)"
sleep 2
done
echo 'Servers are ready!'
echo 'All services are ready!'
# Ensure we have the same environment as the base image.
export PNPM_HOME=/root/.local/share/pnpm
export PATH=$PNPM_HOME/bin:$PATH
cd /base # Ensure we're in the correct directory
ls -la prisma/
ls -la $PNPM_HOME
ls -la $PNPM_HOME/global/5/node_modules/@prisma || echo "No global prisma found"
prisma -v
# Now execute the entrypoint with the proper environment.
echo "Executing: /base/bin/entrypoint.sh node /base/app/main.bundle.js"
set -x # Enable command tracing.
exec /base/bin/entrypoint.sh node /base/app/main.bundle.js