printServer/scripts/release-orphan-running.cjs
liufumingren fe23af7a7d 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 / 压缩)
2026-10-31 00:27:32 +08:00

42 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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 */
}
}