fix(storage): 写盘失败的键回填并限次重试,不再一次失败永久丢
Verify (Gitea) / gate (build · unit · facts) (push) Canceled after 46s
Verify (Gitea) / gate (build · unit · facts) (push) Canceled after 46s
This commit is contained in:
1 parent
0584991a51
commit
fbd3def41c
4 files changed
+42
-9
No files matched your search
+25
-7
@@ -72,10 +72,17 @@
|
||||
// 避免「每次启动 wsWriteKeys 把每块画布都 setItem 一遍」导致所有画布文件被批量重写。
|
||||
const libDirty = new Set()
|
||||
let lastDataSig = ''
|
||||
const schedule = () => {
|
||||
const schedule = (delay) => {
|
||||
if (!enabled()) return
|
||||
if (timer) clearTimeout(timer)
|
||||
timer = setTimeout(() => { timer = null; flush().catch(() => {}) }, 800)
|
||||
timer = setTimeout(() => { timer = null; flush().catch(() => {}) }, delay || 800)
|
||||
}
|
||||
// 2026-12-05:写盘失败不能「一次失败就永久丢」——失败键放回脏集合,限次重试(盘满/无权限时不空转)。
|
||||
let retryBudget = 0
|
||||
const scheduleRetry = () => {
|
||||
if (retryBudget >= 3) return
|
||||
retryBudget++
|
||||
schedule(3000)
|
||||
}
|
||||
const isSplitKey = (k) => isWsKey(k) || !!libFileOf(k)
|
||||
const snapshotParts = () => {
|
||||
@@ -93,7 +100,9 @@
|
||||
if (writing) return writing
|
||||
const wsDirty = Array.from(dirtyWs)
|
||||
const wsGone = Array.from(removedWs)
|
||||
dirtyWs.clear(); removedWs.clear()
|
||||
const libKeys = Array.from(libDirty)
|
||||
const failedWs = [] // 写失败的键:放回脏集合限次重试(2026-12-05)
|
||||
dirtyWs.clear(); removedWs.clear(); libDirty.clear()
|
||||
const parts = snapshotParts()
|
||||
const dataSig = JSON.stringify(parts)
|
||||
const body = JSON.stringify(Object.assign({ v: SCHEMA, at: Date.now() }, parts))
|
||||
@@ -114,15 +123,14 @@
|
||||
const tmp = await join(cdir, k.slice(9) + '.json.tmp')
|
||||
await fs().writeTextFile(tmp, String(text))
|
||||
await fs().rename(tmp, abs)
|
||||
} catch (e) { try { await fs().writeTextFile(abs, String(text)) } catch (e2) { /* ignore */ } }
|
||||
} catch (e) { try { await fs().writeTextFile(abs, String(text)) } catch (e2) { failedWs.push(k) } }
|
||||
}
|
||||
}
|
||||
for (const k of wsGone) {
|
||||
try { await fs().remove(await join(dir, CANVAS_DIR, k.slice(WS_PREFIX.length) + '.json')) } catch (e) { /* 不存在就算了 */ }
|
||||
}
|
||||
// 2026-12-04:资源库只写「内容真的变了」的键(原来每次 flush 整体重写)
|
||||
const libKeys = Array.from(libDirty)
|
||||
libDirty.clear()
|
||||
const failedLib = []
|
||||
if (libKeys.length) {
|
||||
const ldir = await join(dir, LIB_DIR)
|
||||
try { await fs().mkdir(ldir, { recursive: true }) } catch (e) { /* 已存在 */ }
|
||||
@@ -136,7 +144,7 @@
|
||||
await fs().writeTextFile(await join(ldir, rel), String(mem[k]))
|
||||
// 新位置写成功后才删旧布局文件,避免"删了但没写上"造成数据丢失
|
||||
if (rel === 'gallery/index.json') { try { await fs().remove(await join(ldir, 'gallery.json')) } catch (e2) { /* 不存在 */ } }
|
||||
} catch (e) { /* 单个失败不影响其它 */ }
|
||||
} catch (e) { failedLib.push(k) }
|
||||
}
|
||||
}
|
||||
// 2026-12-04:data.json 用内容签名判重(不含 at),没变化就不写
|
||||
@@ -146,8 +154,17 @@
|
||||
await fs().rename(tmp2, target)
|
||||
lastDataSig = dataSig
|
||||
}
|
||||
// 失败的键放回脏集合,限次重试;成功则清零预算
|
||||
if (failedWs.length) for (const k of failedWs) dirtyWs.add(k)
|
||||
if (failedLib.length) for (const k of failedLib) libDirty.add(k)
|
||||
if (failedWs.length || failedLib.length) { scheduleRetry(); return false }
|
||||
retryBudget = 0
|
||||
return true
|
||||
} catch (e) {
|
||||
// 中途异常:本次所有键放回脏集合(重写同内容无害),限次重试
|
||||
for (const k of wsDirty) dirtyWs.add(k)
|
||||
for (const k of libKeys) libDirty.add(k)
|
||||
scheduleRetry()
|
||||
try { await fs().writeTextFile(target, body) } catch (e2) { /* ignore */ }
|
||||
return false
|
||||
}
|
||||
@@ -300,6 +317,7 @@
|
||||
const s = String(v)
|
||||
if (mem[key] === s) return // 内容未变:不标脏、不触发重写(启动时的重复 setItem 全靠这条挡住)
|
||||
mem[key] = s
|
||||
retryBudget = 0
|
||||
if (isWsKey(key)) { removedWs.delete(key); dirtyWs.add(key) }
|
||||
if (libFileOf(key)) libDirty.add(key)
|
||||
schedule()
|
||||
|
||||
Reference in new issue
Block a user