Merge pull request 'Wait for final log flush before closing deploymentLogWriter (closes #4)' (#9) from clawbot/upaas:fix/issue-4 into main

Reviewed-on: #9
This commit is contained in:
Jeffrey Paul 2026-02-16 06:29:18 +01:00
commit 97ee1e212f

View File

@ -78,6 +78,7 @@ type deploymentLogWriter struct {
lineBuffer bytes.Buffer // buffer for incomplete lines
mu sync.Mutex
done chan struct{}
flushed sync.WaitGroup // waits for flush goroutine to finish
flushCtx context.Context //nolint:containedctx // needed for async flush goroutine
}
@ -87,6 +88,7 @@ func newDeploymentLogWriter(ctx context.Context, deployment *models.Deployment)
done: make(chan struct{}),
flushCtx: ctx,
}
w.flushed.Add(1)
go w.runFlushLoop()
return w
@ -128,12 +130,15 @@ func (w *deploymentLogWriter) Write(p []byte) (int, error) {
return len(p), nil
}
// Close stops the flush loop and performs a final flush.
// Close stops the flush loop, waits for the final flush to complete.
func (w *deploymentLogWriter) Close() {
close(w.done)
w.flushed.Wait()
}
func (w *deploymentLogWriter) runFlushLoop() {
defer w.flushed.Done()
ticker := time.NewTicker(logFlushInterval)
defer ticker.Stop()