fix(stats): kept worker pool for one-file syncs
Keyed the serial-parser branch off the caller-requested worker count instead of the per-pool clamp, so a one-file sync still fans out to workers when the caller (or default) asked for more than one. Fixes #3733
This commit is contained in:
@@ -241,14 +241,16 @@ export async function syncAllSessions(opts?: SyncOptions): Promise<{ processed:
|
||||
report(sessionFile);
|
||||
};
|
||||
|
||||
const poolSize = Math.max(1, Math.min(files.length, opts?.workers ?? defaultWorkerCount()));
|
||||
if (poolSize === 1) {
|
||||
const requestedWorkers = Math.max(1, Math.floor(opts?.workers ?? defaultWorkerCount()));
|
||||
if (requestedWorkers === 1) {
|
||||
for (const sessionFile of files) {
|
||||
await processFile(sessionFile, parseSessionFile);
|
||||
}
|
||||
return { processed: totalProcessed, files: filesProcessed };
|
||||
}
|
||||
|
||||
const poolSize = Math.min(files.length, requestedWorkers);
|
||||
|
||||
const handles: WorkerHandle[] = [];
|
||||
for (let i = 0; i < poolSize; i++) handles.push(spawnWorker());
|
||||
|
||||
|
||||
@@ -88,4 +88,15 @@ describe("stats sync serial mode", () => {
|
||||
expect(overall.totalRequests).toBe(1);
|
||||
expect(workerSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("spawns a worker pool when callers explicitly request workers: 2 with a single file", async () => {
|
||||
await writeSessionFile();
|
||||
const workerProbe = new Error("worker probe");
|
||||
const workerSpy = vi.spyOn(globalThis, "Worker").mockImplementation(() => {
|
||||
throw workerProbe;
|
||||
});
|
||||
|
||||
await expect(syncAllSessions({ workers: 2 })).rejects.toBe(workerProbe);
|
||||
expect(workerSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user