Explorar o código

feat: graceful shutdown, crawl state persistence, task-store write coalescing

- SIGTERM/SIGINT: flush task store + crawl state before exit (double Ctrl+C force-quits)
- crawl progress persisted to <root>/.genpic/crawl-state.json at every shop
  boundary; restored on boot as 'interrupted' with per-shop status and log
- tasks.json: urgent writes on create/start/taskId/terminal, progress ticks
  coalesced to 2s instead of full rewrite every poll
- scripts/smoke.js: zero-dep regression suite spawning a real server on a
  random port (44 checks, safety-scoped to read-only + no-op requests)
KenZ1117 hai 2 semanas
pai
achega
2eb15e8971
Modificáronse 9 ficheiros con 407 adicións e 19 borrados
  1. 3 3
      public/crawl.js
  2. 10 3
      public/gen.js
  3. 1 3
      public/index.html
  4. 4 0
      public/style.css
  5. 4 1
      public/tasks.js
  6. 292 0
      scripts/smoke.js
  7. 15 0
      server.js
  8. 46 0
      server/services/crawl.js
  9. 32 9
      server/services/generation.js

+ 3 - 3
public/crawl.js

@@ -71,7 +71,7 @@ function statusBadgeHtml(st) {
     pending: '排队中', running: '抓取中',
     done: `完成 ${st.downloaded}/${st.total || 0}`,
     skipped_completed: '已完成',
-    failed: '失败', stopped: '已停止',
+    failed: '失败', stopped: '已停止', interrupted: '已中断',
   }[st.state] || st.state;
   const cls = { done: 'badge-ok', failed: 'badge-fail', running: 'badge-warn' }[st.state] || 'badge-info';
   return `<span class="badge ${cls}">${label}</span>`;
