Persist api-server logs and refresh recharge balance

This commit is contained in:
2026-05-15 01:07:39 +08:00
parent 2801b55d2f
commit 8ade75390c
11 changed files with 406 additions and 54 deletions

View File

@@ -653,11 +653,13 @@ wait_for_api_server() {
local health_url="$1"
local timeout_seconds="$2"
local process_pid="${3:-}"
local log_file="${4:-${API_SERVER_LOG_FILE:-}}"
local deadline=$((SECONDS + timeout_seconds))
while ((SECONDS < deadline)); do
if [[ -n "${process_pid}" ]] && ! kill -0 "${process_pid}" 2>/dev/null; then
echo "[dev:rust] api-server 进程在就绪前退出。" >&2
print_api_server_log_tail "${log_file}"
exit 1
fi
@@ -679,9 +681,58 @@ request.on("error", () => process.exit(1));
done
echo "[dev:rust] 等待 api-server 就绪超时: ${health_url}" >&2
print_api_server_log_tail "${log_file}"
exit 1
}
format_api_server_log_timestamp() {
date +%Y%m%d-%H%M%S
}
normalize_api_server_log_path() {
local path_value="$1"
if [[ "${path_value}" == *\\* ]]; then
path_value="${path_value//\\//}"
fi
echo "${path_value}"
}
resolve_api_server_log_file() {
local explicit_log_file="${GENARRATIVE_API_SERVER_LOG_FILE:-}"
local log_dir="${GENARRATIVE_API_SERVER_LOG_DIR:-${REPO_ROOT}/logs/api-server}"
if [[ -n "${explicit_log_file//[[:space:]]/}" ]]; then
explicit_log_file="$(normalize_api_server_log_path "${explicit_log_file}")"
if [[ "${explicit_log_file}" = /* || "${explicit_log_file}" =~ ^[A-Za-z]:[\\/] ]]; then
echo "${explicit_log_file}"
return
fi
echo "${REPO_ROOT}/${explicit_log_file}"
return
fi
log_dir="$(normalize_api_server_log_path "${log_dir}")"
if [[ ! "${log_dir}" = /* && ! "${log_dir}" =~ ^[A-Za-z]:[\\/] ]]; then
log_dir="${REPO_ROOT}/${log_dir}"
fi
echo "${log_dir}/api-server-dev-rust-$(format_api_server_log_timestamp).log"
}
print_api_server_log_tail() {
local log_file="${1:-}"
if [[ -z "${log_file}" || ! -f "${log_file}" ]]; then
return
fi
echo "[dev:rust] api-server 最近日志: ${log_file}" >&2
tail -n 80 "${log_file}" >&2 || true
}
generate_migration_bootstrap_secret() {
node -e 'const crypto = require("crypto"); process.stdout.write(crypto.randomBytes(32).toString("hex"));'
@@ -990,22 +1041,26 @@ API_PORT="$(find_nearest_available_port "${API_HOST}" "${API_PORT}" "api-server"
API_TARGET_HOST="$(resolve_client_host "${API_HOST}")"
# `.env.local` 可以给单独 `dev:web` 配置代理目标,但完整栈的前端必须跟随本次 `--api-port`。
RUST_SERVER_TARGET="http://${API_TARGET_HOST}:${API_PORT}"
API_SERVER_LOG_FILE="$(resolve_api_server_log_file)"
mkdir -p "$(dirname -- "${API_SERVER_LOG_FILE}")"
echo "[dev:rust] api actual: ${RUST_SERVER_TARGET}"
echo "[dev:rust] api-server log: ${API_SERVER_LOG_FILE}"
(
cd "${REPO_ROOT}"
GENARRATIVE_API_HOST="${API_HOST}" \
GENARRATIVE_API_PORT="${API_PORT}" \
GENARRATIVE_API_LOG="${API_LOG}" \
GENARRATIVE_API_SERVER_LOG_FILE="${API_SERVER_LOG_FILE}" \
GENARRATIVE_SPACETIME_SERVER_URL="${SPACETIME_SERVER}" \
GENARRATIVE_SPACETIME_DATABASE="${DATABASE}" \
exec cargo run -p api-server --manifest-path "${MANIFEST_PATH}"
) &
) > >(tee -a "${API_SERVER_LOG_FILE}") 2>&1 &
API_PID="$!"
PIDS+=("${API_PID}")
NAMES+=("api-server")
echo "[dev:rust] 等待 api-server 就绪"
wait_for_api_server "${RUST_SERVER_TARGET}/healthz" "${API_SERVER_TIMEOUT_SECONDS}" "${API_PID}"
wait_for_api_server "${RUST_SERVER_TARGET}/healthz" "${API_SERVER_TIMEOUT_SECONDS}" "${API_PID}" "${API_SERVER_LOG_FILE}"
echo "[dev:rust] 启动 vite"
(