diff --git a/scripts/release-orphan-running.cjs b/scripts/release-orphan-running.cjs new file mode 100644 index 0000000..915cac8 --- /dev/null +++ b/scripts/release-orphan-running.cjs @@ -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 */ + } +} diff --git a/scripts/systemd/printserver.logrotate b/scripts/systemd/printserver.logrotate new file mode 100644 index 0000000..d1f3f93 --- /dev/null +++ b/scripts/systemd/printserver.logrotate @@ -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 +} diff --git a/scripts/systemd/printserver.service b/scripts/systemd/printserver.service new file mode 100644 index 0000000..0c46b71 --- /dev/null +++ b/scripts/systemd/printserver.service @@ -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