diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fea983..a269a87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ Each package and app has its own section and its own tags (`contracts@1.0.0`, `accounts@1.0.0`, …). +## crosscode script - 2026-11-19 (no tag: servers run `main`) + +- **Scripts: a failed install says why its apps didn't start.** Before removing what a failed + install made, `crosscode install` / `crosscode add` print each PM2 app's last 30 log lines. The + logs used to go with the apps, so the Hub only showed "/health didn't report 2.3.1 within 90s + (last answer: none)" and the reason was lost. + ## agent@1.1.1 - 2026-11-18 - **Stats keep going while a job runs.** The agent sent stats and polled in one loop, so during diff --git a/scripts/lib/install.sh b/scripts/lib/install.sh index fa1bb16..517a73f 100644 --- a/scripts/lib/install.sh +++ b/scripts/lib/install.sh @@ -58,12 +58,26 @@ random_secret() { openssl rand -base64 64 | tr -d '/+=\n' | head -c "${1:-48}"; INSTALL_UNDO="" undo_add() { [ -z "$INSTALL_UNDO" ] || printf '%s\n' "$*" >> "$INSTALL_UNDO"; } +# pm2_last_logs : each PM2 app's last 30 log lines (output and errors), indented. +pm2_last_logs() { + local user="$1" name + shift + for name in "$@"; do + info "PM2: last log lines of $name" + run_as "$user" / pm2 logs "$name" --lines 30 --nostream --raw &1 | tail -n 60 | sed 's/^/ /' || true + done +} + install_undo() { local kind a b step "Removing what this install made" tac "$INSTALL_UNDO" | while read -r kind a b; do case "$kind" in - pm2) # shellcheck disable=SC2086 # b is a space-separated list of PM2 names + pm2) + # Their logs go with them, so show why they didn't start first (the Hub gets the log). + # shellcheck disable=SC2086 # b is a space-separated list of PM2 names + pm2_last_logs "$a" $b + # shellcheck disable=SC2086 run_as "$a" / pm2 delete $b >/dev/null 2>&1 || true run_as "$a" / pm2 save --force >/dev/null 2>&1 || true info "PM2: $b removed" ;; diff --git a/scripts/tests/common.bats b/scripts/tests/common.bats index 39779cb..fb742f6 100644 --- a/scripts/tests/common.bats +++ b/scripts/tests/common.bats @@ -259,3 +259,20 @@ EOF [ "$(host_from_url "$(env_value "$TMP/.env" FRONTEND_URL)")" = events.hopefamily.co.za ] [ "$(host_from_url http://localhost:3000/x)" = localhost ] } + +# ── install undo ──────────────────────────────────────────────────────────────────────────── + +@test "a failed install shows each PM2 app's last log lines before removing it" { + # shellcheck source=../lib/install.sh + source "$BATS_TEST_DIRNAME/../lib/install.sh" + run_as() { shift 2; case "$2" in logs) printf 'Error: %s crashed\n' "$3" ;; esac; echo "$*" >> "$TMP/pm2"; } + INSTALL_UNDO="$TMP/undo" + undo_add pm2 cc-hope "hope-events-backend hope-events-frontend" + run install_undo + [ "$status" -eq 0 ] + [[ "$output" == *"last log lines of hope-events-backend"* ]] + [[ "$output" == *" Error: hope-events-backend crashed"* ]] + [[ "$output" == *" Error: hope-events-frontend crashed"* ]] + # The logs are read before the apps are deleted. + [ "$(grep -n 'logs hope-events-backend' "$TMP/pm2" | cut -d: -f1)" -lt "$(grep -n '^pm2 delete' "$TMP/pm2" | cut -d: -f1)" ] +}