diff --git a/scripts/cli/upgrade b/scripts/cli/upgrade new file mode 100755 index 0000000..4c15aea --- /dev/null +++ b/scripts/cli/upgrade @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# +# Upgrade auto-commit-service +# +# Gracefully stops the running service and restarts with latest code. +# +# Usage: +# ./scripts/cli/upgrade +# + +set -euo pipefail + +SERVICE_URL="${AUTO_COMMIT_URL:-http://localhost:8200}" +SERVICE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } +log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } +log_error() { echo -e "${RED}[ERROR]${NC} $1"; } + +# Step 1: Disable daemon (graceful stop) +log_info "Disabling daemon..." +if curl -s -X POST "${SERVICE_URL}/disable" > /dev/null 2>&1; then + log_info "Daemon disabled, waiting for current cycle to complete..." + sleep 2 +else + log_warn "Service not responding (may not be running)" +fi + +# Step 2: Kill existing process +log_info "Stopping existing process..." +pkill -f "auto_commit_service" 2>/dev/null || true +sleep 2 + +# Step 3: Verify stopped +if curl -s "${SERVICE_URL}/health" > /dev/null 2>&1; then + log_error "Service still running, force killing..." + pkill -9 -f "auto_commit_service" 2>/dev/null || true + sleep 2 +fi + +# Step 4: Start new process +log_info "Starting service from ${SERVICE_DIR}..." +cd "${SERVICE_DIR}" +nohup python -m auto_commit_service > /tmp/auto-commit.log 2>&1 & + +# Step 5: Wait for startup +log_info "Waiting for service to start..." +for i in {1..10}; do + sleep 1 + if curl -s "${SERVICE_URL}/health" > /dev/null 2>&1; then + break + fi + echo -n "." +done +echo "" + +# Step 6: Verify health +if curl -s "${SERVICE_URL}/health" | grep -q '"status":"ok"'; then + log_info "Service upgraded successfully" + curl -s "${SERVICE_URL}/health" | python3 -m json.tool 2>/dev/null || cat +else + log_error "Service failed to start" + log_error "Check logs: tail -f /tmp/auto-commit.log" + exit 1 +fi