diff --git a/deploy/manage.sh b/deploy/manage.sh index 4e1d543..40bd759 100644 --- a/deploy/manage.sh +++ b/deploy/manage.sh @@ -200,16 +200,27 @@ fix_permissions() { wait_until_ready() { local ready=0 local _i - for _i in 1 2 3 4 5 6 7 8 9 10; do - if curl -fsS "http://127.0.0.1:${BIND_PORT}/health" >/dev/null; then + echo "等待服务监听 ${BIND_PORT} ..." + for ((_i = 1; _i <= 40; _i++)); do + if curl -fsS "http://127.0.0.1:${BIND_PORT}/health" >/dev/null 2>&1; then ready=1 break fi sleep 1 done if [[ "${ready}" -ne 1 ]]; then - echo "服务未在 10 秒内就绪。最近日志:" >&2 - journalctl -u "${SERVICE_NAME}.service" -n 50 --no-pager >&2 || true + echo "服务未在 40 秒内就绪。状态和最近日志:" >&2 + systemctl status "${SERVICE_NAME}.service" --no-pager >&2 || true + journalctl -u "${SERVICE_NAME}.service" -n 80 --no-pager >&2 || true + return 1 + fi + echo "健康检查通过:http://127.0.0.1:${BIND_PORT}/health" +} + +verify_app() { + log "检查程序能否启动" + if ! /usr/sbin/runuser -u "${APP_USER}" -- bash -c "cd '${INSTALL_ROOT}' && exec '${INSTALL_ROOT}/.venv/bin/python' -c 'import app.main'"; then + echo "程序导入失败,没有重启服务。请把上面的报错发出来。" >&2 return 1 fi } @@ -223,9 +234,10 @@ cmd_deploy() { install_python_deps fix_permissions write_service + verify_app || return 1 systemctl enable "${SERVICE_NAME}.service" systemctl restart "${SERVICE_NAME}.service" - wait_until_ready + wait_until_ready || return 1 log "部署完成" echo "监听地址:${BIND_HOST}:${BIND_PORT}" echo "健康检查:curl http://127.0.0.1:${BIND_PORT}/health" @@ -257,8 +269,9 @@ cmd_update() { install_python_deps fix_permissions write_service + verify_app || return 1 systemctl restart "${SERVICE_NAME}.service" - wait_until_ready + wait_until_ready || return 1 log "更新完成,配置和数据库已保留" }