diff --git a/lib/db.ts b/lib/db.ts index 32aee1b..55d7bb9 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -12,6 +12,21 @@ const globalForPrisma = globalThis as unknown as { const SLOW_QUERY_MS = Number(process.env.DB_SLOW_QUERY_MS ?? "1000"); +// Docker 파이프 환경에서 stdout fd는 non-blocking이라 writeSync가 부분 쓰기를 +// 하거나 EAGAIN을 던질 수 있다. 전부 쓸 때까지 반복해 로그 유실을 막는다. +function writeAll(fd: number, text: string): void { + const buf = Buffer.from(text, "utf8"); + let offset = 0; + while (offset < buf.length) { + try { + offset += writeSync(fd, buf, offset, buf.length - offset); + } catch (err) { + if ((err as NodeJS.ErrnoException).code === "EAGAIN") continue; + throw err; + } + } +} + // ── SQL 로깅 ($on) ──────────────────────────────────────────────── function inlineParams(query: string, rawParams: string): string { @@ -49,7 +64,7 @@ function onQuery(e: Prisma.QueryEvent): void { .join("\n"); const out = `${header}\n${indented}\n`; - writeSync(isSlow ? 2 : 1, out); + writeAll(isSlow ? 2 : 1, out); } // ── 결과 로깅 ($extends) ────────────────────────────────────────── @@ -196,7 +211,7 @@ function logResult( if (!logger.isEnabled("debug")) return; const ts = new Date().toISOString(); const body = buildResultLog(model, operation, result); - writeSync(1, `[${ts}] [DEBUG] [db:result] ${body}\n`); + writeAll(1, `[${ts}] [DEBUG] [db:result] ${body}\n`); } // ── PrismaClient 생성 ─────────────────────────────────────────────