refactor: 重构巡检报告调度器,移除设备依赖并优化重试逻辑
Some checks failed
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled

- 重构调度器,改为注入获取设备ID的回调,移除硬依赖的deviceStore
- 调整前端设备配置同步逻辑,改为直接读取解密后的本地存储
- 修改重试间隔,从15秒改为3秒,首次同步改为立即执行
- 调整日志级别,优化错误信息输出
This commit is contained in:
2026-10-15 14:15:16 +08:00
parent 2f9f8da48e
commit 2561001dca
6 changed files with 77 additions and 59 deletions

View File

@@ -24,6 +24,8 @@ import {
inspectionSchedulerApi,
type ReportSchedule,
} from "../../infrastructure/inspection/inspectionApi";
import { STORAGE_KEY_HOSTS, STORAGE_KEY_KEYS } from "../../infrastructure/config/storageKeys";
import { decryptHosts, decryptKeys } from "../../infrastructure/persistence/secureFieldAdapter";
interface HostConfig {
device_id: string;
@@ -36,19 +38,39 @@ interface HostConfig {
catalog_id?: string;
}
/** 从 vault 解密 + device extension 构建所有巡检设备 target */
/** 从 localStorage 读加密 hosts/keys → 解密 → 构建所有巡检设备 target */
async function buildTargets(enabledDeviceIds?: string[]): Promise<HostConfig[]> {
const api = (window as any).netcatty;
const hosts = await api?.getHosts?.() ?? [];
const keys = await api?.getSSHKeys?.() ?? [];
// 1. 从 localStorage 读加密的 hosts/keys(VaultPublisher 已解密过的,或自己解密)
let hosts: any[] = [];
let keys: any[] = [];
try {
const rawHosts = localStorage.getItem(STORAGE_KEY_HOSTS);
if (rawHosts) hosts = JSON.parse(rawHosts);
} catch (_) {}
try {
const rawKeys = localStorage.getItem(STORAGE_KEY_KEYS);
if (rawKeys) keys = JSON.parse(rawKeys);
} catch (_) {}
// 2. 解密
let decryptedHosts: any[] = [];
let decryptedKeys: any[] = [];
try { decryptedHosts = await decryptHosts(hosts); } catch (_) { decryptedHosts = hosts; }
try { decryptedKeys = await decryptKeys(keys); } catch (_) { decryptedKeys = keys; }
if (decryptedHosts.length === 0) {
console.error("[ReportAutoScheduler] localStorage 里没有 hosts!hosts=", hosts.length);
return [];
}
// 3. 读 device extension(catalog_id 等)
const exts = await inspectionDeviceApi.getAll().catch(() => ({}));
const targets: HostConfig[] = [];
for (const host of hosts) {
for (const host of decryptedHosts) {
if (enabledDeviceIds && enabledDeviceIds.length > 0 && !enabledDeviceIds.includes(host.id)) continue;
const ext = exts?.[host.id];
const key = keys.find((k: any) => k.id === host.identityFileId);
const key = decryptedKeys.find((k: any) => k.id === host.identityFileId);
const target: HostConfig = {
device_id: host.id,
@@ -129,22 +151,22 @@ export function useReportAutoScheduler() {
try {
const targets = await buildTargets();
if (targets.length === 0) {
// Vault 还没加载好,稍后再试
setTimeout(() => { void syncOnce(); }, 15000);
console.error("[ReportAutoScheduler] buildTargets=0,Vault 可能还没加载,3s 后重试");
setTimeout(() => { void syncOnce(); }, 3000);
return;
}
const hostConfigs = toSchedulerHostConfigs(targets);
await inspectionSchedulerApi.start(hostConfigs);
hostsSyncedRef.current = true;
console.log(`[ReportAutoScheduler] hostConfigs 已同步给主进程,${Object.keys(hostConfigs).length} 台设备`);
console.error(`[ReportAutoScheduler] ✅ hostConfigs 已同步给主进程,${Object.keys(hostConfigs).length} 台设备`);
} catch (e) {
console.warn("[ReportAutoScheduler] 同步 hostConfigs 失败:", e?.message);
setTimeout(() => { void syncOnce(); }, 15000);
console.error("[ReportAutoScheduler] ❌ 同步 hostConfigs 失败:", e?.message);
setTimeout(() => { void syncOnce(); }, 3000);
}
};
// 12 秒后首次同步(等 VaultPublisher 解密 hosts/keys + schedule 加载)
const timer = setTimeout(() => { void syncOnce(); }, 12000);
return () => clearTimeout(timer);
// 立即尝试 + 失败每 3 秒重试
void syncOnce();
return () => { hostsSyncedRef.current = false; };
}, []);
const tick = useCallback(async () => {