Skip to content

feat(service): add Windows service wrapper and graceful single-process deployment #73

Closed

Description

@Antruly

背景

doc/worker-process-design.md 已说明:

因此 Windows 上的现实路径是单进程 + 工作线程池 + 外部进程监管。但当前还缺少服务化部署支持。

建议新增

一个通用的 Windows 服务/守护部署支持(可作为 companion 工具或 webapp 辅助模块):

  • 安装/卸载 Windows 服务
  • 服务启动/停止/暂停/继续
  • 优雅停机:停止接收新连接、排空在途请求、关闭 WS、等待任务/子进程退出
  • 崩溃后自动拉起(由 SCM 或 wrapper 负责)
  • 工作目录、环境变量、配置文件路径
  • stdout/stderr 日志重定向与轮转
  • 单实例互斥,避免重复启动

建议 API / 形态

可以是库 API,也可以是独立 uvcpp_service 工具:

uvcpp_service install --exe app.exe --name MyService --config config.json
uvcpp_service uninstall --name MyService
uvcpp_service run --foreground

应用侧只需要实现:

int main() {
  uvcpp_web_app app;
  // ...
  return uvcpp_service_run(app, argc, argv);
}

设计边界

验收

  • Windows 服务可安装/卸载/启停
  • 停止服务时能优雅关闭 HTTP/WS/任务
  • 进程崩溃后 SCM 能拉起
  • 日志能落盘并轮转
  • 单实例互斥生效
  • Linux/macOS 下至少有前台运行模式,便于开发

Activity

  1. added
    enhancement
    on Nov 21, 2026
  2. Antruly commented on Nov 23, 2026

    @Antruly
    OwnerAuthor

    已实现并合入 master(1.11.0-dev,ce81ff8 上五条 CI 全绿)。Windows 的 SCM 安装/启停本身还没有在真 Windows 上实跑过 —— CI 只覆盖编译,所以这条先留着等实机验证。

    API:src/webapp/uvcpp_service.h

    int main(int argc, char** argv) {
      uvcpp::uvcpp_web_app app;
      // 配路由……
      return uvcpp::uvcpp_service_run(app, argc, argv);
    }

    子命令:run [--foreground] / install / uninstall / start / stop / status(缺省 run)。选项:--name、--display-name、--description、--config、--log、--log-max-mb、--log-files、--workdir、--env K=V、--grace-ms、--foreground、--no-single-instance。

    已落地:

    • Windows:CreateServiceW(自动启动)、ChangeServiceConfig2W 配描述与 SC_ACTION_RESTART(5 秒 ×3,一天复位)、STOP/SHUTDOWN → app.stop() + join();非 SCM 启动自动落回前台模式。
    • 前台模式(POSIX/macOS):SIGINT/SIGTERM → 优雅停机;工作目录、环境变量、日志重定向与轮转(1 秒看门狗,x.log → .1…)。
    • 单实例:Windows 命名互斥体 / POSIX flock,冲突返回 UV_EBUSY 并明确报错。
    • 暂停/继续不支持(不接受 SERVICE_ACCEPT_PAUSE_CONTINUE);POSIX 上 install 等返回 UV_ENOTSUP,交给 systemd/launchd。

    验证:test_web_app_service_func(POSIX)覆盖信号停机退出码 0、在途请求在 SIGTERM 后仍拿到完整 body、同名第二实例退出非 0、日志落盘并轮转、子命令错误码。Windows 分支用 MinGW -fsyntax-only 交叉检查;真正的 SCM 安装/停止请在 Windows 机器上跑 app install,再用 sc start / sc stop 验证,通过后即可关闭。

  3. Antruly commented on Nov 23, 2026

    @Antruly
    OwnerAuthor

    SCM 的真机验证补上了,并且是自动化的:新用例 test_web_app_service_scm_func 在 CI 的 MSVC web 格里真跑(runner 是管理员),CI 日志:

    82/121 Test #82: test_web_app_service_scm_func ... Passed 5.67 sec
    

    它做的事正好覆盖本 issue 的验收:install → SCM start → 服务进程写出真实端口、父进程访问 /hello → TerminateProcess 模拟崩溃 → 等 SCM 按 SC_ACTION_RESTART 拉起(pid 变化、新实例再次可访问)→ stop 后服务进程走完优雅停机并写出标记 → uninstall 之后 status 打不到它。

    开关是 CMake 选项 UVCPP_TEST_SERVICE_SCM(默认 OFF,只在这一格打开);没有管理员权限的机器上用例退 3(SKIP),不会伪装成通过。

    结合之前的 POSIX 前台用例(信号停机、在途请求排空、单实例、日志轮转),本 issue 的验收全部有自动化覆盖。关闭。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions