perf(server): write the file log in batches off the event loop

Every log line ran existsSync, statSync and appendFileSync on the event
loop before it returned. On a slow data volume (an SMB or NFS bind mount
on a NAS) that stalled every request that logged, and rotation renamed
files without checking whether another process sharing data/logs had
rotated already.

The file half of the logger is now BufferedLogFile: a line is queued in
memory and the queue goes out with one appendFile per batch, through a
single serial chain, so no append runs while files are being renamed. The
files and the rotation are the same as before (trek.log rotates to .1 at
10 MB, five files in total), but the size is re-read before a rotation
and a file another process already rotated is left alone. A stalled disk
costs dropped lines with a count instead of unbounded memory, a failed
write is reported instead of thrown, and index.ts writes whatever is
still queued synchronously when the process exits.
This commit is contained in:
Maurice
2026-10-08 15:18:03 +02:00
parent ba1504d6e9
commit a82cb3adbb
6 changed files with 464 additions and 126 deletions
+6
View File
@@ -18,6 +18,7 @@ import type { DatabaseLifecycle } from './nest/database/database-lifecycle.servi
import { getAppUrl, getMcpSafeUrl, readEnv } from './app-config';
import { resolveDataPaths } from './app-config/data-paths';
import { resolveDbPath } from './db/db-path';
import { flushLogFileSync } from './nest/audit/audit-log.logger';
// data/tmp is the driver-agnostic global scratch dir (restore-upload spool,
// mirror stream staging) and stays boot-created here. Driver-owned roots — the
@@ -206,5 +207,10 @@ function shutdown(signal: string): void {
});
}
// trek.log is written in batches off the event loop. Whatever is still queued
// when the process ends, by any of the exits above or below, goes out
// synchronously here: an 'exit' listener cannot wait for a promise.
process.on('exit', () => flushLogFileSync());
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));