Remove axiom and fix scripts

This commit is contained in:
Abhishek Kumar 2025-09-26 10:57:51 +05:30
parent 443490b2dd
commit 39dade1755
4 changed files with 43 additions and 368 deletions

View file

@ -1,18 +1,22 @@
#!/usr/bin/env bash
# restart_services.sh — safer, simplified
# start_services.sh
set -euo pipefail
set -e # Exit on error
### CONFIGURATION #############################################################
ENV_FILE="api/.env"
RUN_DIR="run" # where we keep *.pid
LOG_ROOT="logs"
VENV_PATH="/home/ubuntu/dograh_venv"
ARQ_WORKERS=${ARQ_WORKERS:-1}
### 1) Load environment vars so that configurations like FASTAPI_WORKERS are loaded #
# Log startup
echo "Starting Dograh Services at $(date)"
### 1) Load environment vars so that configurations like FASTAPI_WORKERS are loaded
set -a && . "$ENV_FILE" && set +a
# Get ENVIRONMENT for nginx config selection
ENVIRONMENT="${ENVIRONMENT:-staging}"
cd /home/ubuntu/app
if [[ -z "${FASTAPI_PORT:-}" ]]; then
echo "Error: FASTAPI_PORT environment variable is not set."
@ -24,14 +28,6 @@ if [[ -z "${FASTAPI_WORKERS:-}" ]]; then
exit 1
fi
if [[ -z "${CONDA_ENV_NAME:-}" ]]; then
echo "Error: CONDA_ENV_NAME environment variable is not set."
exit 1
fi
# Default ARQ_WORKERS to 1 if not set
ARQ_WORKERS=${ARQ_WORKERS:-1}
# map "service name" → "command to run"
declare -A SERVICES=(
[ari_manager]="python -m api.services.telephony.ari_manager"
@ -44,13 +40,8 @@ for ((i=1; i<=ARQ_WORKERS; i++)); do
SERVICES[arq$i]="python -m arq api.tasks.arq.WorkerSettings --custom-log-dict api.tasks.arq.LOG_CONFIG"
done
### 2) Activate conda #########################################################
# Source conda if not already available (needed when running from systemd)
if ! command -v conda &>/dev/null; then
source /opt/conda/etc/profile.d/conda.sh
fi
eval "$(conda shell.bash hook)"
conda activate "$CONDA_ENV_NAME"
### 2) Activate virtual environment #########################################
source ${VENV_PATH}/bin/activate
### 3) Stop old services (only via PID files) #################################
mkdir -p "$RUN_DIR"
@ -58,16 +49,16 @@ for name in "${!SERVICES[@]}"; do
pidfile="$RUN_DIR/$name.pid"
if [[ -f $pidfile ]]; then
oldpid=$(<"$pidfile")
if kill -0 "$oldpid" 2>/dev/null; then
if kill -0 "$oldpid"; then
echo "Stopping $name (PID $oldpid and its process group)…"
# Kill the entire process group (negative PID)
# First try SIGTERM
kill -TERM -"$oldpid" 2>/dev/null || kill -TERM "$oldpid" 2>/dev/null || true
kill -TERM -"$oldpid" || kill -TERM "$oldpid" || true
sleep 4
# If still running, use SIGKILL
if kill -0 "$oldpid" 2>/dev/null; then
if kill -0 "$oldpid"; then
echo "⚠️ $name did not exit cleanly, forcing stop..."
kill -KILL -"$oldpid" 2>/dev/null || kill -KILL "$oldpid" 2>/dev/null || true
kill -KILL -"$oldpid" || kill -KILL "$oldpid" || true
sleep 1
fi
fi
@ -80,32 +71,17 @@ done
# Clean up any port tracking files for uvicorn
rm -f "$RUN_DIR/uvicorn.port" "$RUN_DIR/uvicorn_new.port" "$RUN_DIR/uvicorn_old.pid"
### 4) Run migrations #########################################################
alembic -c api/alembic.ini upgrade head
### 5) Prepare logs ###########################################################
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
LOG_DIR="$LOG_ROOT/$timestamp"
LOG_DIR="$LOG_ROOT/latest"
mkdir -p "$LOG_DIR"
# Create relative symlink
cd "$LOG_ROOT" && ln -sfn "$timestamp" latest && cd - >/dev/null
### 6) (Optional) Free FastAPI port ###########################################
FASTAPI_PORT=$FASTAPI_PORT
if command -v lsof &>/dev/null; then
lsof -ti tcp:"$FASTAPI_PORT" | xargs -r kill -9 || true
fi
### 7) Start services #########################################################
# Export rotation settings for loguru (if using file logging)
export LOG_ROTATION_SIZE="${LOG_ROTATION_SIZE:-100 MB}"
export LOG_RETENTION="${LOG_RETENTION:-7 days}"
export LOG_COMPRESSION="${LOG_COMPRESSION:-gz}"
for name in "${!SERVICES[@]}"; do
cmd=${SERVICES[$name]}
echo "→ Starting $name with loguru rotation…"
echo "→ Starting $name"
# Export LOG_FILE_PATH for this specific service
export LOG_FILE_PATH="$LOG_DIR/$name.log"
@ -118,55 +94,9 @@ for name in "${!SERVICES[@]}"; do
pid=$!
echo $pid >"$RUN_DIR/$name.pid"
# For uvicorn, also save the port for rolling updates and update nginx
# For uvicorn, also save the port for rolling updates
if [[ "$name" == "uvicorn" ]]; then
echo "$FASTAPI_PORT" >"$RUN_DIR/uvicorn.port"
# Update nginx upstream configuration if nginx is installed
if command -v nginx &>/dev/null && [[ -d /etc/nginx ]]; then
# Determine which upstream config to update based on ENVIRONMENT
if [[ "${ENVIRONMENT:-}" == "production" ]]; then
NGINX_UPSTREAM_CONF="/etc/nginx/conf.d/dograh_production_upstream.conf"
UPSTREAM_NAME="dograh_production_backend"
echo "→ Updating PRODUCTION nginx upstream to port $FASTAPI_PORT"
else
# Default to staging for any non-production environment
NGINX_UPSTREAM_CONF="/etc/nginx/conf.d/dograh_staging_upstream.conf"
UPSTREAM_NAME="dograh_staging_backend"
echo "→ Updating STAGING nginx upstream to port $FASTAPI_PORT"
fi
if [[ -w $(dirname "$NGINX_UPSTREAM_CONF") ]] || [[ $EUID -eq 0 ]]; then
cat > "${NGINX_UPSTREAM_CONF}.tmp" <<EOF
# Auto-generated by start_services.sh for ${ENVIRONMENT:-staging}
# Last updated: $(date)
upstream ${UPSTREAM_NAME} {
server 127.0.0.1:${FASTAPI_PORT} max_fails=3 fail_timeout=30s;
}
EOF
# Atomic move (may need sudo)
if [[ $EUID -eq 0 ]]; then
mv "${NGINX_UPSTREAM_CONF}.tmp" "${NGINX_UPSTREAM_CONF}"
else
sudo mv "${NGINX_UPSTREAM_CONF}.tmp" "${NGINX_UPSTREAM_CONF}" 2>/dev/null || \
echo "⚠️ Could not update nginx config (need sudo). Run: sudo $0"
fi
# Test and reload nginx if config was updated
if [[ -f "$NGINX_UPSTREAM_CONF" ]]; then
if nginx -t 2>/dev/null || sudo nginx -t 2>/dev/null; then
echo "→ Reloading nginx…"
nginx -s reload 2>/dev/null || sudo nginx -s reload 2>/dev/null || \
echo "⚠️ Could not reload nginx (may need sudo)"
else
echo "⚠️ Nginx configuration test failed"
fi
fi
else
echo "⚠️ Cannot write to nginx config directory (need sudo privileges)"
echo " Run: sudo $0 to update nginx configuration"
fi
fi
fi
done
disown -a