fix(scan): 扫描进行中拒绝重新扫描,避免破坏并发扫描

POST /api/scan 在 scanState.scanning 时直接返回 SCANNING,不再调用 invalidateScan 清空 pkgCache,防止进行中的扫描读到被清空的增量缓存导致数据不一致。前端收到 SCANNING 后转为轮询当前扫描进度,体验与正常扫描一致。
This commit is contained in:
yinjun.chen1
2026-09-22 09:31:38 +08:00
parent 552eb0df3f
commit ce81a59e84
2 changed files with 13 additions and 2 deletions
+5 -1
View File
@@ -372,13 +372,17 @@ app.get('/api/packages/:pkgName', (req, res) => {
// —— API:手动重新扫描缓存目录(异步,扫描期间前端轮询进度) ——
app.post('/api/scan', (req, res) => {
// 扫描进行中时拒绝,避免 invalidateScan 清空 pkgCache 破坏进行中的扫描
if (scanState.scanning) {
return res.json({ ok: false, code: 'SCANNING', message: '扫描进行中,请稍后重试', scanning: true })
}
invalidateScan()
const demo = isDemoMode()
if (demo) {
const scannedAt = new Date().toISOString()
return res.json({ ok: true, demo, scanning: false, scannedAt })
}
if (!scanState.scanning) runScanAsync() // 后台启动,不阻塞响应
runScanAsync() // 后台启动,不阻塞响应
res.json({
ok: true,
demo: false,
+8 -1
View File
@@ -162,7 +162,14 @@ async function scanNow() {
state.loading = true
try {
const res = await api.scanNow()
if (!res.ok) return res
if (!res.ok) {
// 扫描进行中:转为轮询当前扫描进度,扫完再拉取数据
if (res.code === 'SCANNING') {
await pollScanProgress()
await refreshPackages()
}
return res
}
await doRefresh()
return res
} finally {