Parallelize read-only status and reuse report probes
This commit is contained in:
parent
12adcc5abc
commit
1bd3cba8d2
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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}"
|
||||
|
|
|
|||
|
|
@ -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_JOB_COUNT; index++)); do
|
||||
wait "${REPORT_UI_JOB_PIDS[index]}" || true
|
||||
done
|
||||
for ((index=0; index<REPORT_UI_JOB_COUNT; index++)); do
|
||||
slot="$(printf '%06d' "${index}")"
|
||||
if [[ ! -f "${REPORT_UI_WORK_DIR}/result-${slot}" ]]; then
|
||||
report_ui_emit fail "Diagnostics" "Probe execution" "A report probe did not complete" "slot=${slot}"
|
||||
REPORT_UI_FAILURES=$((REPORT_UI_FAILURES + 1))
|
||||
continue
|
||||
fi
|
||||
cat "${REPORT_UI_WORK_DIR}/row-${slot}" >>"${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
|
||||
|
||||
|
|
|
|||
|
|
@ -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"])
|
||||
Loading…
Reference in New Issue