From 33e379be05fea36ac82f9ad0142d281b42c0bc0b Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 27 Aug 2026 01:11:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(hikvision):=20=E5=A4=B1=E8=B4=A5=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E9=87=8D=E6=89=AB=E6=94=B9=E4=B8=BA3=E5=88=86?= =?UTF-8?q?=E9=92=9F=E5=AE=9A=E6=97=B6=EF=BC=8C=E6=97=A0running=E4=B8=94?= =?UTF-8?q?=E6=9C=80=E8=BF=91=E7=BB=93=E6=9D=9F<5=E5=88=86=E9=92=9F?= =?UTF-8?q?=E6=97=B6=E9=87=8D=E7=BD=AE=E6=9C=80=E6=97=A920=E4=B8=AA?= =?UTF-8?q?=EF=BC=8C=E5=A4=B1=E8=B4=A5=E8=AE=B0=E5=BD=95>10=E6=9D=A1?= =?UTF-8?q?=E6=8E=92=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/main.js | 80 +++++++++++++++++++++++++++++++++++++++++++--------- db/init.sql | 11 +++++++- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/core/main.js b/core/main.js index cd95ae2..df16fc3 100644 --- a/core/main.js +++ b/core/main.js @@ -587,6 +587,17 @@ class Main extends EventEmitter { try { const p = JSON.parse(row.task_params || '{}'); const fp = this._resolvePath(p.output); if (fp && fs.existsSync(fp)) fs.unlinkSync(fp); } catch {} } + // ③b 失败记录:每次重试耗尽(retry>=3,与 notice 无关)写入一条失败记录 + this.db.run(` + INSERT INTO hik_task_fail_log (task_id) + SELECT task_id FROM hik_tasks + WHERE status IN ('failed','timeout','error') AND retry >= 3 AND fail_logged = 0 + `); + this.db.run(` + UPDATE hik_tasks SET fail_logged = 1 + WHERE status IN ('failed','timeout','error') AND retry >= 3 AND fail_logged = 0 + `); + // ④ 每个录像机(IP)最多 maxPerNvr 个并发 worker const maxPerNvr = 3; // 每个 NVR 最多 3 个并发 const maxTotal = 4; // 总并发上限 @@ -704,17 +715,18 @@ class Main extends EventEmitter { this._hikScheduleTimer = setInterval(() => this._scheduleHikTasks(), 3000); console.log('[Hikvision] 任务调度器已启动(每3秒),空闲拉取(间隔5分钟)'); - // 每小时 1 次:录制开启时,重新拉起创建时间 24~48 小时的失败任务 - if (!this._hikHourlyRetryTimer) { - this._hikHourlyRetryTimer = setInterval(() => this._sweepOldFailedTasks(), 3600 * 1000); - console.log('[Hikvision] 失败任务重扫器已启动(每小时1次,仅录制模式,扫描24~48小时前创建的失败任务)'); + // 每 3 分钟 1 次:录制开启时,无进行中任务且最近结束 <5 分钟时,重置最早 20 个失败任务 + if (!this._hikSweepTimer) { + this._hikSweepTimer = setInterval(() => this._sweepOldFailedTasks(), 3 * 60 * 1000); + console.log('[Hikvision] 失败任务重扫器已启动(每3分钟1次,仅录制模式;无running且最近结束<5分钟时重置最早20个失败任务)'); } } /** - * 每小时执行:仅录制模式(isRecording)下, - * 将创建时间在 24~48 小时前且状态为失败的任务 retry 重置为 0, - * 由现有调度器(每3秒)自动重新启动 + * 每 3 分钟执行:仅录制模式(isRecording)下, + * 触发条件:当前无 running 任务,且上一个任务结束时间距现在 < 5 分钟 + * 重置范围:按创建时间升序最早 20 个失败任务(排除失败记录 >10 条的 task_id), + * retry 重置为 0,由现有调度器(每3秒)自动重新启动 */ _sweepOldFailedTasks() { try { @@ -723,16 +735,41 @@ class Main extends EventEmitter { return; // 非录制模式,跳过 } + // 触发条件 1:当前无进行中任务 + const running = this.db.get(`SELECT COUNT(*) AS c FROM hik_tasks WHERE status = 'running'`); + if (running && running.c > 0) { + return; + } + + // 触发条件 2:上一个任务结束时间距现在 < 5 分钟 + const lastEnd = this.db.get(` + SELECT (strftime('%s','now') - strftime('%s', MAX(end_time))) AS age + FROM hik_tasks WHERE end_time IS NOT NULL AND end_time != '' + `); + if (!lastEnd || lastEnd.age === null || lastEnd.age >= 300) { + return; + } + + // 范围:按创建时间升序最早的 20 个失败任务;排除失败记录 > 10 条的 task_id const res = this.db.run(` - UPDATE hik_tasks - SET retry = 0 + UPDATE hik_tasks SET retry = 0, fail_logged = 0 WHERE status IN ('failed','timeout','error') - AND created_at >= datetime('now', '-48 hours') - AND created_at < datetime('now', '-24 hours') + AND task_id NOT IN ( + SELECT task_id FROM hik_task_fail_log GROUP BY task_id HAVING COUNT(*) > 10 + ) + AND id IN ( + SELECT id FROM hik_tasks + WHERE status IN ('failed','timeout','error') + AND task_id NOT IN ( + SELECT task_id FROM hik_task_fail_log GROUP BY task_id HAVING COUNT(*) > 10 + ) + ORDER BY created_at ASC + LIMIT 20 + ) `); if (res.changes > 0) { - console.log(`[Hikvision] 失败任务重扫: ${res.changes} 个(创建于24~48小时前) retry 已重置为 0,等待调度器自动重启`); + console.log(`[Hikvision] 失败任务重扫: ${res.changes} 个 retry 已重置为 0,等待调度器自动重启`); } } catch (e) { console.error('[Hikvision] 失败任务重扫异常:', e.message); @@ -1611,6 +1648,15 @@ class Main extends EventEmitter { try { this.db.exec('ALTER TABLE hik_tasks ADD COLUMN unique_code TEXT DEFAULT \'\''); } catch (_) {} try { this.db.exec('ALTER TABLE hik_tasks ADD COLUMN push_retry INTEGER DEFAULT 0'); } catch (_) {} try { this.db.exec('ALTER TABLE hik_tasks ADD COLUMN push_next_retry_at TEXT DEFAULT \'\''); } catch (_) {} + try { this.db.exec('ALTER TABLE hik_tasks ADD COLUMN fail_logged INTEGER DEFAULT 0'); } catch (_) {} + this.db.exec(` + CREATE TABLE IF NOT EXISTS hik_task_fail_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + task_id TEXT NOT NULL, + created_at TEXT DEFAULT (datetime('now')) + ); + CREATE INDEX IF NOT EXISTS idx_hik_fail_log_task ON hik_task_fail_log(task_id); + `); // 优先读取 db/init.sql const candidates = [ @@ -1664,11 +1710,19 @@ class Main extends EventEmitter { pushed INTEGER DEFAULT 0, start_time TEXT, end_time TEXT, - created_at TEXT DEFAULT (datetime('now')) + created_at TEXT DEFAULT (datetime('now')), + fail_logged INTEGER DEFAULT 0 ); CREATE INDEX IF NOT EXISTS idx_hik_tasks_status ON hik_tasks(status); CREATE INDEX IF NOT EXISTS idx_hik_tasks_worker ON hik_tasks(worker_id); CREATE INDEX IF NOT EXISTS idx_hik_tasks_unique_code ON hik_tasks(unique_code); + + CREATE TABLE IF NOT EXISTS hik_task_fail_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + task_id TEXT NOT NULL, + created_at TEXT DEFAULT (datetime('now')) + ); + CREATE INDEX IF NOT EXISTS idx_hik_fail_log_task ON hik_task_fail_log(task_id); `); console.log('[Main] init.sql 未找到,已执行内联 DDL'); } diff --git a/db/init.sql b/db/init.sql index 2be6ce0..88ffcb0 100644 --- a/db/init.sql +++ b/db/init.sql @@ -37,8 +37,17 @@ CREATE TABLE IF NOT EXISTS hik_tasks ( push_next_retry_at TEXT DEFAULT '', start_time TEXT, end_time TEXT, - created_at TEXT DEFAULT (datetime('now')) + created_at TEXT DEFAULT (datetime('now')), + fail_logged INTEGER DEFAULT 0 ); CREATE INDEX IF NOT EXISTS idx_hik_tasks_unique_code ON hik_tasks(unique_code); CREATE INDEX IF NOT EXISTS idx_hik_tasks_status ON hik_tasks(status); CREATE INDEX IF NOT EXISTS idx_hik_tasks_worker ON hik_tasks(worker_id); + +-- 任务失败记录表(每次重试耗尽记一条;失败记录 > 10 条的任务不再被重扫) +CREATE TABLE IF NOT EXISTS hik_task_fail_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + task_id TEXT NOT NULL, + created_at TEXT DEFAULT (datetime('now')) +); +CREATE INDEX IF NOT EXISTS idx_hik_fail_log_task ON hik_task_fail_log(task_id);