fix(team): clean corrupt task projection caches
This commit is contained in:
parent
e6d2a0baee
commit
9ad258272d
2 changed files with 60 additions and 2 deletions
|
|
@ -838,18 +838,23 @@ async function readPersistentTaskProjectionCache(
|
||||||
const parsed = JSON.parse(raw) as unknown;
|
const parsed = JSON.parse(raw) as unknown;
|
||||||
if (!isRecord(parsed)) {
|
if (!isRecord(parsed)) {
|
||||||
taskDiag.persistentCacheReadFailures++;
|
taskDiag.persistentCacheReadFailures++;
|
||||||
|
await fs.promises.rm(cachePath, { force: true }).catch(() => undefined);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
parsed.version !== PERSISTENT_TASK_PROJECTION_CACHE_VERSION ||
|
parsed.version !== PERSISTENT_TASK_PROJECTION_CACHE_VERSION ||
|
||||||
parsed.tasksBase !== payload.tasksBase ||
|
parsed.tasksBase !== payload.tasksBase ||
|
||||||
parsed.teamName !== teamName ||
|
parsed.teamName !== teamName ||
|
||||||
parsed.optionKey !== optionKey ||
|
parsed.optionKey !== optionKey
|
||||||
!isRecord(parsed.entries)
|
|
||||||
) {
|
) {
|
||||||
taskDiag.persistentCacheMisses++;
|
taskDiag.persistentCacheMisses++;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (!isRecord(parsed.entries)) {
|
||||||
|
taskDiag.persistentCacheReadFailures++;
|
||||||
|
await fs.promises.rm(cachePath, { force: true }).catch(() => undefined);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const entries = new Map<string, PersistentTaskProjectionCacheEntry>();
|
const entries = new Map<string, PersistentTaskProjectionCacheEntry>();
|
||||||
for (const [file, entry] of Object.entries(parsed.entries)) {
|
for (const [file, entry] of Object.entries(parsed.entries)) {
|
||||||
|
|
@ -866,6 +871,7 @@ async function readPersistentTaskProjectionCache(
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
taskDiag.persistentCacheReadFailures++;
|
taskDiag.persistentCacheReadFailures++;
|
||||||
|
await fs.promises.rm(cachePath, { force: true }).catch(() => undefined);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -757,6 +757,58 @@ describe('team-fs-worker integration', () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('removes corrupt persisted task projection caches even when a team has no tasks', async () => {
|
||||||
|
const workerPath = await getWorkerPath();
|
||||||
|
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'team-fs-worker-'));
|
||||||
|
const tasksBase = path.join(tempDir, 'tasks');
|
||||||
|
const projectionCacheBase = path.join(tempDir, 'projection-cache');
|
||||||
|
const teamName = 'empty-corrupt-persistent-cache-team';
|
||||||
|
const tasksDir = path.join(tasksBase, teamName);
|
||||||
|
await fs.mkdir(tasksDir, { recursive: true });
|
||||||
|
|
||||||
|
const seedWorker = createWorker(workerPath);
|
||||||
|
try {
|
||||||
|
const seed = await callGetAllTasks(seedWorker, tasksBase, projectionCacheBase);
|
||||||
|
expect(seed.tasks).toHaveLength(0);
|
||||||
|
} finally {
|
||||||
|
await seedWorker.terminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
await fs.writeFile(path.join(tasksDir, '1.json'), JSON.stringify({ id: '1' }), 'utf8');
|
||||||
|
const writerWorker = createWorker(workerPath);
|
||||||
|
try {
|
||||||
|
const written = await callGetAllTasks(writerWorker, tasksBase, projectionCacheBase);
|
||||||
|
expect(written.tasks[0]).toMatchObject({ teamName });
|
||||||
|
expect(written.diag?.persistentCacheWrites).toBe(1);
|
||||||
|
} finally {
|
||||||
|
await writerWorker.terminate();
|
||||||
|
}
|
||||||
|
await fs.rm(path.join(tasksDir, '1.json'));
|
||||||
|
|
||||||
|
const cacheFiles = await fs.readdir(path.join(projectionCacheBase, 'v1'));
|
||||||
|
const cachePath = path.join(projectionCacheBase, 'v1', cacheFiles[0]);
|
||||||
|
await fs.writeFile(cachePath, '{bad json', 'utf8');
|
||||||
|
|
||||||
|
const firstWorker = createWorker(workerPath);
|
||||||
|
try {
|
||||||
|
const first = await callGetAllTasks(firstWorker, tasksBase, projectionCacheBase);
|
||||||
|
expect(first.tasks).toHaveLength(0);
|
||||||
|
expect(first.diag?.persistentCacheReadFailures).toBe(1);
|
||||||
|
await expect(fs.stat(cachePath)).rejects.toMatchObject({ code: 'ENOENT' });
|
||||||
|
} finally {
|
||||||
|
await firstWorker.terminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
const secondWorker = createWorker(workerPath);
|
||||||
|
try {
|
||||||
|
const second = await callGetAllTasks(secondWorker, tasksBase, projectionCacheBase);
|
||||||
|
expect(second.tasks).toHaveLength(0);
|
||||||
|
expect(second.diag?.persistentCacheReadFailures).toBe(0);
|
||||||
|
} finally {
|
||||||
|
await secondWorker.terminate();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('rejects persisted task projections that contain deleted tasks as task records', async () => {
|
it('rejects persisted task projections that contain deleted tasks as task records', async () => {
|
||||||
const workerPath = await getWorkerPath();
|
const workerPath = await getWorkerPath();
|
||||||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'team-fs-worker-'));
|
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'team-fs-worker-'));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue