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

View File

@ -78,6 +78,7 @@ type deploymentLogWriter struct {
lineBuffer bytes.Buffer // buffer for incomplete lines lineBuffer bytes.Buffer // buffer for incomplete lines
mu sync.Mutex mu sync.Mutex
done chan struct{} done chan struct{}
flushed sync.WaitGroup // waits for flush goroutine to finish
flushCtx context.Context //nolint:containedctx // needed for async flush goroutine 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{}), done: make(chan struct{}),
flushCtx: ctx, flushCtx: ctx,
} }
w.flushed.Add(1)
go w.runFlushLoop() go w.runFlushLoop()
return w return w
@ -128,12 +130,15 @@ func (w *deploymentLogWriter) Write(p []byte) (int, error) {
return len(p), nil 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() { func (w *deploymentLogWriter) Close() {
close(w.done) close(w.done)
w.flushed.Wait()
} }
func (w *deploymentLogWriter) runFlushLoop() { func (w *deploymentLogWriter) runFlushLoop() {
defer w.flushed.Done()
ticker := time.NewTicker(logFlushInterval) ticker := time.NewTicker(logFlushInterval)
defer ticker.Stop() defer ticker.Stop()