NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
neforce::daemon类 参考

守护进程 更多...

#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_statusall_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 d;
d.on_start([] { logger::info("starting"); });
d.on_stop([] { logger::info("stopping"); });
d.daemonize("/");
d.write_pid_file("/var/run/myapp.pid");
daemon::child_config cfg;
cfg.name = "worker";
cfg.executable = "/usr/bin/myworker";
cfg.auto_restart = true;
d.add_child(cfg);
return d.run();
int run()
进入信号驱动主循环(阻塞)
bool daemonize(const string &work_dir="/")
将当前进程转为守护进程(Linux 双 fork)
bool write_pid_file(const string &path)
创建并锁定 PID 文件(单实例保护)
bool add_child(const child_config &cfg)
添加受管理的子进程
void on_stop(stop_callback cb)
注册停止回调
void on_start(start_callback cb)
注册启动回调
void info(string msg, source_location loc)
INFO 级别快捷方法

在文件 daemon.hpp71 行定义.

成员枚举类型说明

◆ daemon_state

enum class neforce::daemon::daemon_state
strong

守护进程运行状态

枚举值
stopped 

未启动

starting 

正在启动

running 

正常运行

reloading 

正在重载配置

stopping 

正在停止

在文件 daemon.hpp74 行定义.

成员函数说明

◆ add_child()

bool neforce::daemon::add_child ( const child_config & cfg)

添加受管理的子进程

参数
cfg子进程配置
返回
true 添加成功,false 同名已存在

引用了 add_child().

被这些函数引用 add_child().

◆ daemonize()

bool neforce::daemon::daemonize ( const string & work_dir = "/")

将当前进程转为守护进程(Linux 双 fork)

参数
work_dir守护进程工作目录
返回
父进程返回 false,子进程返回 true
注解
Windows 上始终返回 true

◆ notify_status()

void neforce::daemon::notify_status ( const string & status)
static

通知 systemd 服务状态

参数
status状态描述字符串

◆ remove_child()

void neforce::daemon::remove_child ( const string & name)

移除子进程(先优雅终止)

参数
name子进程名称

引用了 remove_child().

被这些函数引用 remove_child().

◆ run()

int neforce::daemon::run ( )

进入信号驱动主循环(阻塞)

返回
退出码,0 为正常退出

引用了 run().

被这些函数引用 run().

◆ set_watchdog_timeout()

void neforce::daemon::set_watchdog_timeout ( int timeout_ms)

设置看门狗超时

参数
timeout_ms超时毫秒数,0 表示禁用

◆ watchdog_ping()

void neforce::daemon::watchdog_ping ( )

看门狗喂狗(从工作线程周期性调用)

若超过 set_watchdog_timeout() 设置的时长未调用此方法,守护进程将强制退出。

◆ write_pid_file()

bool neforce::daemon::write_pid_file ( const string & path)

创建并锁定 PID 文件(单实例保护)

参数
pathPID 文件路径
返回
true 创建成功;false 已被其他实例锁定
异常
daemon_exception创建失败时抛出

该类的文档由以下文件生成: