Wait for final log flush before closing deploymentLogWriter (closes #4) #9

Merged
sneak merged 1 commits from :fix/issue-4 into main 2026-02-16 06:29:18 +01:00
Showing only changes of commit 69456abd25 - Show all commits

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()