-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathworker.hpp
More file actions
56 lines (47 loc) · 3 KB
/
Copy pathworker.hpp
File metadata and controls
56 lines (47 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once
// Worker 主流程: daemon 真实跑 HTTP/WebSocket server 的进程入口。
// 现阶段(Section 4+5)只做最小骨架: GUID 校验 + pid/port/guid/token 文件 +
// HeartbeatWriter 起线程 + 阻塞等终止信号。Section 9 把 Crow server 接进来
// 后,run_worker() 会替换成"启动 Crow + 阻塞 app.run()"。
//
// CLI 三种入口都最终走 run_worker():
// acecode daemon --foreground → standalone 模式
// acecode daemon start (再起子进程) → standalone 模式(detached)
// acecode service install + SCM 启动 → supervised 模式(launcher 派生)
//
// supervised 模式: launcher 通过 --supervised --guid=<G> 派 GUID 进来。
// standalone 模式: 自己生成 GUID。两种模式都会校验"是否已有别的 daemon 在跑"。
#include "../config/config.hpp"
#include <cstdint>
#include <string>
namespace acecode::daemon {
struct WorkerOptions {
bool supervised = false; // true = launcher 派生; false = 独立运行
std::string guid; // supervised 时必填,standalone 时留空(自动生成)
bool foreground = false; // 仅影响日志(stderr 同时输出)。spec 12.3
bool dangerous = false; // 透传 -dangerous,worker 启动期校验
int port_override = 0; // > 0 时覆盖 cfg.web.port(desktop 父进程预选空闲端口后传入)
std::string token_override; // 非空时覆盖自动生成 token(desktop 预生成,前端无需 poll token 文件)
std::string static_dir_override; // 非空时覆盖 cfg.web.static_dir(desktop dev 模式注入仓库 web/ 路径)
std::string cwd_override; // 非空时先切到该目录,让 session/history/tools 使用指定 workspace
bool native_folder_picker_enabled = false; // desktop/webapp 内部:允许 daemon 调 OS 目录选择器
bool desktop_managed = false;
int desktop_protocol_version = 0;
std::int64_t desktop_owner_pid = 0;
std::string desktop_owner_instance;
};
// 主入口。返回进程退出码: 0 成功正常退出,非 0 启动失败 / 异常。
// 阻塞直到收到 SIGTERM/SIGINT(POSIX)或 CTRL_BREAK_EVENT(Windows)。
int run_worker(const WorkerOptions& opts, const AppConfig& cfg);
// 校验"是否可以以新 worker 身份启动":
// - supervised: opts.guid 必须与 ~/.acecode/run/daemon.guid 一致(若文件存在)
// - standalone: 若 runtime file bundle 描述一个当前可用 daemon → 拒启
//
// 返回空字符串 = 通过;否则返回人类可读的拒启理由(调用方应打印 + 非零退出)。
std::string validate_can_start(const WorkerOptions& opts,
int heartbeat_timeout_ms = 15000);
// 触发 worker 优雅退出(从外部线程调,如 ServiceMain 的 SCM 控制 handler)。
// 内部行为等同于 worker 自己注册的 SIGTERM / Console handler — 唤醒主循环
// 让 server.stop() 退,run_worker() 返回。线程安全;反复调无副作用。
void request_worker_termination();
} // namespace acecode::daemon