fix: improve spacetime start diagnostics
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
2026-04-27 10:00:31 +08:00
parent 5eb3c7abfb
commit 4ac2da5de1
3 changed files with 145 additions and 24 deletions

View File

@@ -548,19 +548,25 @@ wait_for_spacetime() {
local deadline=$((SECONDS + SPACETIME_TIMEOUT_SECONDS))
while ((SECONDS < deadline)); do
if [[ -n "${process_pid}" ]] && ! kill -0 "${process_pid}" 2>/dev/null; then
echo "[start] SpacetimeDB 进程在就绪前退出。" >&2
exit 1
fi
if is_spacetime_ready; then
return
fi
if [[ -n "${process_pid}" ]] && ! kill -0 "${process_pid}" 2>/dev/null; then
echo "[start] SpacetimeDB 进程在就绪前退出。" >&2
print_spacetime_start_diagnostics
exit 1
fi
sleep 0.5
done
if is_spacetime_ready; then
return
fi
echo "[start] 等待 SpacetimeDB 就绪超时: ${SPACETIME_SERVER_URL}" >&2
print_spacetime_start_diagnostics
exit 1
}
@@ -575,6 +581,42 @@ is_spacetime_ready() {
[[ "${output}" == *"Server is online:"* ]]
}
print_spacetime_start_diagnostics() {
local log_file="${LOG_DIR}/spacetimedb.log"
local root_owner=""
# SpacetimeDB 启动日志默认重定向到文件;失败时主动回显关键现场,避免只看到“就绪前退出”。
echo "[start] SpacetimeDB 启动诊断:" >&2
echo "[start] - server: ${SPACETIME_SERVER_URL}" >&2
echo "[start] - listen: ${SPACETIME_HOST}:${SPACETIME_PORT}" >&2
echo "[start] - root-dir: ${SPACETIME_ROOT_DIR}" >&2
echo "[start] - log: ${log_file}" >&2
if [[ -f "${log_file}" ]]; then
echo "[start] ${log_file} 最近 120 行:" >&2
tail -n 120 "${log_file}" >&2 || true
else
echo "[start] 尚未生成 ${log_file}" >&2
fi
echo "[start] server ping 结果:" >&2
spacetime --root-dir="${SPACETIME_ROOT_DIR}" server ping "${SPACETIME_SERVER_URL}" >&2 || true
if command -v ss >/dev/null 2>&1; then
echo "[start] ${SPACETIME_PORT} 端口监听检查:" >&2
ss -ltnp 2>/dev/null | awk -v listen=":${SPACETIME_PORT}" 'NR == 1 || index($0, listen) > 0 { print }' >&2 || true
elif command -v netstat >/dev/null 2>&1; then
echo "[start] ${SPACETIME_PORT} 端口监听检查:" >&2
netstat -ltnp 2>/dev/null | awk -v listen=":${SPACETIME_PORT}" 'NR == 1 || index($0, listen) > 0 { print }' >&2 || true
fi
root_owner="$(describe_spacetime_root_owner || true)"
if [[ -n "${root_owner}" ]]; then
echo "[start] root-dir 相关 SpacetimeDB 进程:" >&2
echo "${root_owner}" >&2
fi
}
describe_spacetime_root_owner() {
if command -v ps >/dev/null 2>&1; then
ps -eo user=,pid=,ppid=,stat=,comm=,args= 2>/dev/null | awk -v root_dir="${SPACETIME_ROOT_DIR}" '
@@ -600,7 +642,9 @@ describe_spacetime_root_owner() {
sync_ubuntu_spacetime_install() {
local root_dir="$1"
local target_cli="${root_dir}/bin/current/spacetimedb-cli"
local target_bin_dir="${root_dir}/bin/current"
local target_cli="${target_bin_dir}/spacetimedb-cli"
local target_standalone="${target_bin_dir}/spacetimedb-standalone"
local spacetime_command=""
local resolved_command=""
local install_dir=""
@@ -609,7 +653,7 @@ sync_ubuntu_spacetime_install() {
local share_bin_dir=""
local version_dir=""
if [[ -x "${target_cli}" ]]; then
if [[ -x "${target_cli}" && -x "${target_standalone}" ]]; then
return
fi
@@ -632,11 +676,12 @@ sync_ubuntu_spacetime_install() {
"${HOME:-}/.local/share/spacetime/bin"; do
if [[ -d "${share_bin_dir}" ]]; then
version_dir="$(find "${share_bin_dir}" -mindepth 1 -maxdepth 1 -type d | sort -V | tail -n 1)"
if [[ -n "${version_dir}" && -x "${version_dir}/spacetimedb-cli" ]]; then
echo "[start] 同步 Ubuntu SpacetimeDB CLI: ${version_dir}/spacetimedb-cli -> ${target_cli}"
mkdir -p "${root_bin}/current"
cp -f "${version_dir}/spacetimedb-cli" "${target_cli}"
chmod +x "${target_cli}"
if [[ -n "${version_dir}" && -x "${version_dir}/spacetimedb-cli" && -x "${version_dir}/spacetimedb-standalone" ]]; then
echo "[start] 同步 Ubuntu SpacetimeDB 安装: ${version_dir} -> ${target_bin_dir}"
rm -rf "${target_bin_dir}"
mkdir -p "${target_bin_dir}"
cp -a "${version_dir}/." "${target_bin_dir}/"
chmod +x "${target_cli}" "${target_standalone}"
return
fi
fi
@@ -648,27 +693,27 @@ sync_ubuntu_spacetime_install() {
elif [[ -x "${install_dir}/current/spacetimedb-cli" ]]; then
echo "[start] 同步 Ubuntu SpacetimeDB 安装: ${install_dir} -> ${root_bin}"
cp -a "${install_dir}/." "${root_bin}/"
elif [[ -x "${install_dir}/spacetimedb-cli" ]]; then
echo "[start] 同步 Ubuntu SpacetimeDB CLI: ${install_dir}/spacetimedb-cli -> ${target_cli}"
mkdir -p "${root_bin}/current"
elif [[ -x "${install_dir}/spacetimedb-cli" && -x "${install_dir}/spacetimedb-standalone" ]]; then
echo "[start] 同步 Ubuntu SpacetimeDB 安装: ${install_dir} -> ${target_bin_dir}"
rm -rf "${target_bin_dir}"
mkdir -p "${target_bin_dir}"
cp -f "${install_dir}/spacetimedb-cli" "${target_cli}"
chmod +x "${target_cli}"
cp -f "${install_dir}/spacetimedb-standalone" "${target_standalone}"
chmod +x "${target_cli}" "${target_standalone}"
elif [[ -f "${resolved_command}" ]]; then
parent_dir="$(cd -- "${install_dir}/.." && pwd)"
if [[ -d "${parent_dir}/bin" && -x "${parent_dir}/bin/current/spacetimedb-cli" ]]; then
if [[ -d "${parent_dir}/bin" && -x "${parent_dir}/bin/current/spacetimedb-cli" && -x "${parent_dir}/bin/current/spacetimedb-standalone" ]]; then
echo "[start] 同步 Ubuntu SpacetimeDB 安装: ${parent_dir}/bin -> ${root_bin}"
cp -a "${parent_dir}/bin/." "${root_bin}/"
else
echo "[start] 同步 Ubuntu SpacetimeDB 命令: ${resolved_command} -> ${target_cli}"
mkdir -p "${root_bin}/current"
cp -f "${resolved_command}" "${target_cli}"
chmod +x "${target_cli}"
echo "[start] 未能从 spacetime 命令路径推断完整 SpacetimeDB 安装目录: ${resolved_command}" >&2
fi
fi
if [[ ! -x "${target_cli}" ]]; then
echo "[start] 同步 SpacetimeDB 安装后仍未找到 ${target_cli}。" >&2
echo "[start] 请确认 Ubuntu 上的 spacetime 安装目录包含 bin/current/spacetimedb-cli或提供可执行的 spacetime 命令。" >&2
if [[ ! -x "${target_cli}" || ! -x "${target_standalone}" ]]; then
echo "[start] 同步 SpacetimeDB 安装后仍未找到完整 current 目录。" >&2
echo "[start] 需要同时存在: ${target_cli} 与 ${target_standalone}" >&2
echo "[start] 请确认 Ubuntu 上的 spacetime 安装目录包含 spacetimedb-cli 和 spacetimedb-standalone。" >&2
exit 1
fi
}