Update for start and restart logic

This commit is contained in:
Tianyong Qiu 2025-02-21 00:24:56 +08:00
parent 1e25c6452b
commit 4c7974ae8b

View File

@ -1472,29 +1472,36 @@ devbox_start_command() {
# Start the backend and frontend services
echo "Starting backend and frontend services..."
# Check if /home/.devbox/.backend.pid exists
if [ ! -f /home/.devbox/.backend.pid ]; then
echo "ERROR: Backend service is not running. Please run 'devbox init' first."
exit 1
fi
# Check if /home/.devbox/.frontend.pid exists
if [ ! -f /home/.devbox/.frontend.pid ]; then
echo "ERROR: Frontend service is not running. Please run 'devbox init' first."
exit 1
fi
# Start the backend service
echo '============================================'
echo ' Start to run start_webapi.sh'
echo ' Start to run webapi.main:app '
echo '============================================'
pushd /home/.devbox/freeleaps/apps
# CHeck if the virtual environment is created
if [ ! -f "venv_t/bin/activate" ]; then
echo "ERROR: The virtual environment cannot be created"
exit 1
fi
echo '============================================'
echo ' Start to activate virtual environment'
echo '============================================'
source venv_t/bin/activate
source /home/.devbox/freeleaps/apps/.env
# Verify the virtual environment is activated
if [[ "\$VIRTUAL_ENV" != "" ]]; then
echo "Virtual environment activate: \$VIRTUAL_ENV"
else
echo "ERROR: The virtual environment cannot be startup \$VIRTUAL_ENV"
exit 1
fi
# Check if the backend service is already running
SERVICE_API_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port)
uvicorn webapi.main:app --reload --host 0.0.0.0 --port \$SERVICE_API_ACCESS_PORT > /home/.devbox/logs/backend.logs 2>&1 &
SERVICE_API_ACCESS_PORT=\$(cat /home/.devbox/.devbox-backend-port)
uvicorn freeleaps.webapi.main:app --reload --host 0.0.0.0 --port \$SERVICE_API_ACCESS_PORT > /home/.devbox/logs/backend.logs 2>&1 &
BACKEND_PID=\$!
# Save BACKEND_PID to a file \${WORKING_HOME}/.backend.pid: Stores the process id of backend process.
@ -1507,6 +1514,28 @@ if ! ps -p "\$BACKEND_PID" &>/dev/null; then
exit 1
fi
# Test backend and frontend services
echo "Testing backend and frontend services..."
# Test the backend service
echo "Testing backend service..."
attempt=0
max_attempts=10
while [ \$attempt -lt \$max_attempts ]; do
http_code=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs")
if [ "\$http_code" -eq 200 ]; then
break
fi
attempt=\$((attempt+1))
sleep 5
done
if [ \$attempt -eq \$max_attempts ]; then
echo "ERROR: Backend service is not available after \$max_attempts attempts."
exit 1
fi
# Start the frontend service
echo '============================================'
@ -1526,23 +1555,29 @@ if ! ps -p "\$FRONTEND_PID" &>/dev/null; then
exit 1
fi
# Test backend and frontend services
echo "Testing backend and frontend services..."
# Test the backend service
echo "Testing backend service..."
curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs"
if [ "\$?" -ne 0 ]; then
echo "ERROR: Backend service is not available."
exit 1
fi
# Test the frontend service
echo "Testing frontend service..."
curl -s -o /dev/null -w "%{http_code}" "http://localhost:5173/"
if [ "\$?" -ne 0 ]; then
echo "ERROR: Frontend service is not available."
WEB_APP_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port)
echo "Testing frontend service... PORT: \$WEB_APP_ACCESS_PORT"
attempt=0
max_attempts=10
while [ \$attempt -lt \$max_attempts ]; do
HTTP_CODE=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$WEB_APP_ACCESS_PORT/")
# Check HTTP Code 200
if [ "\$HTTP_CODE" -eq 200 ]; then
echo "Frontend is available (HTTP \$HTTP_CODE)"
break
else
echo "Attempt \$((attempt+1)): Frontend not available (HTTP \$HTTP_CODE). Waiting..."
attempt=\$((attempt+1))
sleep 10
fi
done
if [ \$attempt -eq \$max_attempts ]; then
echo "ERROR: Frontend service is not available after \$max_attempts attempts."
exit 1
fi
@ -1826,29 +1861,38 @@ if [[ "$FREELEAPS_ENDPOINT" == "true" ]]; then
# Start the backend and frontend services
echo "Starting backend and frontend services..."
# Check if /home/.devbox/.backend.pid exists
if [ ! -f /home/.devbox/.backend.pid ]; then
echo "ERROR: Backend service is not running. Please run 'devbox init' first."
exit 1
fi
# Check if /home/.devbox/.frontend.pid exists
if [ ! -f /home/.devbox/.frontend.pid ]; then
echo "ERROR: Frontend service is not running. Please run 'devbox init' first."
exit 1
fi
# Start the backend service
echo '============================================'
echo ' Start to run start_webapi.sh'
echo ' Start to run webapi.main:app'
echo '============================================'
pushd /home/.devbox/freeleaps/apps
# CHeck if the virtual environment is created
if [ ! -f "venv_t/bin/activate" ]; then
echo "ERROR: The virtual environment cannot be created"
exit 1
fi
echo '============================================'
echo ' Start to activate virtual environment'
echo '============================================'
source venv_t/bin/activate
source /home/.devbox/freeleaps/apps/.env
# Verify the virtual environment is activated
if [[ "\$VIRTUAL_ENV" != "" ]]; then
echo "Virtual environment activate: \$VIRTUAL_ENV"
else
echo "ERROR: The virtual environment cannot be startup \$VIRTUAL_ENV"
exit 1
fi
# Check if the backend service is already running
SERVICE_API_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port)
uvicorn webapi.main:app --reload --host 0.0.0.0 --port \$SERVICE_API_ACCESS_PORT > /home/.devbox/logs/backend.logs 2>&1 &
SERVICE_API_ACCESS_PORT=\$(cat /home/.devbox/.devbox-backend-port)
uvicorn freeleaps.webapi.main:app --reload --host 0.0.0.0 --port \$SERVICE_API_ACCESS_PORT > /home/.devbox/logs/backend.logs 2>&1 &
BACKEND_PID=\$!
# Save BACKEND_PID to a file \${WORKING_HOME}/.backend.pid: Stores the process id of backend process.
@ -1861,6 +1905,28 @@ if ! ps -p "\$BACKEND_PID" &>/dev/null; then
exit 1
fi
# Test backend and frontend services
echo "Testing backend and frontend services..."
# Test the backend service
echo "Testing backend service..."
attempt=0
max_attempts=10
while [ \$attempt -lt \$max_attempts ]; do
http_code=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs")
if [ "\$http_code" -eq 200 ]; then
break
fi
attempt=\$((attempt+1))
sleep 5
done
if [ \$attempt -eq \$max_attempts ]; then
echo "ERROR: Backend service is not available after \$max_attempts attempts."
exit 1
fi
# Start the frontend service
echo '============================================'
@ -1880,23 +1946,27 @@ if ! ps -p "\$FRONTEND_PID" &>/dev/null; then
exit 1
fi
# Test backend and frontend services
echo "Testing backend and frontend services..."
# Test the backend service
echo "Testing backend service..."
curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$SERVICE_API_ACCESS_PORT/docs"
if [ "\$?" -ne 0 ]; then
echo "ERROR: Backend service is not available."
exit 1
fi
# Test the frontend service
WEB_APP_ACCESS_PORT=\$(cat /home/.devbox/.devbox-frontend-port)
echo "Testing frontend service..."
curl -s -o /dev/null -w "%{http_code}" "http://localhost:5173/"
if [ "\$?" -ne 0 ]; then
echo "ERROR: Frontend service is not available."
attempt=0
max_attempts=10
while [ \$attempt -lt \$max_attempts ]; do
http_code=\$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:\$WEB_APP_ACCESS_PORT/")
if [ "\$http_code" -eq 200 ]; then
echo "Frontend service is available (HTTP \$http_code)"
break
else
echo "Attempt \$((attempt+1)): Frontend not available (HTTP \$http_code). Waiting..."
attempt=\$((attempt+1))
sleep 10
fi
done
if [ \$attempt -eq \$max_attempts ]; then
echo "ERROR: Frontend service is not available after \$max_attempts attempts."
exit 1
fi