@@ -81,8 +81,8 @@ const ICON_PLAY = '<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="tru
 
 function renderCrawlState(cs) {
   const badge = document.getElementById('crawlStatusBadge');
-  const labelMap = { idle: '空闲', running: '抓取中', done: '已完成', stopped: '已停止', error: '出错' };
-  const clsMap = { running: 'badge-warn', done: 'badge-ok', error: 'badge-fail', stopped: 'badge-warn' };
+  const labelMap = { idle: '空闲', running: '抓取中', done: '已完成', stopped: '已停止', error: '出错', interrupted: '已中断' };
+  const clsMap = { running: 'badge-warn', done: 'badge-ok', error: 'badge-fail', stopped: 'badge-warn', interrupted: 'badge-warn' };
   badge.textContent = labelMap[cs.status] || cs.status;
   badge.className = 'badge ' + (clsMap[cs.status] || '');
 

+ 10 - 3
public/gen.js

@@ -335,8 +335,12 @@ function buildPosterCropper(source, spec, context, kind) {
   const panel = el('div', 'poster-crop-panel');
   const status = el('div', 'status-line', `正在生成 ${spec.width}×${spec.height}px 平台图…`);
   const actions = el('div', 'crop-actions');
-  const download = el('a', 'btn btn-ghost btn-small', '下载平台尺寸');
-  const open = el('button', 'btn btn-ghost btn-small', '打开资源目录');
+  const download = el('a', 'btn btn-ghost btn-small icon-btn', '下载平台尺寸');
+  download.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 4v11"/><path d="M7 11l5 5 5-5"/><path d="M4 20h16"/></svg>';
+  download.title = '下载平台尺寸';
+  const open = el('button', 'btn btn-ghost btn-small icon-btn', '打开资源目录');
+  open.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z"/><path d="M3 11h18"/></svg>';
+  open.title = '打开资源目录';
   download.hidden = true; open.disabled = true;
   actions.append(download, open);
   panel.append(status, actions);
@@ -643,7 +647,10 @@ function buildShopCard(shop) {
   info.append(shopNameInput, el('span', 'workflow-folder', folder));
   const actions = el('div', 'workflow-actions');
   const save = el('button', 'btn btn-ghost btn-small', '保存提示词');
-  const open = el('button', 'btn btn-ghost btn-small', '打开目录');
+  const open = el('button', 'btn btn-ghost btn-small icon-btn', '打开目录');
+  open.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z"/><path d="M3 11h18"/></svg>';
+  open.title = '打开目录';
+  open.setAttribute('aria-label', '打开目录');
   actions.append(save, open);
   const countBadge = el('span', 'badge badge-info workflow-count', `${generatedCount}/${totalCount} 已生成`);
   head.append(toggle, info, countBadge, actions);

+ 1 - 3
public/index.html

@@ -491,13 +491,11 @@
               <div id="settingsTabChat" class="settings-tab-body" role="tabpanel" hidden>
               <div class="field">
                 <label>API Key</label>
-                <input type="text" id="settingsChatApiKey" placeholder="留空复用生图 API Key;否则填对话模型专用 Key">
-                <div class="help">留空则复用「图像模型」的 API Key</div>
+                <input type="text" id="settingsChatApiKey" placeholder="留空复用上方 API Key">
               </div>
               <div class="field">
                 <label>API 地址</label>
                 <input type="text" id="settingsChatApiBase" disabled placeholder="默认与生图 API 地址一致">
-                <div class="help">默认使用生图的 API 地址,无需修改</div>
               </div>
               <div class="field">
                 <label>对话模型</label>

+ 4 - 0
public/style.css

@@ -959,3 +959,7 @@ main.content { padding: var(--page-padding-y) var(--page-padding-x) var(--page-p
 .chatbot-input-row input { flex: 1; min-width: 0; }
 .chatbot-input-row .btn { flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center; padding: 0 12px; }
 .chatbot-input-row .btn svg { width: 15px; height: 15px; }
+
+/* 图标按钮(视觉生成 · 保存弹层) */
+.btn.icon-btn { width: 34px; min-width: 34px; padding: 0; justify-content: center; }
+.btn.icon-btn svg { width: 16px; height: 16px; display: block; }

+ 4 - 1
public/tasks.js

@@ -118,7 +118,10 @@
     right.textContent = `${task.progress || progressValue(task) + '%'}${task.fileSizeBytes ? ` · ${formatBytes(task.fileSizeBytes)}` : ''}`;
     actions.appendChild(right);
     if (task.resultUrl) {
-      const open = el('a', 'btn btn-ghost btn-small', '查看结果');
+      const open = el('a', 'btn btn-ghost btn-small icon-btn', '查看结果');
+      open.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M14 5h5v5"/><path d="M19 5l-8 8"/><path d="M19 14v4a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4"/></svg>';
+      open.title = '查看结果';
+      open.setAttribute('aria-label', '查看结果');
       open.href = task.resultUrl;
       open.target = '_blank';
       open.rel = 'noopener noreferrer';

+ 292 - 0
scripts/smoke.js

@@ -0,0 +1,292 @@
+#!/usr/bin/env node
+'use strict';
+
+// 零依赖冒烟测试:拉起真实服务(随机端口),全量核对端点行为后关停。
+// 安全边界:只做只读请求 + 无副作用 POST(校验失败类、空操作);
+// 绝不触碰 DELETE /api/reports/data、/api/crawl/start、/api/open-folder、
+// /api/image/test、/api/chat/test,也不在数据湖有主体时调用 /api/reports/ask。
+// 用法:node scripts/smoke.js
+
+const { spawn } = require('child_process');
+const net = require('net');
+const http = require('http');
+const path = require('path');
+
+const ROOT = path.resolve(__dirname, '..');
+
+function pickPort() {
+  return new Promise((resolve, reject) => {
+    const srv = net.createServer();
+    srv.unref();
+    srv.on('error', reject);
+    srv.listen(0, '127.0.0.1', () => {
+      const port = srv.address().port;
+      srv.close(() => resolve(port));
+    });
+  });
+}
+
+function request(port, method, urlPath, body) {
+  return new Promise((resolve, reject) => {
+    const req = http.request({
+      host: '127.0.0.1', port, method, path: urlPath, timeout: 15000,
+      headers: body !== undefined ? { 'Content-Type': 'application/json' } : {},
+    }, (res) => {
+      const chunks = [];
+      res.on('data', (c) => chunks.push(c));
+      res.on('end', () => resolve({
+        status: res.statusCode,
+        headers: res.headers,
+        text: Buffer.concat(chunks).toString('utf8'),
+        json: () => JSON.parse(Buffer.concat(chunks).toString('utf8')),
+      }));
+    });
+    req.on('error', reject);
+    req.on('timeout', () => req.destroy(new Error('timeout')));
+    if (body !== undefined) req.write(JSON.stringify(body));
+    req.end();
+  });
+}
+
+const results = [];
+async function check(name, fn) {
+  try {
+    await fn();
+    results.push({ name, ok: true });
+  } catch (e) {
+    results.push({ name, ok: false, error: e.message });
+  }
+}
+
+function expect(cond, msg) {
+  if (!cond) throw new Error(msg);
+}
+
+async function waitForHealth(port, timeoutMs = 20000) {
+  const start = Date.now();
+  for (;;) {
+    try {
+      const res = await request(port, 'GET', '/healthz');
+      if (res.status === 200) return;
+    } catch { /* not up yet */ }
+    if (Date.now() - start > timeoutMs) throw new Error('服务未在超时内启动');
+    await new Promise((r) => setTimeout(r, 300));
+  }
+}
+
+function enc(s) { return encodeURIComponent(s); }
+
+async function main() {
+  const port = await pickPort();
+  const child = spawn(process.execPath, [path.join(ROOT, 'server.js')], {
+    env: { ...process.env, PORT: String(port), HOST: '127.0.0.1' },
+    stdio: ['ignore', 'pipe', 'pipe'],
+  });
+  let bootLog = '';
+  child.stdout.on('data', (c) => { bootLog += c; });
+  child.stderr.on('data', (c) => { bootLog += c; });
+
+  try {
+    await waitForHealth(port);
+    let workspaceRoot = '';
+    {
+      const res = await request(port, 'GET', '/api/default-root');
+      workspaceRoot = res.json().root;
+    }
+    const encRoot = enc(workspaceRoot);
+    const b64Root = Buffer.from(workspaceRoot, 'utf8').toString('base64url');
+
+    // ---- 基础与健康 ----
+    await check('GET /healthz', async () => {
+      const r = await request(port, 'GET', '/healthz');
+      expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
+    });
+    await check('GET /readyz', async () => {
+      const r = await request(port, 'GET', '/readyz');
+      const j = r.json();
+      expect(r.status === 200 && typeof j.ok === 'boolean' && typeof j.workspaceReady === 'boolean', `status=${r.status}`);
+    });
+    await check('GET /api/default-root', async () => {
+      const r = await request(port, 'GET', '/api/default-root');
+      expect(r.status === 200 && !!r.json().root, `status=${r.status}`);
+    });
+
+    // ---- 设置域 ----
+    await check('GET /api/models', async () => {
+      const r = await request(port, 'GET', '/api/models');
+      const j = r.json();
+      expect(r.status === 200 && Array.isArray(j.models) && j.models.length >= 5 && !!j.default, `status=${r.status}`);
+    });
+    await check('GET /api/settings(脱敏)', async () => {
+      const r = await request(port, 'GET', '/api/settings');
+      const j = r.json();
+      expect(r.status === 200 && typeof j.rootDir === 'string' && typeof j.port === 'number', `status=${r.status}`);
+      expect(!j.apiKey || j.apiKey.includes('••••'), 'apiKey 未脱敏');
+    });
+    await check('POST /api/settings 非法 pageSize → 400', async () => {
+      const r = await request(port, 'POST', '/api/settings', { pageSize: 9999 });
+      expect(r.status === 400, `status=${r.status}`);
+    });
+    await check('POST /api/settings 非法 delayMs → 400', async () => {
+      const r = await request(port, 'POST', '/api/settings', { delayMs: -5 });
+      expect(r.status === 400, `status=${r.status}`);
+    });
+
+    // ---- 采集域 ----
+    await check('GET /api/session', async () => {
+      const r = await request(port, 'GET', '/api/session');
+      expect(r.status === 200 && typeof r.json().cookieConfigured === 'boolean', `status=${r.status}`);
+    });
+    await check('GET /api/shops(base64url root)', async () => {
+      const r = await request(port, 'GET', `/api/shops?root=${b64Root}`);
+      expect(r.status === 200 && Array.isArray(r.json().shops), `status=${r.status}`);
+    });
+    await check('GET /api/shops 非法 root → 403', async () => {
+      const bad = Buffer.from('/tmp/elsewhere', 'utf8').toString('base64url');
+      const r = await request(port, 'GET', `/api/shops?root=${bad}`);
+      expect(r.status === 403, `status=${r.status}`);
+    });
+    await check('GET /api/scan', async () => {
+      const r = await request(port, 'GET', `/api/scan?root=${encRoot}`);
+      const j = r.json();
+      expect(r.status === 200 && Array.isArray(j.shops), `status=${r.status}`);
+    });
+    await check('GET /api/crawl/status', async () => {
+      const r = await request(port, 'GET', '/api/crawl/status');
+      expect(r.status === 200 && typeof r.json().status === 'string', `status=${r.status}`);
+    });
+    await check('POST /api/crawl/stop(空操作)', async () => {
+      const r = await request(port, 'POST', '/api/crawl/stop');
+      expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
+    });
+
+    // ---- 生成/任务/历史域 ----
+    await check('GET /api/tasks', async () => {
+      const r = await request(port, 'GET', '/api/tasks');
+      const j = r.json();
+      expect(r.status === 200 && j.summary && Array.isArray(j.tasks), `status=${r.status}`);
+    });
+    await check('GET /api/job/未知ID → 404', async () => {
+      const r = await request(port, 'GET', '/api/job/00000000-0000-0000-0000-000000000000');
+      expect(r.status === 404, `status=${r.status}`);
+    });
+    await check('GET /api/creations', async () => {
+      const r = await request(port, 'GET', `/api/creations?root=${encRoot}&limit=5`);
+      const j = r.json();
+      expect(r.status === 200 && typeof j.total === 'number' && Array.isArray(j.items), `status=${r.status}`);
+    });
+    await check('GET /files 越界 → 403', async () => {
+      const r = await request(port, 'GET', `/files?path=${enc('/etc/passwd')}`);
+      expect(r.status === 403, `status=${r.status}`);
+    });
+    await check('GET /files 缺参 → 400', async () => {
+      const r = await request(port, 'GET', '/files');
+      expect(r.status === 400, `status=${r.status}`);
+    });
+
+    // ---- 日志 ----
+    await check('GET /api/logs', async () => {
+      const r = await request(port, 'GET', '/api/logs');
+      expect(r.status === 200 && Array.isArray(r.json().logs), `status=${r.status}`);
+    });
+    await check('POST /api/logs/clear', async () => {
+      const r = await request(port, 'POST', '/api/logs/clear');
+      expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
+    });
+
+    // ---- 数据报表域 ----
+    await check('GET /api/reports/status(不限方法 GET)', async () => {
+      const r = await request(port, 'GET', '/api/reports/status');
+      expect(r.status === 200 && r.json().napos, `status=${r.status}`);
+    });
+    await check('POST /api/reports/status(不限方法 POST)', async () => {
+      const r = await request(port, 'POST', '/api/reports/status');
+      expect(r.status === 200, `status=${r.status}`);
+    });
+    await check('GET /api/reports/columns', async () => {
+      const r = await request(port, 'GET', '/api/reports/columns');
+      expect(r.status === 200 && Array.isArray(r.json().columns) && r.json().columns.length >= 10, `status=${r.status}`);
+    });
+    await check('GET /api/reports/entities', async () => {
+      const r = await request(port, 'GET', '/api/reports/entities');
+      expect(r.status === 200 && Array.isArray(r.json().entities), `status=${r.status}`);
+    });
+    await check('GET /api/reports/sync/status', async () => {
+      const r = await request(port, 'GET', '/api/reports/sync/status');
+      expect(r.status === 200 && typeof r.json().running === 'boolean', `status=${r.status}`);
+    });
+    await check('GET /api/reports/syncs', async () => {
+      const r = await request(port, 'GET', '/api/reports/syncs');
+      expect(r.status === 200 && Array.isArray(r.json().runs), `status=${r.status}`);
+    });
+    await check('GET /api/reports/daily', async () => {
+      const r = await request(port, 'GET', '/api/reports/daily');
+      expect(r.status === 200, `status=${r.status}`);
+    });
+    await check('GET /api/reports/aggregate', async () => {
+      const r = await request(port, 'GET', '/api/reports/aggregate');
+      expect(r.status === 200, `status=${r.status}`);
+    });
+    await check('GET /api/reports/export → CSV', async () => {
+      const r = await request(port, 'GET', '/api/reports/export');
+      expect(r.status === 200 && /text\/csv/.test(r.headers['content-type'] || ''), `status=${r.status}`);
+    });
+    await check('GET /api/reports/report-html → HTML', async () => {
+      const r = await request(port, 'GET', '/api/reports/report-html');
+      expect(r.status === 200 && /text\/html/.test(r.headers['content-type'] || ''), `status=${r.status}`);
+    });
+    await check('POST /api/reports/ask(有主体时跳过)', async () => {
+      const ent = await request(port, 'GET', '/api/reports/entities');
+      if (ent.json().entities.length > 0) return; // 有主体会真调对话模型,跳过
+      const r = await request(port, 'POST', '/api/reports/ask', { q: 'test' });
+      expect(r.status === 400 && r.json().code === 'ASK_NO_ENTITY', `status=${r.status}`);
+    });
+
+    // ---- 静态与路由行为 ----
+    for (const p of ['/', '/index.html', '/app.js', '/crawl.js', '/gen.js', '/history.js', '/tasks.js', '/reports.js', '/style.css', '/vendor/echarts.min.js']) {
+      await check(`静态 ${p}`, async () => {
+        const r = await request(port, 'GET', p);
+        expect(r.status === 200, `status=${r.status}`);
+      });
+    }
+    await check('GET /no-such-path → 404', async () => {
+      const r = await request(port, 'GET', '/no-such-path');
+      expect(r.status === 404, `status=${r.status}`);
+    });
+    await check('DELETE /api/settings → 405', async () => {
+      const r = await request(port, 'DELETE', '/api/settings');
+      expect(r.status === 405, `status=${r.status}`);
+    });
+
+    // ---- 优雅停机(SIGTERM 后应自行退出) ----
+    await check('SIGTERM 优雅停机', async () => {
+      process.kill(child.pid, 'SIGTERM');
+      const exited = await new Promise((resolve) => {
+        const t = setTimeout(() => resolve(false), 8000);
+        child.on('exit', () => { clearTimeout(t); resolve(true); });
+      });
+      expect(exited, 'SIGTERM 后 8 秒内未退出');
+    });
+  } catch (e) {
+    results.push({ name: '(测试执行中断)', ok: false, error: e.message });
+  } finally {
+    try { child.kill('SIGKILL'); } catch { /* already dead */ }
+  }
+
+  const failed = results.filter((r) => !r.ok);
+  console.log('\n========== 冒烟测试结果 ==========');
+  for (const r of results) {
+    console.log(`${r.ok ? '✓' : '✗'} ${r.name}${r.ok ? '' : '  ← ' + r.error}`);
+  }
+  console.log(`\n通过 ${results.length - failed.length}/${results.length}`);
+  if (failed.length) {
+    console.log('\n服务启动日志(末尾 500 字):');
+    console.log(bootLog.slice(-500));
+    process.exit(1);
+  }
+}
+
+main().catch((e) => {
+  console.error('冒烟脚本自身异常:', e);
+  process.exit(1);
+});

+ 15 - 0
server.js

@@ -135,3 +135,18 @@ server.listen(LISTEN_PORT, HOST, () => {
   // the empty "" argument is the window-title placeholder start expects before the URL.
   execFile('cmd', ['/c', 'start', '', url], () => {});
 });
+
+// ---------- 优雅停机:Ctrl+C / kill 时先落盘任务与采集状态,再退出 ----------
+let shuttingDown = false;
+function shutdown(signal) {
+  if (shuttingDown) { process.exit(1); } // 连按两次 Ctrl+C 强制退出
+  shuttingDown = true;
+  console.log(`\n收到 ${signal},正在保存状态并退出...`);
+  try { ctx.generation.flushPersisted(); } catch { /* ignore */ }
+  try { ctx.crawl.flushState(); } catch { /* ignore */ }
+  try { if (server.closeIdleConnections) server.closeIdleConnections(); } catch { /* ignore */ }
+  server.close(() => process.exit(0));
+  setTimeout(() => process.exit(0), 3000).unref();
+}
+process.on('SIGTERM', () => shutdown('SIGTERM'));
+process.on('SIGINT', () => shutdown('SIGINT'));

+ 46 - 0
server/services/crawl.js

@@ -20,6 +20,46 @@ function createCrawlService(ctx) {
   let crawlState = { status: 'idle', startedAt: null, finishedAt: null, shops: [], log: [] };
   let cancelFlag = false;
 
+  // ---------- 采集状态持久化(重启后进度不丢) ----------
+
+  const stateFilePath = () => path.join(workspace.getWorkspaceRoot(), '.genpic', 'crawl-state.json');
+
+  function persistState() {
+    if (crawlState.status === 'idle' && !crawlState.shops.length) return;
+    try {
+      const file = stateFilePath();
+      fs.mkdirSync(path.dirname(file), { recursive: true });
+      const temp = `${file}.${process.pid}.tmp`;
+      const snapshot = { ...crawlState, log: crawlState.log.slice(-100) };
+      fs.writeFileSync(temp, JSON.stringify({ version: 1, savedAt: new Date().toISOString(), state: snapshot }, null, 2), 'utf8');
+      fs.renameSync(temp, file);
+    } catch { /* 持久化失败不影响采集进行 */ }
+  }
+
+  function restoreState() {
+    let parsed;
+    try { parsed = JSON.parse(fs.readFileSync(stateFilePath(), 'utf8')); } catch { return; }
+    const saved = parsed && parsed.version === 1 && parsed.state;
+    if (!saved || typeof saved.status !== 'string') return;
+    crawlState = {
+      status: saved.status,
+      startedAt: saved.startedAt || null,
+      finishedAt: saved.finishedAt || null,
+      forceFullCrawl: !!saved.forceFullCrawl,
+      shops: Array.isArray(saved.shops) ? saved.shops : [],
+      log: Array.isArray(saved.log) ? saved.log : [],
+    };
+    if (crawlState.status === 'running') {
+      crawlState.status = 'interrupted';
+      crawlState.finishedAt = Date.now();
+      for (const s of crawlState.shops) {
+        if (s.state === 'running' || s.state === 'pending') s.state = 'interrupted';
+      }
+      crawlState.log.push(logs.createLogEntry('服务重启,上次采集被中断;未完成的店铺可重新勾选采集', 'task', 'warn'));
+    }
+  }
+  restoreState();
+
   function pushLog(msg) {
     const entry = logs.createLogEntry(msg, 'task', logs.detectLogLevel(msg));
     crawlState.log.push(entry);
@@ -184,6 +224,7 @@ function createCrawlService(ctx) {
       log: [],
     };
     pushLog(`开始抓取,实际抓取 ${pendingShops.length} 家${skippedCompleted.length ? `,历史完成跳过 ${skippedCompleted.length} 家` : ''},root=${root}`);
+    persistState();
     sendJson(res, 200, { ok: true, crawlCount: pendingShops.length, skippedCompletedCount: skippedCompleted.length });
     if (!pendingShops.length) return pushLog('所选店铺均已完成;勾选"强制全量抓取"可重新下载');
 
@@ -191,6 +232,7 @@ function createCrawlService(ctx) {
       crawlState.status = 'error';
       crawlState.finishedAt = Date.now();
       pushLog('抓取流程异常终止: ' + (e.message || e));
+      persistState();
     });
   }
 
@@ -312,17 +354,20 @@ function createCrawlService(ctx) {
       }
       shopEntry.state = shopEntry.failed ? 'failed' : cancelFlag ? 'stopped' : 'done';
       pushLog(`[${shopEntry.shopName}] 完成: 成功${shopEntry.downloaded} 跳过${shopEntry.skipped} 失败${shopEntry.failed}`);
+      persistState();
     }
 
     crawlState.status = cancelFlag ? 'stopped' : 'done';
     crawlState.finishedAt = Date.now();
     pushLog('抓取任务结束');
+    persistState();
   }
 
   function stopCrawl(res) {
     if (crawlState.status !== 'running') return sendJson(res, 200, { ok: true, note: '当前没有正在进行的任务' });
     cancelFlag = true;
     pushLog('收到停止请求,将在当前步骤完成后停止');
+    persistState();
     sendJson(res, 200, { ok: true });
   }
 
@@ -333,6 +378,7 @@ function createCrawlService(ctx) {
     getLog: () => crawlState.log,
     clearLog: () => { crawlState.log = []; },
     readCompletionState,
+    flushState: persistState,
   };
 }
 

+ 32 - 9
server/services/generation.js

@@ -307,6 +307,7 @@ function createGenerationService(ctx) {
   let activeCount = 0;
   const persistedTasks = new Map();
   let taskStoreLoaded = false;
+  let flushTimer = null;
 
   function getTaskStoreFile() {
     return path.join(workspace.getWorkspaceRoot(), '.genpic', 'tasks.json');
@@ -321,9 +322,8 @@ function createGenerationService(ctx) {
     };
   }
 
-  function persistJob(job) {
-    if (!job || !job.id) return;
-    persistedTasks.set(job.id, taskSnapshot(job));
+  function writeTaskStore() {
+    flushTimer = null;
     const storeFile = getTaskStoreFile();
     fs.mkdirSync(path.dirname(storeFile), { recursive: true });
     const tempPath = `${storeFile}.${process.pid}.tmp`;
@@ -335,6 +335,28 @@ function createGenerationService(ctx) {
     fs.renameSync(tempPath, storeFile);
   }
 
+  // 持久化策略:关键节点(新建/启动/拿到供应商任务ID/终态)立即落盘,保证重启可恢复;
+  // 轮询期的进度/花费更新合并为 2 秒一次,避免每个任务每 5 秒全量重写 tasks.json。
+  function persistJob(job, opts = {}) {
+    if (!job || !job.id) return;
+    persistedTasks.set(job.id, taskSnapshot(job));
+    if (opts.urgent) {
+      if (flushTimer) { clearTimeout(flushTimer); flushTimer = null; }
+      try { writeTaskStore(); } catch { /* 磁盘异常不阻断任务 */ }
+      return;
+    }
+    if (!flushTimer) {
+      flushTimer = setTimeout(() => { try { writeTaskStore(); } catch { /* ignore */ } }, 2000);
+      flushTimer.unref();
+    }
+  }
+
+  function flushPersisted() {
+    if (flushTimer) { clearTimeout(flushTimer); flushTimer = null; }
+    if (!persistedTasks.size) return;
+    try { writeTaskStore(); } catch { /* ignore */ }
+  }
+
   function loadPersistedJobs() {
     if (taskStoreLoaded) return;
     taskStoreLoaded = true;
@@ -363,13 +385,13 @@ function createGenerationService(ctx) {
         job.state = 'failed';
         job.finishedAt = Date.now();
         job.error = '服务重启前任务仍在排队,请重新提交';
-        persistJob(job);
+        persistJob(job, { urgent: true });
       } else if (job.state === 'running' || job.state === 'submitted') {
         if (!job.taskId) {
           job.state = 'failed';
           job.finishedAt = Date.now();
           job.error = '服务重启时任务尚未获得供应商任务 ID,请重新提交';
-          persistJob(job);
+          persistJob(job, { urgent: true });
         } else {
           runJob(job);
         }
@@ -410,7 +432,7 @@ function createGenerationService(ctx) {
       finishedAt: null,
     };
     jobs.set(id, job);
-    persistJob(job);
+    persistJob(job, { urgent: true });
     const jobTitle = job.dishName ? `${job.shopName} / ${job.dishName}` : `${job.shopName} / ${job.kind}`;
     logs.pushSystemLog(`生成任务已提交:${jobTitle}(${job.model})`);
     if (activeCount < config.maxConcurrentJobs) {
@@ -429,7 +451,7 @@ function createGenerationService(ctx) {
     activeCount++;
     job.state = 'running';
     job.startedAt = job.startedAt || Date.now();
-    persistJob(job);
+    persistJob(job, { urgent: true });
     processJob(job)
       .catch((e) => {
         job.state = 'failed';
@@ -440,7 +462,7 @@ function createGenerationService(ctx) {
         if (job.state === 'success') logs.pushSystemLog(`生成完成:${jobTitle}`, 'success');
         else if (job.state === 'failed') logs.pushSystemLog(`生成失败:${jobTitle},${job.error || '未知错误'}`, 'error');
         job.finishedAt = Date.now();
-        persistJob(job);
+        persistJob(job, { urgent: true });
         activeCount--;
         const nextId = pendingQueue.shift();
         if (nextId) {
@@ -498,7 +520,7 @@ function createGenerationService(ctx) {
         throw new Error('提交生成任务失败: ' + (submit.json ? JSON.stringify(submit.json) : submit.raw.slice(0, 500)));
       }
       job.taskId = taskId;
-      persistJob(job);
+      persistJob(job, { urgent: true });
     }
 
     await sleep(resuming ? 1000 : 7000);
@@ -601,6 +623,7 @@ function createGenerationService(ctx) {
     getJob: (id) => jobs.get(id),
     listJobs: () => Array.from(jobs.values()),
     loadPersistedJobs,
+    flushPersisted,
     buildImagesAndPrompt,
     readAsDataUri,
     loadCreationHistory,