Discover Packages
github.com/darkit/daemon
example
advanced
command
Version:
v1.2.5
Opens a new window with list of versions in this module.
Published: Dec 10, 2025
License: Zlib
Opens a new window with license information.
Imports: 7
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
README
¶
高级示例(StructuredDeps + TimeoutConfig + 生命周期钩子)
本示例演示如何在 github.com/darkit/daemon 中同时配置:
StructuredDeps :跨平台描述服务依赖,替代旧的 Dependencies 字符串格式。
TimeoutConfig :显式控制启动 / 停止 / 重启等待时间。
生命周期钩子 :在 Start/Stop 前后执行自定义逻辑,附带简单心跳循环与错误处理。
运行方法
直接运行(开发/调试)
go run ./example/advanced
控制台会每 2 秒打印一次心跳日志,并输出当前运行模式(交互/服务)。
以服务方式安装 / 控制
go run ./example/advanced -service install # 安装
go run ./example/advanced -service start # 启动
go run ./example/advanced -service restart # 重启
go run ./example/advanced -service stop # 停止
go run ./example/advanced -service uninstall # 卸载
安装后可通过系统自带的服务管理器查看日志(systemd journal、Windows 事件查看器等)。
关键配置说明
StructuredDeps :
示例中使用 network.target(After)和 remote-fs.target(Want)。请根据目标平台替换为真实存在的服务名:
Windows 可改为 Tcpip、Dnscache 等常见服务。
macOS / launchd 仅实验性支持 after/require,若无对应服务会被忽略。
TimeoutConfig :
Start:20s、Stop:10s、Restart:5s;未设置的字段沿用各平台默认值。
systemd 会生成 TimeoutStartSec / TimeoutStopSec;其他平台按各自实现处理。
生命周期钩子 :
beforeStartHook/afterStartHook/beforeStopHook/afterStopHook 展示如何在 Start/Stop 周期插入业务逻辑或自检。
主循环在 goroutine 中运行,Stop 会等待最多 5 秒并返回超时错误。
系统特定选项 :
OptionExecStartPre 仅在 systemd 下生效,用于演示启动前钩子。
OptionRestart、OptionRestartSec 在 systemd 下控制重启策略;其他平台会安全忽略。
平台提示
Linux (systemd) :安装后需 systemctl daemon-reload(框架在 install 时已处理),日志可通过 journalctl -u GoServiceExampleAdvanced 查看。
Windows :依赖名称需存在,否则安装会失败;请先将 StructuredDeps 中的名称替换为本机服务名。日志默认写入事件查看器。
macOS (launchd) :依赖为实验性轮询等待,若依赖不存在仅记录警告;其他配置保持兼容。
示例代码位于 example/advanced/main.go,可按需裁剪或替换钩子内容以适配实际业务。
Expand ▾
Collapse ▴
Documentation
¶
高级示例:展示 StructuredDeps、TimeoutConfig 与生命周期钩子用法。
Source Files
¶
Click to show internal directories.
Click to hide internal directories.