AvanzaCast/scripts/restart_backend_prod.sh

84 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# restart_backend_prod.sh
# Usage:
# Run this on the production host where the repository is deployed.
# ./restart_backend_prod.sh [mode]
# Modes:
# docker - use docker compose to recreate backend-api (default)
# node - restart node process started with npx tsx
# systemd - restart systemd service named backend-api
#
MODE=${1:-docker}
COMPOSE_FILE=${COMPOSE_FILE:-/home/xesar/Documentos/Nextream/AvanzaCast/docker-compose.prod.yml}
REPO_DIR=${REPO_DIR:-/home/xesar/Documentos/Nextream/AvanzaCast}
echo "--> Restart backend-api (mode=$MODE)"
if [ "$MODE" = "docker" ]; then
echo "Using docker-compose file: $COMPOSE_FILE"
if ! command -v docker >/dev/null 2>&1; then
echo "Docker not found on PATH; cannot continue in docker mode" >&2
exit 2
fi
cd "$(dirname "$COMPOSE_FILE")" || cd "$REPO_DIR" || true
echo "Recreating backend-api service (no deps, force recreate)"
docker compose -f "$COMPOSE_FILE" up -d --no-deps --force-recreate backend-api
echo "Waiting 3s for startup..."
sleep 3
echo "--- docker compose ps backend-api ---"
docker compose -f "$COMPOSE_FILE" ps backend-api || true
echo "--- docker ps (filter backend-api) ---"
docker ps --filter name=backend-api || true
echo "--- backend-api logs (last 200 lines) ---"
docker compose -f "$COMPOSE_FILE" logs --tail=200 backend-api || true
elif [ "$MODE" = "node" ]; then
echo "Restarting node process (npx tsx)..."
# This assumes the process was started with a matching pattern
pkill -f 'npx tsx src/index.ts' || true
sleep 1
cd "$REPO_DIR/packages/backend-api" || true
# Start in background (modify as needed for your env)
nohup npx tsx src/index.ts > /tmp/backend_api_run.log 2>&1 &
echo $! > /tmp/backend_api_pid.txt
sleep 2
tail -n 200 /tmp/backend_api_run.log || true
elif [ "$MODE" = "systemd" ]; then
echo "Restarting systemd service: backend-api"
sudo systemctl restart backend-api.service
sudo journalctl -u backend-api.service -n 200 --no-pager || true
else
echo "Unknown mode: $MODE" >&2
exit 3
fi
# Basic health checks (local)
echo "\n--- Local health check: http://localhost:4000/health ---"
if command -v curl >/dev/null 2>&1; then
curl -sS http://localhost:4000/health || echo "Local health check failed or endpoint not reachable"
else
echo "curl not available to run health check"
fi
echo "\n--- CORS quick check (OPTIONS against production token-server) ---"
PROD_HOST=${PROD_HOST:-https://avanzacast-servertokens.bfzqqk.easypanel.host}
if command -v curl >/dev/null 2>&1; then
curl -i -X OPTIONS "$PROD_HOST/api/session" \
-H 'Origin: https://avanzacast-broadcastpanel.bfzqqk.easypanel.host' \
-H 'Access-Control-Request-Method: POST' | sed -n '1,200p'
else
echo "curl not available to run CORS check"
fi
cat <<'EOF'
Done. If you ran this on the production host, please verify:
- The /health endpoint responds 200
- The OPTIONS call returns Access-Control-Allow-Origin header for the broadcast domain
- A test POST to /api/session returns JSON with redirectUrl
To revert a temporary ALLOW_ALL_CORS=1 deployment, restart the service without that env var, and ensure FRONTEND_URLS is set in the production environment.
EOF