Documentation
¶
Overview ¶
Package logstreamer implements the log-chunk publisher for sqi-worker.
A Publisher implements [executor.OutputHandler] and accumulates process output lines from stdout and stderr into protocol.LogChunkMsg batches, publishing them to the task.logs.<workerID>.<taskID> NATS JetStream subject.
Chunking ¶
Lines are buffered per-stream (stdout / stderr) per task attempt. A chunk is flushed immediately when:
- the buffered line count reaches Config.MaxLinesPerChunk, or
- the accumulated byte count reaches Config.MaxBytesPerChunk.
A background per-attempt goroutine also flushes buffered content at Config.FlushInterval intervals so the web UI log viewer feels live even for slowly printing processes.
Sequence numbers ¶
Each published chunk carries a monotonic protocol.LogChunkMsg.SeqNum starting at 1 for the first chunk of an attempt. Seq numbers increment across both stdout and stderr chunks so the server can replay all chunks in publication order regardless of NATS delivery order.
Delivery ¶
A flushed chunk is encoded and queued for delivery; it is never published inline, so a slow or broken broker does not hold up the process's output. One sender goroutine delivers the queue through a Transport, putting up to 32 chunks on the wire in order before it waits for their acks, so a worker far from the broker is not held to one chunk per round trip. It tries each chunk again, with backoff, until the log stream stores it or the failure is one no retry can cure, such as a chunk over the broker's payload limit or a closed connection. Every try of a chunk carries the message ID "<attemptID>/<seq>", so the stream drops a retry it already stored, and the server keeps one copy per seq. The queue holds at most 32 MiB of encoded chunks; past that the oldest are dropped, with a warning at most once a minute.
Lifecycle ¶
Call Publisher.FlushLogs after the process exits to drain all remaining buffered lines and stop the background flush goroutine. FlushLogs then waits up to 5 s for the attempt's chunks to be stored, so in the normal case all log output reaches the server before the terminal status message is published; a chunk still unstored after the wait keeps being retried and lands after it. At worker shutdown Publisher.Flush delivers what is still queued, for as long as its context allows; Publisher.Abandon drops it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AckWait ¶ added in v0.4.0
AckWait waits for the stream's ack of one sent chunk: nil once the stream stored it, or the reason it did not, or ctx's error when ctx ends first. An outcome already in when it is called is returned even if ctx has ended.
type Config ¶
type Config struct {
// MaxLinesPerChunk is the maximum number of lines batched into a single
// NATS message. A flush is triggered immediately when the buffer reaches
// this count. Default: 50.
MaxLinesPerChunk int
// MaxBytesPerChunk is the maximum total byte count of line content in a
// single NATS message. A flush is triggered when the accumulated bytes
// would meet or exceed this limit after adding a line. Default: 16384.
MaxBytesPerChunk int
// FlushInterval is the maximum time a line may sit in the buffer before
// being flushed regardless of chunk thresholds. Keeps the web UI log
// viewer feeling live. Default: 500 ms.
FlushInterval time.Duration
// Now stamps each chunk's At; nil is time.Now.
Now func() time.Time
// contains filtered or unexported fields
}
Config holds the tunable parameters for the log-chunk Publisher. Zero or negative values are replaced by the package defaults when New is called.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config populated with production-sensible defaults.
type Publisher ¶
type Publisher struct {
// contains filtered or unexported fields
}
Publisher implements executor.OutputHandler, accumulating process output lines from concurrent task goroutines into batched protocol.LogChunkMsg messages delivered to NATS JetStream.
Publisher is safe for concurrent use from multiple goroutines.
func New ¶
New returns a ready-to-use Publisher publishing as workerID through t. Any zero or negative fields in cfg are replaced with defaults before use.
func (*Publisher) Abandon ¶ added in v0.4.0
Abandon stops delivery for good without waiting on the broker and returns how many queued chunks it dropped. It is the crash path: a crashed worker's unsent lines are lost.
func (*Publisher) Flush ¶ added in v0.4.0
Flush delivers the chunks still queued until none is left or ctx ends, then stops delivery for good and returns how many chunks it dropped, logging them. It is the worker's shutdown call: it must run while the connection is open.
func (*Publisher) FlushLogs ¶
FlushLogs implements the executor.LogFlusher interface.
It signals the background flush goroutine to stop, waits for it to exit, then performs a final synchronous drain of any remaining buffered lines for both stdout and stderr. FlushLogs must be called after the task process has exited and before the terminal status message is published. It waits up to the flush wait (5 s) for the attempt's chunks to be stored, so in the normal case all log output reaches the server first.
Safety: the executor calls FlushLogs only after readWg.Wait() completes (all scanOutput goroutines — and thus all HandleLine calls — have returned). Because threshold-triggered flushes are synchronous inside HandleLine, there are no in-flight flushes for this attempt when FlushLogs runs. The only concurrent publisher is the background flushLoop goroutine, which is stopped via close(buf.done) + wg.Wait() before the final drain.
FlushLogs is idempotent: if no lines were ever received for the given attempt, or if FlushLogs has already been called, it returns nil immediately.
func (*Publisher) HandleLine ¶
HandleLine implements executor.OutputHandler.
It appends line to the in-memory buffer for (taskID, attemptID, stream). When the buffer meets the MaxLinesPerChunk or MaxBytesPerChunk threshold, a chunk is flushed immediately: HandleLine queues it for delivery before it returns, without waiting on the broker. The per-attempt background goroutine handles the periodic FlushInterval drain between threshold events.
Concurrency note: threshold-triggered flushes happen synchronously inside HandleLine, so when all HandleLine callers return (i.e., when the executor's readWg.Wait() completes after process exit), no threshold flush is in flight. This is why [FlushLogs] can safely drain the remaining buffer without a concurrent write racing against it.
type Transport ¶ added in v0.4.0
type Transport interface {
// Publish sends data to subj with msgID as its Nats-Msg-Id without
// waiting for the stream, and returns the wait for its ack. Chunks sent
// one after another reach the stream in that order. An error means the
// chunk was not sent.
Publish(subj string, data []byte, msgID string) (AckWait, error)
}
Transport sends encoded log chunks and reports whether the log stream stored each one.