|
NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
|
守护进程 更多...
#include <daemon.hpp>
类 | |
| struct | child_config |
| 子进程配置 更多... | |
| struct | child_status |
| 子进程运行时状态 更多... | |
Public 类型 | |
| enum class | daemon_state { stopped , starting , running , reloading , stopping } |
| 守护进程运行状态 更多... | |
| using | start_callback = function<void()> |
| 启动回调,run() 进入主循环前调用 | |
| using | stop_callback = function<void()> |
| 停止回调,收到终止信号后调用 | |
| using | reload_callback = function<bool()> |
| 重载回调,收到 SIGHUP 后调用。返回 true 继续运行,false 请求终止 | |
| using | child_exit_callback = function<void(const string& name, int exit_code)> |
| 子进程退出回调 | |
Public 成员函数 | |
| void | on_start (start_callback cb) |
| 注册启动回调 | |
| void | on_stop (stop_callback cb) |
| 注册停止回调 | |
| void | on_reload (reload_callback cb) |
| 注册重载回调 | |
| void | on_child_exit (child_exit_callback cb) |
| 注册子进程退出回调 | |
| bool | daemonize (const string &work_dir="/") |
| 将当前进程转为守护进程(Linux 双 fork) | |
| bool | write_pid_file (const string &path) |
| 创建并锁定 PID 文件(单实例保护) | |
| void | remove_pid_file () noexcept |
| 删除 PID 文件并释放锁 | |
| bool | add_child (const child_config &cfg) |
| 添加受管理的子进程 | |
| void | remove_child (const string &name) |
| 移除子进程(先优雅终止) | |
| child_status | get_child_status (const string &name) const |
| 获取子进程运行状态 | |
| vector< child_status > | all_child_statuses () const |
| 获取所有子进程状态 | |
| int | run () |
| 进入信号驱动主循环(阻塞) | |
| void | request_shutdown () |
| 请求优雅关闭(可从任何线程调用) | |
| void | request_reload () |
| 请求重载配置(可从任何线程调用) | |
| daemon_state | state () const noexcept |
| 获取当前状态 | |
| void | watchdog_ping () |
| 看门狗喂狗(从工作线程周期性调用) | |
| void | set_watchdog_timeout (int timeout_ms) |
| 设置看门狗超时 | |
静态 Public 成员函数 | |
| static bool | is_pid_file_locked (const string &path) noexcept |
| 检查 PID 文件是否被锁定 | |
| static void | notify_ready () |
| 通知 systemd 服务已就绪 | |
| static void | notify_status (const string &status) |
| 通知 systemd 服务状态 | |
| static void | notify_watchdog () |
| 通知 systemd 看门狗喂狗 | |
守护进程
封装了守护进程的完整生命周期:后台化 → PID 文件锁 → 子进程管理 → 信号驱动主循环 → 优雅关闭。
在文件 daemon.hpp 第 71 行定义.
|
strong |
守护进程运行状态
| 枚举值 | |
|---|---|
| stopped | 未启动 |
| starting | 正在启动 |
| running | 正常运行 |
| reloading | 正在重载配置 |
| stopping | 正在停止 |
在文件 daemon.hpp 第 74 行定义.
| bool neforce::daemon::add_child | ( | const child_config & | cfg | ) |
| bool neforce::daemon::daemonize | ( | const string & | work_dir = "/" | ) |
将当前进程转为守护进程(Linux 双 fork)
| work_dir | 守护进程工作目录 |
|
static |
通知 systemd 服务状态
| status | 状态描述字符串 |
| void neforce::daemon::remove_child | ( | const string & | name | ) |
| void neforce::daemon::set_watchdog_timeout | ( | int | timeout_ms | ) |
设置看门狗超时
| timeout_ms | 超时毫秒数,0 表示禁用 |
| void neforce::daemon::watchdog_ping | ( | ) |
看门狗喂狗(从工作线程周期性调用)
若超过 set_watchdog_timeout() 设置的时长未调用此方法,守护进程将强制退出。
| bool neforce::daemon::write_pid_file | ( | const string & | path | ) |