fix: 恢复成功后整体替换待写队列,修首屏自愈抢跑写空
This commit is contained in:
+26
-4
@@ -282,13 +282,27 @@ export function apply(ctx: Context) {
|
||||
|
||||
const previews = createPreviewFiles()
|
||||
ctx.effect(() => () => previews.clear(), 'dsh-worktable: preview capabilities')
|
||||
/** 诊断日志:状态路由到底有没有被调用、被什么挡住(排查重启丢数据用,稳定后可删) */
|
||||
const stateLog = (line: string) => {
|
||||
void (async () => {
|
||||
try {
|
||||
const fsx = await import('node:fs/promises')
|
||||
const dir = pathResolve(resolveDshHomeSafe(), 'logs')
|
||||
await fsx.mkdir(dir, { recursive: true })
|
||||
await fsx.appendFile(pathResolve(dir, 'worktable-state.log'), new Date().toISOString() + ' ' + line + '\n', 'utf8')
|
||||
} catch {}
|
||||
})()
|
||||
}
|
||||
// Direct WebServer routes do NOT inherit the host's /api authentication route.
|
||||
const register = (route: any, publicAsset = false) => ctx.effect(() => webServer.register({
|
||||
...route,
|
||||
handler: async (req: any, res: any) => {
|
||||
if (!publicAsset && route.path !== HEALTH_PATH) {
|
||||
const rejected = await requestRejection(ctx, req)
|
||||
if (rejected !== undefined) { json(res, rejected, { error: rejectionText(rejected) }); return }
|
||||
if (rejected !== undefined) {
|
||||
if (route.path === '/api/worktable/state') stateLog('REJECT ' + req.method + ' -> ' + rejected)
|
||||
json(res, rejected, { error: rejectionText(rejected) }); return
|
||||
}
|
||||
}
|
||||
return route.handler(req, res)
|
||||
},
|
||||
@@ -561,9 +575,11 @@ export function apply(ctx: Context) {
|
||||
try {
|
||||
const raw = await readFile(file, 'utf8')
|
||||
const text = raw.charCodeAt(0) === 0xFEFF ? raw.slice(1) : raw
|
||||
json(res, 200, { ok: true, path: file, state: JSON.parse(text) })
|
||||
const parsed: any = JSON.parse(text)
|
||||
json(res, 200, { ok: true, path: file, state: parsed })
|
||||
} catch (err: any) {
|
||||
if (err?.code === 'ENOENT') { json(res, 200, { ok: true, path: file, state: null }); return }
|
||||
if (err?.code === 'ENOENT') { stateLog('GET empty (no file)'); json(res, 200, { ok: true, path: file, state: null }); return }
|
||||
stateLog('GET error ' + String(err))
|
||||
json(res, 500, { error: String(err) })
|
||||
}
|
||||
return
|
||||
@@ -572,7 +588,7 @@ export function apply(ctx: Context) {
|
||||
try {
|
||||
const body = await readJsonBody(req)
|
||||
const incoming = body && typeof body.state === 'object' && body.state !== null ? body.state : null
|
||||
if (!incoming) { json(res, 400, { error: 'missing state' }); return }
|
||||
if (!incoming) { stateLog('POST 400 missing state'); json(res, 400, { error: 'missing state' }); return }
|
||||
// 按 key 合并:客户端只带 view 的半截写入不能把 projects 抹掉
|
||||
let previous: any = null
|
||||
try {
|
||||
@@ -583,6 +599,12 @@ export function apply(ctx: Context) {
|
||||
? { ...previous, ...incoming }
|
||||
: incoming
|
||||
const text = JSON.stringify(state)
|
||||
// 只在「项目变少」时记一行:正常保存不写日志,避免文件无限增长
|
||||
const inN = incoming?.projects?.layouts?.length ?? 0
|
||||
const prevN = previous?.projects?.layouts?.length ?? 0
|
||||
if (inN < prevN) {
|
||||
stateLog('WARN projects shrink ' + prevN + ' -> ' + inN + ' (keys=' + Object.keys(incoming).join('+') + ')')
|
||||
}
|
||||
if (text.length > 20 * 1024 * 1024) { json(res, 413, { error: 'state too large' }); return }
|
||||
const fsx = await import('node:fs/promises')
|
||||
await fsx.mkdir(dirname(file), { recursive: true })
|
||||
|
||||
Reference in New Issue
Block a user