Merge branch 'master' of http://82.157.175.59:3000/GenarrativeAI/Genarrative
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
2026-05-10 13:19:03 +08:00
17 changed files with 1934 additions and 56 deletions

View File

@@ -191,6 +191,111 @@ port_from_listen_addr() {
echo "${listen_addr##*:}"
}
spacetime_url_record_path() {
local data_dir="$1"
echo "${data_dir}/dev-rust-spacetime-url"
}
spacetime_start_log_path() {
local data_dir="$1"
echo "${data_dir}/logs/dev-rust-spacetime-start.log"
}
spacetime_standalone_log_path() {
local data_dir="$1"
echo "${data_dir}/logs/spacetime-standalone.log"
}
read_spacetime_pid() {
local data_dir="$1"
local pid_file="${data_dir}/spacetime.pid"
if [[ ! -f "${pid_file}" ]]; then
return 1
fi
local pid
pid="$(tr -cd '0-9' <"${pid_file}" | head -c 20)"
if [[ -z "${pid}" ]]; then
return 1
fi
echo "${pid}"
}
try_reuse_existing_spacetime() {
local data_dir="$1"
local url_file
url_file="$(spacetime_url_record_path "${data_dir}")"
local existing_pid
local recorded_url=""
if ! existing_pid="$(read_spacetime_pid "${data_dir}")"; then
return 1
fi
if ! kill -0 "${existing_pid}" 2>/dev/null; then
echo "[dev:rust] 发现过期 spacetime.pid: ${existing_pid},将重新启动 SpacetimeDB。"
return 1
fi
if [[ ! -f "${url_file}" ]]; then
local start_log
start_log="$(spacetime_start_log_path "${data_dir}")"
if [[ -f "${start_log}" ]]; then
local logged_addr
logged_addr="$(sed -n 's/^.*Starting SpacetimeDB listening on \([^[:space:]]\+\).*$/\1/p' "${start_log}" | tail -n 1)"
if [[ -n "${logged_addr}" ]]; then
recorded_url="http://${logged_addr}"
fi
fi
if [[ -z "${recorded_url}" ]]; then
local standalone_log
standalone_log="$(spacetime_standalone_log_path "${data_dir}")"
if [[ -f "${standalone_log}" ]]; then
local standalone_addr
standalone_addr="$(sed -n 's/^.*Starting SpacetimeDB listening on \([^[:space:]]\+\).*$/\1/p' "${standalone_log}" | tail -n 1)"
if [[ -n "${standalone_addr}" ]]; then
recorded_url="http://${standalone_addr}"
fi
fi
fi
if [[ -z "${recorded_url}" ]]; then
echo "[dev:rust] 发现运行中的 SpacetimeDB pid=${existing_pid},但未找到 URL 记录: ${url_file}"
return 1
fi
else
recorded_url="$(head -n 1 "${url_file}" | tr -d '\r' | xargs)"
fi
if [[ -z "${recorded_url}" ]]; then
echo "[dev:rust] SpacetimeDB URL 记录为空: ${url_file}"
return 1
fi
if is_spacetime_ready "${recorded_url}"; then
SPACETIME_SERVER="${recorded_url}"
SPACETIME_PORT="$(port_from_listen_addr "${recorded_url}")"
SPACETIME_REUSED_EXISTING=1
echo "[dev:rust] 复用已启动 SpacetimeDB: ${SPACETIME_SERVER} (pid=${existing_pid})"
return 0
fi
echo "[dev:rust] pid=${existing_pid} 存在,但 URL 不在线: ${recorded_url},将重新启动 SpacetimeDB。"
return 1
}
record_spacetime_server_url() {
local data_dir="$1"
local server="$2"
local url_file
url_file="$(spacetime_url_record_path "${data_dir}")"
mkdir -p "${data_dir}"
printf '%s\n' "${server}" >"${url_file}"
echo "[dev:rust] 记录 SpacetimeDB URL: ${url_file} -> ${server}"
}
is_spacetime_ready() {
local server="$1"
local output
@@ -466,6 +571,7 @@ SKIP_PUBLISH=0
PRESERVE_DATABASE=0
MIGRATION_BOOTSTRAP_SECRET=""
MIGRATION_BOOTSTRAP_SECRET_MODE="auto"
SPACETIME_REUSED_EXISTING=0
PIDS=()
NAMES=()
@@ -645,35 +751,38 @@ ensure_tcp_port_available "后台 Vite" "${ADMIN_WEB_HOST}" "${ADMIN_WEB_PORT}"
if [[ "${SKIP_SPACETIME}" -ne 1 ]]; then
mkdir -p "${SPACETIME_ROOT_DIR}" "${SPACETIME_DATA_DIR}"
SPACETIME_ROOT_OWNER="$(describe_spacetime_root_owner "${SPACETIME_DATA_DIR}")"
if [[ -n "${SPACETIME_ROOT_OWNER}" ]]; then
echo "[dev:rust] 当前 data-dir 已被其他 SpacetimeDB 实例占用,无法再次启动。" >&2
echo "[dev:rust] 如需复用,请传入占用实例实际端口并追加 --skip-spacetime如需重启请先停止下列进程。" >&2
echo "${SPACETIME_ROOT_OWNER}" >&2
exit 1
if ! try_reuse_existing_spacetime "${SPACETIME_DATA_DIR}"; then
SPACETIME_ROOT_OWNER="$(describe_spacetime_root_owner "${SPACETIME_DATA_DIR}")"
if [[ -n "${SPACETIME_ROOT_OWNER}" ]]; then
echo "[dev:rust] 当前 data-dir 已被其他 SpacetimeDB 实例占用,无法再次启动。" >&2
echo "[dev:rust] 如需复用,请确认 ${SPACETIME_DATA_DIR}/dev-rust-spacetime-url 记录了实例 URL或传入占用实例实际端口并追加 --skip-spacetime如需重启请先停止下列进程。" >&2
echo "${SPACETIME_ROOT_OWNER}" >&2
exit 1
fi
SPACETIME_START_LOG="$(spacetime_start_log_path "${SPACETIME_DATA_DIR}")"
mkdir -p "$(dirname -- "${SPACETIME_START_LOG}")"
: >"${SPACETIME_START_LOG}"
echo "[dev:rust] 启动 spacetimedb"
(
cd "${SERVER_RS_DIR}"
# 当目标端口被占用时SpacetimeDB 会询问是否使用最近的可用端口;
# 这里直接发送回车接受默认建议,再从启动日志解析实际监听端口。
printf '\n' | spacetime \
start \
--data-dir "${SPACETIME_DATA_DIR}" \
--listen-addr "${SPACETIME_HOST}:${SPACETIME_PORT}" \
--non-interactive
) 2>&1 | tee "${SPACETIME_START_LOG}" &
PIDS+=("$!")
NAMES+=("spacetimedb")
SPACETIME_LISTEN_ADDR="$(wait_for_spacetime_listen_addr "${SPACETIME_START_LOG}" "${SPACETIME_TIMEOUT_SECONDS}" "${PIDS[0]:-}")"
SPACETIME_PORT="$(port_from_listen_addr "${SPACETIME_LISTEN_ADDR}")"
SPACETIME_SERVER="http://${SPACETIME_HOST}:${SPACETIME_PORT}"
echo "[dev:rust] spacetime actual: ${SPACETIME_SERVER}"
record_spacetime_server_url "${SPACETIME_DATA_DIR}" "${SPACETIME_SERVER}"
fi
SPACETIME_START_LOG="${SPACETIME_DATA_DIR}/logs/dev-rust-spacetime-start.log"
mkdir -p "$(dirname -- "${SPACETIME_START_LOG}")"
: >"${SPACETIME_START_LOG}"
echo "[dev:rust] 启动 spacetimedb"
(
cd "${SERVER_RS_DIR}"
# 当目标端口被占用时SpacetimeDB 会询问是否使用最近的可用端口;
# 这里直接发送回车接受默认建议,再从启动日志解析实际监听端口。
printf '\n' | spacetime \
start \
--data-dir "${SPACETIME_DATA_DIR}" \
--listen-addr "${SPACETIME_HOST}:${SPACETIME_PORT}" \
--non-interactive
) 2>&1 | tee "${SPACETIME_START_LOG}" &
PIDS+=("$!")
NAMES+=("spacetimedb")
SPACETIME_LISTEN_ADDR="$(wait_for_spacetime_listen_addr "${SPACETIME_START_LOG}" "${SPACETIME_TIMEOUT_SECONDS}" "${PIDS[0]:-}")"
SPACETIME_PORT="$(port_from_listen_addr "${SPACETIME_LISTEN_ADDR}")"
SPACETIME_SERVER="http://${SPACETIME_HOST}:${SPACETIME_PORT}"
echo "[dev:rust] spacetime actual: ${SPACETIME_SERVER}"
fi
if [[ "${SKIP_PUBLISH}" -ne 1 ]]; then
@@ -686,6 +795,7 @@ if [[ "${SKIP_PUBLISH}" -ne 1 ]]; then
"${DATABASE}"
--server "${SPACETIME_SERVER}"
--module-path "${MODULE_PATH}"
--build-options="--debug"
)
if [[ "${PRESERVE_DATABASE}" -ne 1 ]]; then