diff --git a/deploy.sh b/deploy.sh index 0bfd6ec..a2e1481 100755 --- a/deploy.sh +++ b/deploy.sh @@ -76,10 +76,54 @@ ENDSSH echo "✅ Frontend built" echo "" -# 5. Restart Services +# 5. Restart Services with Proper Cleanup echo "🔄 [5/6] Restarting services..." ssh -i $SSH_KEY $HOST << 'ENDSSH' -supervisorctl restart hosting-backend hosting-frontend +# Kill all existing backend processes +echo " 🔪 Stopping all backend processes..." +pkill -f "python.*hosting-platform.*main.py" || true +sleep 2 + +# Force kill if still running +if pgrep -f "python.*hosting-platform.*main.py" > /dev/null; then + echo " ⚠️ Force killing remaining processes..." + pkill -9 -f "python.*hosting-platform.*main.py" || true + sleep 1 +fi + +# Update supervisor config with proper settings +cat > /etc/supervisor/conf.d/hosting-backend.conf << 'EOF' +[program:hosting-backend] +command=/opt/hosting-platform/backend/venv/bin/python /opt/hosting-platform/backend/app/main.py +directory=/opt/hosting-platform/backend +user=root +autostart=true +autorestart=true +startsecs=5 +stopwaitsecs=10 +stopsignal=TERM +killasgroup=true +stopasgroup=true +redirect_stderr=true +stdout_logfile=/var/log/hosting-backend.log +stdout_logfile_maxbytes=50MB +stdout_logfile_backups=5 +environment=PYTHONPATH="/opt/hosting-platform/backend",FLASK_APP="app.main",FLASK_ENV="production",SECRET_KEY="cfef4ad2f52832def87c20ebddb5067c44379c5ab366ebeb50217b5f484a92df",DATABASE_URL="postgresql://hosting:hosting_519c6c66a8e2695ce704ccba@localhost:5432/hosting",REDIS_URL="redis://localhost:6379/0",ENCRYPTION_KEY="tThpEL7KeYwGSg9isM7LUbxv-Lju325c2gtIf56DHV4",PLATFORM_CF_API_TOKEN="",PLATFORM_CF_ACCOUNT_ID="",LB_IPS="185.123.45.67,185.123.45.68,185.123.45.69",API_HOST="0.0.0.0",API_PORT="5000" +EOF + +# Reload supervisor +echo " 🔄 Reloading supervisor..." +supervisorctl reread +supervisorctl update +sleep 2 + +# Start backend +echo " ▶️ Starting backend..." +supervisorctl start hosting-backend || true +sleep 3 + +# Restart frontend +supervisorctl restart hosting-frontend || true ENDSSH sleep 3 echo "✅ Services restarted" @@ -87,8 +131,33 @@ echo "" # 6. Health Check echo "🏥 [6/6] Running health checks..." -sleep 2 +# Wait for backend to be ready +MAX_RETRIES=15 +RETRY_COUNT=0 +echo " ⏳ Waiting for backend to be ready..." + +while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do + HEALTH=$(ssh -i $SSH_KEY $HOST "curl -s http://localhost:5000/health" 2>/dev/null || echo "") + + if echo "$HEALTH" | grep -q "ok"; then + echo " ✅ Backend is healthy!" + break + fi + + RETRY_COUNT=$((RETRY_COUNT + 1)) + echo " ⏳ Attempt $RETRY_COUNT/$MAX_RETRIES..." + sleep 2 +done + +if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then + echo " ❌ Backend failed to start! Checking logs..." + ssh -i $SSH_KEY $HOST "tail -30 /var/log/hosting-backend.log" + exit 1 +fi + +# Check public endpoints +sleep 2 HEALTH=$(curl -s https://api.argeict.net/health) if echo "$HEALTH" | grep -q "ok"; then echo "✅ API Health: OK"