From 1bd3cba8d23712ec445d7b2374c2af6e04849e25 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Wed, 4 Nov 2026 14:17:30 -0600 Subject: [PATCH] Parallelize read-only status and reuse report probes --- docs/app-ops.md | 6 ++ docs/templates/app-ops.md.tmpl | 6 ++ lib/jeannie/commands.sh | 23 ++++++- scripts/report-ui | 94 ++++++++++++++++++++++++++- tests/jeannie/test_report_parallel.py | 38 +++++++++++ 5 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 tests/jeannie/test_report_parallel.py diff --git a/docs/app-ops.md b/docs/app-ops.md index 25718bd..3414b23 100644 --- a/docs/app-ops.md +++ b/docs/app-ops.md @@ -2,6 +2,12 @@ ## Current status and recent events +Compact `status` and `doctor-homelab` run independent read-only probes in +parallel (four at a time by default, capped at eight). Set +`LAB_REPORT_PARALLELISM=1` to serialize them or `LAB_REPORT_TIMING=true` to +print probe elapsed time. Identical selected HTTP/container probes reuse results +only within that report. Healing performs its own live identity checks. + Status examines Gitea and Traefik logs from the last five minutes by default; `LAB_STATUS_LOG_SINCE` overrides the interval. A few Gitea client disconnects are informational only when its container, local HTTP, and public HTTP checks diff --git a/docs/templates/app-ops.md.tmpl b/docs/templates/app-ops.md.tmpl index 057f9d2..c8f73f6 100644 --- a/docs/templates/app-ops.md.tmpl +++ b/docs/templates/app-ops.md.tmpl @@ -2,6 +2,12 @@ ## Current status and recent events +Compact `status` and `doctor-homelab` run independent read-only probes in +parallel (four at a time by default, capped at eight). Set +`LAB_REPORT_PARALLELISM=1` to serialize them or `LAB_REPORT_TIMING=true` to +print probe elapsed time. Identical selected HTTP/container probes reuse results +only within that report. Healing performs its own live identity checks. + Status examines Gitea and Traefik logs from the last five minutes by default; `LAB_STATUS_LOG_SINCE` overrides the interval. A few Gitea client disconnects are informational only when its container, local HTTP, and public HTTP checks diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index b0076dd..f5b4891 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -1684,9 +1684,9 @@ status_gitea_recent_errors() { all_matches="$(grep -Eai 'error|panic|fatal|authentication failed|denied| 50[0-9] ' <<<"${sample}" || true)" count="$(printf '%s\n' "${all_matches}" | wc -l | tr -d ' ')" if ((count <= 3)) && ! grep -Eavi 'broken pipe|connection reset by peer' <<<"${all_matches}" >/dev/null && \ - status_container_running "${container}" >/dev/null 2>&1 && \ - status_http_ok "http://127.0.0.1:${LAB_GITEA_HTTP_PORT:-3000}/" >/dev/null 2>&1 && \ - status_gitea_public_ok >/dev/null 2>&1; then + report_ui_cached_probe status_container_running "${container}" >/dev/null 2>&1 && \ + report_ui_cached_probe status_http_ok "http://127.0.0.1:${LAB_GITEA_HTTP_PORT:-3000}/" >/dev/null 2>&1 && \ + report_ui_cached_probe status_gitea_public_ok >/dev/null 2>&1; then printf 'Recent client disconnects (%s in %s); Gitea container and HTTP checks currently pass. No fix indicated; monitor for recurrence.\n%s\n' "${count}" "${since}" "${matches}" return 3 fi @@ -1696,6 +1696,14 @@ status_gitea_recent_errors() { } status_cascade_recent_gitea() { + if truthy "${JEANNIE_REPORT_MODE:-false}" && [[ "${REPORT_UI_PARALLEL:-false}" == true ]]; then + report_ui_enqueue status_cascade_recent_gitea_sync + else + status_cascade_recent_gitea_sync + fi +} + +status_cascade_recent_gitea_sync() { local output status=0 output="$(status_gitea_recent_errors 2>&1)" || status=$? if truthy "${JEANNIE_REPORT_MODE:-false}"; then @@ -2334,7 +2342,10 @@ status_report() { # shellcheck disable=SC2034 REPORT_UI_ONLY=problems report_ui_begin + local REPORT_UI_PARALLEL=true JEANNIE_REPORT_MODE=true status_cascade_report >/dev/null || cascade_status=$? + report_ui_finish + ((REPORT_UI_FAILURES == 0)) || cascade_status=1 report_ui_render "Jeannie Status" >"${status_json}" report_ui_cleanup if [[ "${heal_mode}" == "plan" ]]; then @@ -2348,7 +2359,10 @@ status_report() { if [[ "${REPORT_UI_VERBOSE}" != "true" ]]; then report_ui_begin + local REPORT_UI_PARALLEL=true JEANNIE_REPORT_MODE=true status_cascade_report || cascade_status=$? + report_ui_finish + ((REPORT_UI_FAILURES == 0)) || cascade_status=1 report_ui_render "Jeannie Status" report_ui_cleanup return "${cascade_status}" @@ -2979,6 +2993,7 @@ doctor_homelab() { if [[ "${REPORT_UI_VERBOSE}" != "true" ]]; then report_ui_begin + local REPORT_UI_PARALLEL=true report_ui_check fail "Architecture Boundary" "Selected cluster identity" "./jeannie cluster-context check" "" "" cluster_context_require || failures=$((failures + 1)) report_ui_check fail "Architecture Boundary" "External services host resolved" "./jeannie cluster-profile" "" "" status_external_services_host_declared || failures=$((failures + 1)) @@ -3037,6 +3052,8 @@ doctor_homelab() { report_ui_check warn "Operations" "Pod restart pressure" "kubectl get pods -A" "" "" status_pod_restart_pressure report_ui_check warn "Operations" "Traefik 5xx/404 signals" "kubectl -n traefik logs -l app.kubernetes.io/name=traefik --since=5m" "" "" status_traefik_error_rates + report_ui_finish + failures=$((failures + REPORT_UI_FAILURES)) report_ui_render "Post-Migration Homelab Doctor" report_ui_cleanup return "${failures}" diff --git a/scripts/report-ui b/scripts/report-ui index 62b30cf..7abe5b2 100644 --- a/scripts/report-ui +++ b/scripts/report-ui @@ -7,6 +7,8 @@ REPORT_UI_LOCAL_RCA="${REPORT_UI_LOCAL_RCA:-true}" # shellcheck disable=SC2034 REPORT_UI_VERBOSE=false REPORT_UI_ROWS_FILE="" +REPORT_UI_WORK_DIR="" +REPORT_UI_FAILURES=0 report_ui_usage_flags() { cat <<'EOF' @@ -61,13 +63,91 @@ report_ui_parse_args() { report_ui_begin() { REPORT_UI_ROWS_FILE="$(mktemp "${TMPDIR:-/tmp}/jeannie-report-rows.XXXXXX")" + REPORT_UI_WORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/jeannie-report-work.XXXXXX")" + REPORT_UI_FAILURES=0 + REPORT_UI_JOB_COUNT=0 + REPORT_UI_JOB_WAITED=0 + REPORT_UI_JOB_PIDS=() + REPORT_UI_STARTED_SECONDS=${SECONDS} + export REPORT_UI_WORK_DIR } report_ui_cleanup() { if [[ -n "${REPORT_UI_ROWS_FILE:-}" && -f "${REPORT_UI_ROWS_FILE}" ]]; then rm -f "${REPORT_UI_ROWS_FILE}" fi + if [[ -n "${REPORT_UI_WORK_DIR:-}" && -d "${REPORT_UI_WORK_DIR}" ]]; then + rm -rf "${REPORT_UI_WORK_DIR}" + fi REPORT_UI_ROWS_FILE="" + REPORT_UI_WORK_DIR="" +} + +# Reuse only explicitly read-only probes, within this report. The lock avoids +# duplicate network checks when independent jobs request the same probe. +report_ui_cached_probe() { + if [[ -z "${REPORT_UI_WORK_DIR:-}" ]] || ! command -v flock >/dev/null 2>&1; then + "$@" + return $? + fi + local key lock_fd rc + key="$(printf '%s\0' "$@" | sha256sum | awk '{print $1}')" + exec {lock_fd}>"${REPORT_UI_WORK_DIR}/${key}.lock" + flock -x "${lock_fd}" + if [[ ! -f "${REPORT_UI_WORK_DIR}/${key}.status" ]]; then + rc=0 + "$@" >"${REPORT_UI_WORK_DIR}/${key}.output" 2>&1 || rc=$? + printf '%s\n' "${rc}" >"${REPORT_UI_WORK_DIR}/${key}.status" + fi + cat "${REPORT_UI_WORK_DIR}/${key}.output" + rc="$(cat "${REPORT_UI_WORK_DIR}/${key}.status")" + flock -u "${lock_fd}" + exec {lock_fd}>&- + return "${rc}" +} + +report_ui_enqueue() { + local limit="${LAB_REPORT_PARALLELISM:-4}" slot + [[ "${limit}" =~ ^[1-9][0-9]*$ ]] || limit=4 + ((limit <= 8)) || limit=8 + while ((REPORT_UI_JOB_COUNT - REPORT_UI_JOB_WAITED >= limit)); do + wait "${REPORT_UI_JOB_PIDS[REPORT_UI_JOB_WAITED]}" || true + REPORT_UI_JOB_WAITED=$((REPORT_UI_JOB_WAITED + 1)) + done + slot="$(printf '%06d' "${REPORT_UI_JOB_COUNT}")" + ( + REPORT_UI_ROWS_FILE="${REPORT_UI_WORK_DIR}/row-${slot}" + : >"${REPORT_UI_ROWS_FILE}" + rc=0 + "$@" || rc=$? + printf '%s\n' "${rc}" >"${REPORT_UI_WORK_DIR}/result-${slot}" + ) & + REPORT_UI_JOB_PIDS+=("$!") + REPORT_UI_JOB_COUNT=$((REPORT_UI_JOB_COUNT + 1)) +} + +report_ui_finish() { + local index slot rc + for ((index=REPORT_UI_JOB_WAITED; index>"${REPORT_UI_ROWS_FILE}" + rc="$(cat "${REPORT_UI_WORK_DIR}/result-${slot}")" + ((rc == 0)) || REPORT_UI_FAILURES=$((REPORT_UI_FAILURES + 1)) + done + REPORT_UI_JOB_COUNT=0 + REPORT_UI_JOB_WAITED=0 + REPORT_UI_JOB_PIDS=() + if [[ "${LAB_REPORT_TIMING:-false}" == true ]]; then + printf 'Read-only report probes completed in %ss.\n' "$((SECONDS - REPORT_UI_STARTED_SECONDS))" >&2 + fi } report_ui_one_line() { @@ -113,6 +193,14 @@ report_ui_emit() { } report_ui_check() { + if [[ "${REPORT_UI_PARALLEL:-false}" == true ]]; then + report_ui_enqueue report_ui_check_sync "$@" + else + report_ui_check_sync "$@" + fi +} + +report_ui_check_sync() { local severity="$1" local area="$2" local check="$3" @@ -126,7 +214,11 @@ report_ui_check() { local detail set +e - output="$("$@" 2>&1)" + case "${1:-}" in + status_container_running|status_http_ok|status_gitea_public_ok) + output="$(report_ui_cached_probe "$@" 2>&1)" ;; + *) output="$("$@" 2>&1)" ;; + esac status=$? set -e diff --git a/tests/jeannie/test_report_parallel.py b/tests/jeannie/test_report_parallel.py new file mode 100644 index 0000000..92f042c --- /dev/null +++ b/tests/jeannie/test_report_parallel.py @@ -0,0 +1,38 @@ +"""Read-only report orchestration regression; run on the Debian host.""" + +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] + + +class ParallelReportTests(unittest.TestCase): + def test_order_cache_and_failure_are_preserved(self): + with tempfile.TemporaryDirectory() as directory: + env = dict(os.environ, PROBE_COUNTER=str(Path(directory) / "calls"), LAB_REPORT_PARALLELISM="2") + program = r''' +set -euo pipefail +source scripts/report-ui +status_http_ok() { printf 'call\n' >>"$PROBE_COUNTER"; return 0; } +failed_probe() { echo 'unreachable'; return 1; } +report_ui_begin +REPORT_UI_PARALLEL=true +report_ui_check fail A first fix '' '' status_http_ok same +report_ui_check fail A second fix '' '' status_http_ok same +report_ui_check fail A third fix '' '' failed_probe +report_ui_finish +cat "$REPORT_UI_ROWS_FILE" +printf 'failures=%s\n' "$REPORT_UI_FAILURES" +report_ui_cleanup +''' + result = subprocess.run(["bash", "-c", program], cwd=ROOT, env=env, + text=True, capture_output=True, check=True) + lines = result.stdout.splitlines() + self.assertEqual([line.split("\t")[2] for line in lines[:3]], ["first", "second", "third"]) + self.assertEqual([line.split("\t")[0] for line in lines[:3]], ["ok", "ok", "fail"]) + self.assertIn("failures=1", lines) + self.assertEqual(Path(env["PROBE_COUNTER"]).read_text().splitlines(), ["call"])