From c76bcc36648a2cc76d91a92c4bb06f1276cd235d Mon Sep 17 00:00:00 2001 From: Abhishek Date: Thu, 26 Feb 2026 16:39:35 +0000 Subject: [PATCH] script fixes --- nginx/dograh_upstream.conf.template | 2 +- scripts/rolling_update.sh | 14 +++++++------- scripts/start_services.sh | 10 +++++----- scripts/stop_services.sh | 18 +++++++++--------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/nginx/dograh_upstream.conf.template b/nginx/dograh_upstream.conf.template index e8f2307..8177be9 100644 --- a/nginx/dograh_upstream.conf.template +++ b/nginx/dograh_upstream.conf.template @@ -3,5 +3,5 @@ upstream dograh_backend { least_conn; - {{UVICORN_UPSTREAM_SERVERS}} +{{UVICORN_UPSTREAM_SERVERS}} } diff --git a/scripts/rolling_update.sh b/scripts/rolling_update.sh index e6c5e75..b98bb77 100755 --- a/scripts/rolling_update.sh +++ b/scripts/rolling_update.sh @@ -154,7 +154,7 @@ for ((w = 0; w < FASTAPI_WORKERS; w++)); do if [[ -f "$pidfile" ]]; then pid=$(<"$pidfile") if kill -0 "$pid" 2>/dev/null; then - ((old_running++)) + old_running=$((old_running + 1)) fi fi done @@ -168,7 +168,7 @@ log_info "Found $old_running running old worker(s)" # Verify new ports are free for ((w = 0; w < FASTAPI_WORKERS; w++)); do port=$((NEW_BASE + w)) - if lsof -Pi :"$port" -sTCP:LISTEN -t >/dev/null 2>&1; then + if ss -tln "sport = :$port" | grep -q LISTEN; then log_error "Port $port is already in use. Cannot start new band." exit 1 fi @@ -312,14 +312,14 @@ done # Generate upstream config sed -e "s|{{UVICORN_UPSTREAM_SERVERS}}|${UPSTREAM_SERVERS}|" \ - "$NGINX_UPSTREAM_TEMPLATE" > "$NGINX_UPSTREAM_CONF" + "$NGINX_UPSTREAM_TEMPLATE" | sudo tee "$NGINX_UPSTREAM_CONF" > /dev/null log_info "Generated nginx upstream config with $FASTAPI_WORKERS workers (ports ${NEW_BASE}–$((NEW_BASE + FASTAPI_WORKERS - 1)))" # Validate config -if ! nginx -t 2>/dev/null; then +if ! sudo nginx -t 2>/dev/null; then log_error "nginx config validation failed!" - nginx -t 2>&1 || true + sudo nginx -t 2>&1 || true # Restore old upstream config OLD_UPSTREAM="" for ((w = 0; w < FASTAPI_WORKERS; w++)); do @@ -327,14 +327,14 @@ if ! nginx -t 2>/dev/null; then OLD_UPSTREAM="${OLD_UPSTREAM} server 127.0.0.1:${port};\n" done sed -e "s|{{UVICORN_UPSTREAM_SERVERS}}|${OLD_UPSTREAM}|" \ - "$NGINX_UPSTREAM_TEMPLATE" > "$NGINX_UPSTREAM_CONF" + "$NGINX_UPSTREAM_TEMPLATE" | sudo tee "$NGINX_UPSTREAM_CONF" > /dev/null rollback_new_workers "$NEW_BAND" exit 1 fi # Reload nginx (graceful — finishes in-flight requests to old upstream) -systemctl reload nginx +sudo systemctl reload nginx log_info "nginx reloaded — traffic now routed to band $NEW_BAND" ############################################################################### diff --git a/scripts/start_services.sh b/scripts/start_services.sh index a9fb332..f4c1470 100755 --- a/scripts/start_services.sh +++ b/scripts/start_services.sh @@ -47,7 +47,7 @@ if [[ -d "$RUN_DIR" ]]; then [[ -e "$pidfile" ]] || continue pid=$(<"$pidfile") if kill -0 "$pid" 2>/dev/null; then - ((live_count++)) + live_count=$((live_count + 1)) fi done @@ -171,17 +171,17 @@ if [[ -f "$NGINX_UPSTREAM_TEMPLATE" ]]; then # Generate upstream config from template sed -e "s|{{UVICORN_UPSTREAM_SERVERS}}|${UPSTREAM_SERVERS}|" \ - "$NGINX_UPSTREAM_TEMPLATE" > "$NGINX_UPSTREAM_CONF" + "$NGINX_UPSTREAM_TEMPLATE" | sudo tee "$NGINX_UPSTREAM_CONF" > /dev/null echo "Generated nginx upstream config with $FASTAPI_WORKERS workers (ports ${UVICORN_BASE_PORT}-$((UVICORN_BASE_PORT + FASTAPI_WORKERS - 1)))" # Test and reload nginx - if nginx -t 2>/dev/null; then - systemctl reload nginx + if sudo nginx -t 2>/dev/null; then + sudo systemctl reload nginx echo "Nginx reloaded successfully" else echo "ERROR: nginx config test failed, not reloading" - nginx -t + sudo nginx -t exit 1 fi fi diff --git a/scripts/stop_services.sh b/scripts/stop_services.sh index 4a42138..504ecbd 100755 --- a/scripts/stop_services.sh +++ b/scripts/stop_services.sh @@ -42,17 +42,17 @@ kill_process_tree() { descendants=$(get_descendants "$pid") - # Kill children first (bottom-up), then parent + # Kill the parent first so supervisors don't respawn children + if kill -0 "$pid" 2>/dev/null; then + kill "$signal" "$pid" 2>/dev/null || true + fi + + # Then kill any remaining descendants for desc_pid in $descendants; do if kill -0 "$desc_pid" 2>/dev/null; then kill "$signal" "$desc_pid" 2>/dev/null || true fi done - - # Kill the parent - if kill -0 "$pid" 2>/dev/null; then - kill "$signal" "$pid" 2>/dev/null || true - fi } ############################################################################### @@ -113,14 +113,14 @@ for pidfile in "${pid_files[@]}"; do # Final check if kill -0 "$oldpid" 2>/dev/null; then echo " Error: Failed to stop $name (PID $oldpid)" - ((failed_count++)) + failed_count=$((failed_count + 1)) else echo " Stopped $name (forced)" - ((stopped_count++)) + stopped_count=$((stopped_count + 1)) fi else echo " Stopped $name" - ((stopped_count++)) + stopped_count=$((stopped_count + 1)) fi else echo "Service $name (PID $oldpid) is not running"