chore(ops): 9.101 printServer 改为 systemd 托管

- 新增 printserver.service:开机自启(enabled)、崩溃自动拉起(Restart=always/RestartSec=5)、StartLimitBurst 防抖

- ExecStartPre 调用 release-orphan-running.cjs,启动前幂等释放断电/崩溃遗留的 running 任务

- KillMode=control-group:只连带本项目 frpc(/opt/printserver/frpc.ini),不影响系统 frpc.service

- 日志改 append(重启不丢),配 logrotate(daily + maxsize 200M / rotate 5 / copytruncate / 压缩)
This commit is contained in:
liufumingren 2026-10-31 00:27:32 +08:00
parent 031778c0e3
commit fe23af7a7d
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,41 @@
#!/usr/bin/env node
/**
* systemd ExecStartPre:释放上次异常退出遗留的 running 任务。
*
* 背景:hik_tasks 里的 running 只是内存态标记,进程一死(断电/崩溃/被 kill)
* 就不会再回执;不释放会一直占住该 NVR 的通道,直到 timeout + 30 秒。
* 本脚本幂等,且只在服务启动前执行。
*
* 用法:node scripts/release-orphan-running.cjs
* 退出码 1 时由 unit 的 `-` 前缀忽略,不阻塞启动。
*/
const path = require('path');
const fs = require('fs');
const dbPath = path.join(__dirname, '..', 'printserver.db');
if (!fs.existsSync(dbPath)) {
console.log('[ExecStartPre] 数据库不存在,跳过:', dbPath);
process.exit(0);
}
let db;
try {
const Database = require('better-sqlite3');
db = new Database(dbPath);
const r = db
.prepare(
"UPDATE hik_tasks SET status='timeout', worker_id=NULL, end_time=datetime('now') WHERE status='running'"
)
.run();
console.log('[ExecStartPre] 释放孤儿 running:', r.changes, '行');
} catch (e) {
console.error('[ExecStartPre] 释放孤儿任务失败:', e.message);
process.exitCode = 1;
} finally {
try {
if (db) db.close();
} catch (_) {
/* ignore */
}
}

View File

@ -0,0 +1,12 @@
/opt/printserver/run.log /opt/printserver/cameraOsd.log {
su root root
daily
maxsize 200M
rotate 5
missingok
notifempty
# systemd 用 append 方式持有 run.log 的 fd,copytruncate 才能安全切割
copytruncate
compress
delaycompress
}

View File

@ -0,0 +1,43 @@
# printServer 录制客户端(9.101 / Linux)
#
# 进程关系(务必别搞混两套 frpc):
# 1) 本服务 = node index.js,它会自己 spawn 本项目的 frpc:
# /usr/local/bin/frpc -c /opt/printserver/frpc.ini
# 该子进程与本服务在同一个 cgroup 内,stop/restart 时一起退出,
# start 时由应用重新拉起(services/frpc.js)。
# 2) 系统自带的 frpc = 独立单元 frpc.service:
# /www/server/frpc/frpc -c /www/server/frpc/frpc.ini (宝塔/Gitea 用)
# 属于别的 cgroup,本服务任何操作都不会影响它。
#
# 安装:
# cp printserver.service /etc/systemd/system/printserver.service
# systemctl daemon-reload
# systemctl enable --now printserver
[Unit]
Description=printServer Recording Client (node index.js)
After=network-online.target
Wants=network-online.target
# 防止异常时疯狂重启(5 分钟内最多 10 次)
StartLimitIntervalSec=300
StartLimitBurst=10
[Service]
Type=simple
WorkingDirectory=/opt/printserver
# 启动前释放断电/崩溃遗留的 running 任务(幂等,失败不阻塞启动)
ExecStartPre=-/usr/bin/node /opt/printserver/scripts/release-orphan-running.cjs
ExecStart=/usr/bin/node /opt/printserver/index.js
# 崩溃/被 kill 后自动拉起
Restart=always
RestartSec=5
# 停服时给 node 和它 spawn 的本项目 frpc 都发 SIGTERM(system frpc 在别的 cgroup,不受影响)
KillMode=control-group
KillSignal=SIGTERM
TimeoutStopSec=20
# append 而不是 truncate:重启不丢日志;体积由 /etc/logrotate.d/printserver 控制
StandardOutput=append:/opt/printserver/run.log
StandardError=append:/opt/printserver/run.log
[Install]
WantedBy=multi-user.target