From 4c7974ae8b746ff4dbdedcbd79c2d6abfb6b147d Mon Sep 17 00:00:00 2001 From: Tianyong Qiu Date: Fri, 21 Feb 2025 00:24:56 +0800 Subject: [PATCH] Update for start and restart logic --- devbox/devbox.local/devbox | 186 +++++++++++++++++++++++++------------ 1 file changed, 128 insertions(+), 58 deletions(-) diff --git a/devbox/devbox.local/devbox b/devbox/devbox.local/devbox index ffde28b..218ecca 100644 --- a/devbox/devbox.local/devbox +++ b/devbox/devbox.local/devbox @@ -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