fix local run startup and stale dispatch

This commit is contained in:
npc0-hue
2026-07-23 23:56:10 +08:00
parent 215f6e68c5
commit 2b1f553df0
11 changed files with 155 additions and 54 deletions
+64 -14
View File
@@ -19,6 +19,23 @@ managed_pid_running() {
[[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null
}
pid_is_descendant_of() {
local pid="$1"
local ancestor="$2"
local parent
while [[ "$pid" =~ ^[0-9]+$ && "$pid" -gt 1 ]]; do
if [[ "$pid" == "$ancestor" ]]; then
return 0
fi
parent="$(ps -o ppid= -p "$pid" 2>/dev/null | tr -d '[:space:]')"
[[ -n "$parent" && "$parent" != "$pid" ]] || return 1
pid="$parent"
done
return 1
}
port_listener_pid() {
local port="$1"
lsof -tiTCP:"$port" -sTCP:LISTEN 2>/dev/null | head -n 1 || true
@@ -51,8 +68,12 @@ ensure_port_available() {
if [[ -z "$listener_pid" ]]; then
return 0
fi
if managed_pid_running "$pid_file" && [[ "$listener_pid" == "$(cat "$pid_file")" ]]; then
return 0
if managed_pid_running "$pid_file"; then
local managed_pid
managed_pid="$(cat "$pid_file")"
if pid_is_descendant_of "$listener_pid" "$managed_pid"; then
return 0
fi
fi
if listener_owned_by_local_debug "$listener_pid"; then
printf 'stopping stale %s listener pid %s on port %s\n' "$name" "$listener_pid" "$port"
@@ -117,20 +138,49 @@ wait_for_run_registration() {
local run_pid_file="$LOCAL_DEBUG_PID_DIR/run.pid"
printf 'waiting for run endpoint %s registration\n' "$RUN_ENDPOINT_ID"
for _ in $(seq 1 "$attempts"); do
if [[ -f "$PLATFORM_METADATA_PATH" ]] && grep -Fq "\"ID\": \"$RUN_ENDPOINT_ID\"" "$PLATFORM_METADATA_PATH"; then
printf 'run endpoint %s is registered\n' "$RUN_ENDPOINT_ID"
return 0
fi
if ! managed_pid_running "$run_pid_file"; then
printf 'run exited before endpoint registration; see %s\n' "$LOCAL_DEBUG_LOG_DIR/run.log" >&2
return 1
fi
if run_endpoint_has_recent_heartbeat; then
printf 'run endpoint %s is registered\n' "$RUN_ENDPOINT_ID"
return 0
fi
sleep 1
done
printf 'run endpoint %s did not register before timeout\n' "$RUN_ENDPOINT_ID" >&2
return 1
}
run_endpoint_has_recent_heartbeat() {
[[ -f "$PLATFORM_METADATA_PATH" ]] || return 1
node -e '
const fs = require("fs");
const metadata = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
const endpointID = process.argv[2];
function findEndpoint(value) {
if (!value || typeof value !== "object") return null;
if ((value.ID || value.id) === endpointID && (value.LastHeartbeatAt || value.lastHeartbeatAt)) return value;
if (Array.isArray(value)) {
for (const item of value) {
const found = findEndpoint(item);
if (found) return found;
}
return null;
}
for (const item of Object.values(value)) {
const found = findEndpoint(item);
if (found) return found;
}
return null;
}
const endpoint = findEndpoint(metadata);
const heartbeat = endpoint && (endpoint.LastHeartbeatAt || endpoint.lastHeartbeatAt);
const ageMilliseconds = heartbeat ? Date.now() - Date.parse(heartbeat) : Number.POSITIVE_INFINITY;
process.exit(Number.isFinite(ageMilliseconds) && ageMilliseconds >= 0 && ageMilliseconds < 30000 ? 0 : 1);
' "$PLATFORM_METADATA_PATH" "$RUN_ENDPOINT_ID"
}
printf 'local debug root: %s\n' "$LOCAL_DEBUG_ROOT"
printf 'platform: %s\n' "$(local_debug_platform_url)"
printf 'platform_web: %s\n' "$(local_debug_web_url)"
@@ -160,6 +210,14 @@ start_service platform "$ROOT_DIR/platform" env \
wait_for_url platform "$(local_debug_platform_url)/healthz"
start_service platform_web "$ROOT_DIR" env \
PLATFORM_API_PROXY="$PLATFORM_API_PROXY" \
VITE_PLATFORM_API_BASE_URL="$VITE_PLATFORM_API_BASE_URL" \
VITE_ENABLE_LOCAL_AUTH_FALLBACK="$VITE_ENABLE_LOCAL_AUTH_FALLBACK" \
npm --prefix platform_web run dev -- --host "0.0.0.0" --port "$LOCAL_DEBUG_WEB_PORT"
wait_for_url platform_web "$(local_debug_web_url)"
local_debug_build_bootstrap_run
start_service run "$(dirname "$RUN_BOOTSTRAP_BIN")" env \
GOCACHE="$GOCACHE" \
@@ -180,14 +238,6 @@ start_service run "$(dirname "$RUN_BOOTSTRAP_BIN")" env \
wait_for_run_registration
start_service platform_web "$ROOT_DIR" env \
PLATFORM_API_PROXY="$PLATFORM_API_PROXY" \
VITE_PLATFORM_API_BASE_URL="$VITE_PLATFORM_API_BASE_URL" \
VITE_ENABLE_LOCAL_AUTH_FALLBACK="$VITE_ENABLE_LOCAL_AUTH_FALLBACK" \
npm --prefix platform_web run dev -- --host "0.0.0.0" --port "$LOCAL_DEBUG_WEB_PORT"
wait_for_url platform_web "$(local_debug_web_url)"
cat <<EOF
Local debug stack requested.