/** * A job's log file: the writer that fills it, and the bounded reader that shows it. * * Both sides are one subject — a log grows without limit, so one side rotates it and the other reads * only a byte tail — and they live together because a start pays for what its entry imports. `#547` * merged them: the writer was a module of its own until job's graph needed one back for the label * comparison the roster now shares. * * The reader sanitizes everything it surfaces (see `format.ts`) and never modifies the file. The * writer owns no job state: every path and limit is a callback over the runtime's live config, and a * buffered line is handed back through `onLine`. The line splitter is a seam rather than an import: * `lib/lines.ts` is loaded with the process seam on the first job, so a session that never starts one * never loads it. */ import { closeSync, createWriteStream, existsSync, openSync, readSync, renameSync, statSync, unlinkSync, type WriteStream, } from "node:fs"; import { sanitizeUntrustedText } from "../../lib/format.ts"; import type { LineSplitter } from "../../lib/lines.ts"; import { tailLines } from "./format.ts"; import type { Job, JobRecord } from "./types.ts"; /** Bytes of log read for a `logs` call. */ const LOG_READ_BYTES = 64 * 1024; /** Drop trailing blank lines so a final newline does not eat a requested line. */ function dropTrailingBlank(lines: string[]): string[] { const copy = [...lines]; while (copy.length > 0 && copy[copy.length - 1] === "") copy.pop(); return copy; } export interface LogTail { /** Sanitized tail lines, newest last. */ lines: string[]; /** True when the file was longer than the byte window. */ truncated: boolean; /** True when earlier lines exist beyond the returned window. */ more: boolean; } /** Read a bounded, sanitized tail of `path`; a missing/unreadable file is empty. */ export function readLogTail(path: string | undefined, lines: number): LogTail { let text = ""; let truncated = false; let start = 0; try { if (path && existsSync(path)) { const size = statSync(path).size; start = Math.max(0, size - LOG_READ_BYTES); truncated = start > 0; const fd = openSync(path, "r"); try { const buffer = Buffer.alloc(size - start); const bytesRead = readSync(fd, buffer, 0, buffer.length, start); text = buffer.subarray(0, bytesRead).toString("utf8"); } finally { closeSync(fd); } } } catch { // A missing/unreadable log yields an empty tail. } const all = dropTrailingBlank(sanitizeUntrustedText(text)); const more = start > 0 || all.length > lines; return { lines: tailLines(all, lines), truncated, more }; } /** Bytes held while a rotation swaps the log stream before writes resume. */ const MAX_ROTATION_QUEUE_BYTES = 2 * 1024 * 1024; /** One job's log state: the live segment, the queue, and the counters. */ export interface LogHandle { stream?: WriteStream; lines: LineSplitter; /** Chunks that arrived while the log stream was being swapped. */ queue: Buffer[]; /** Bytes currently in `queue`. */ queuedBytes: number; /** True between ending the old stream and reopening the log. */ rotating: boolean; /** Bytes dropped because a rotation could not keep up; reported by `logs`. */ droppedBytes: number; /** Bytes written to the live segment, counted exactly as chunks arrive. */ logBytes: number; /** * True when this handle was given a log path, so a chunk with nowhere to go is * lost; a detached job has no stream by design and loses nothing. */ expectsStream: boolean; /** True once the handle is disposed; a rotation in flight never reopens. */ disposed: boolean; } export interface LogWriterOptions { /** The clock the repaint window is measured on. */ now(): number; /** * A fresh line splitter for one job's stream. * * Injected rather than imported: the splitter is the only piece of `lib/lines.ts` * a writer needs, and a runtime that never starts a job must not load it. */ newSplitter(): LineSplitter; /** The live segment for a job, or undefined when it cannot be used. */ logPath(job: JobRecord): string | undefined; /** The rotated segment for a job, or undefined when it cannot be used. */ rotatedPath(job: JobRecord): string | undefined; /** Bytes a segment may reach before it rotates; 0 disables rotation. */ maxBytes(): number; /** The repaint window for output-driven painting. */ repaintMs(): number; /** Record one output line; the runtime sanitizes it onto the job. */ onLine(job: Job, line: string): void; /** Repaint, at most once per repaint window while output arrives. */ onPaint(): void; } export interface LogWriter { /** A handle for one job's log; `logPath` is undefined for a detached job. */ create(logPath: string | undefined): LogHandle; /** Append child output, splitting lines and rotating at the cap. */ write(handle: LogHandle, job: Job, chunk: Buffer): void; /** The buffered final lines, including a final line without a newline. */ flush(handle: LogHandle): string[]; /** Close the stream and mark the handle dead; idempotent. */ dispose(handle: LogHandle, options: { endStream: boolean }): void; } export function createLogWriter(options: LogWriterOptions): LogWriter { /** Last output-driven repaint, so a busy child cannot repaint per chunk. */ let lastPaint = 0; const writeLog = (handle: LogHandle, chunk: Buffer): void => { if (handle.stream === undefined) { // A reopen that failed leaves no stream for the rest of the job's life, so // these bytes are gone: count them, or `logs` presents the short segment as // complete. A detached job's chunks are its own shell's to write. if (handle.expectsStream) handle.droppedBytes += chunk.length; return; } try { handle.stream.write(chunk); } catch { // A closed stream must not break output handling. } // Count what we handed the stream: `WriteStream.bytesWritten` only updates on // its completion callback, which can lag a tick behind the file on disk. if (handle.stream) handle.logBytes += chunk.length; }; /** Buffer a chunk while the log stream is being swapped, dropping the oldest past the cap. */ const enqueue = (handle: LogHandle, chunk: Buffer, front = false): void => { if (front) handle.queue.unshift(chunk); else handle.queue.push(chunk); handle.queuedBytes += chunk.length; while (handle.queuedBytes > MAX_ROTATION_QUEUE_BYTES && handle.queue.length > 1) { const dropped = handle.queue.shift(); if (!dropped) break; handle.queuedBytes -= dropped.length; handle.droppedBytes += dropped.length; } }; /** * Move the old segment aside and open a fresh one, then drain the backlog. * * Runs from the old stream's `close`, and directly when that stream has already * closed: a stream that errored emitted `close` before the rotation armed itself, and * a rotation waiting on an event that already happened would leave the handle rotating * for good, queueing every later chunk and dropping the oldest past the cap. * * The reopen opens its descriptor here, not on a later tick: a deferred open lands after * a `clear` that already unlinked both segments, recreating the log of a job that is * gone. Opening it in this call leaves nothing pending once it returns, so the unlink is * final. */ const reopenSegment = (handle: LogHandle, job: Job, logPath: string, rotated: string): void => { // A killed or cleared job disposes its handle; never reopen for it. if (handle.disposed) return; try { if (existsSync(rotated)) unlinkSync(rotated); renameSync(logPath, rotated); } catch { // A failed rename leaves the oversized segment in place; keep appending. } let fd: number | undefined; let reopened: WriteStream | undefined; try { fd = openSync(logPath, "a"); reopened = createWriteStream(logPath, { fd }); reopened.on("error", () => {}); } catch { // A stream event must not carry a throw into the child's output path: an // unwritable log leaves the job running with no stream, and the drain // below still empties the queue. That promise covers the cleanup too, so // `closeSync` is guarded — its own throw (EINTR, EIO) would surface from // the close listener as an uncaught exception and take the session down. if (fd !== undefined) { try { closeSync(fd); } catch { // The descriptor is the OS's to reclaim; the log is unwritable anyway. } } } handle.stream = reopened; handle.rotating = false; handle.logBytes = 0; drain(handle, job); }; /** * Move the live segment aside, queueing `chunk` ahead of the backlog, and reopen. * * Returns false when no rotation can start (no stream, or a record whose log * paths do not validate), so the caller writes the chunk instead of stranding it * in a queue nothing will drain. * * The old stream is ended and its `close` awaited before the rename, because * Windows refuses to rename an open file; chunks that arrive in that window * are queued rather than written, so nothing lands in the renamed inode. */ const rotateLog = (handle: LogHandle, job: Job, chunk: Buffer): boolean => { const stream = handle.stream; const logPath = options.logPath(job); const rotated = options.rotatedPath(job); if (!stream || !logPath || !rotated || handle.rotating) return false; // This chunk precedes whatever queued while the previous segment was closing. enqueue(handle, chunk, true); handle.rotating = true; if (stream.closed) { reopenSegment(handle, job, logPath, rotated); return true; } stream.once("close", () => reopenSegment(handle, job, logPath, rotated)); try { stream.end(); } catch { // Already ended. } return true; }; /** Write one output chunk, rotating the segment first when it reached the cap. */ const writeChunk = (handle: LogHandle, job: Job, chunk: Buffer): void => { if (handle.rotating) { // Later than everything already queued. enqueue(handle, chunk); return; } const cap = options.maxBytes(); if (handle.stream && cap > 0 && handle.logBytes >= cap && rotateLog(handle, job, chunk)) return; writeLog(handle, chunk); }; /** * Replay the queue through `writeChunk` after a rotation, so a backlog that is * itself larger than the cap rotates again instead of filling one segment. */ const drain = (handle: LogHandle, job: Job): void => { while (!handle.rotating && handle.queue.length > 0) { const chunk = handle.queue.shift(); if (!chunk) break; handle.queuedBytes -= chunk.length; writeChunk(handle, job, chunk); } }; return { create(logPath) { const stream = logPath === undefined ? undefined : createWriteStream(logPath, { flags: "a" }); stream?.on("error", () => {}); return { stream, lines: options.newSplitter(), queue: [], queuedBytes: 0, rotating: false, droppedBytes: 0, logBytes: 0, expectsStream: logPath !== undefined, disposed: false, }; }, write(handle, job, chunk) { if (handle.disposed) return; writeChunk(handle, job, chunk); for (const line of handle.lines.push(chunk)) options.onLine(job, line); const time = options.now(); if (time - lastPaint >= options.repaintMs()) { lastPaint = time; options.onPaint(); } }, flush(handle) { return handle.lines.flush(); }, dispose(handle, { endStream }) { if (handle.disposed) return; handle.disposed = true; // A rotation in flight checks `disposed` before reopening, so dropping the // queue here cannot leak a stream for a dead job. handle.queue = []; handle.queuedBytes = 0; if (endStream) { try { handle.stream?.end(); } catch { // Already closed. } } handle.stream = undefined; }, }; }