diff --git a/internal/service/deploy/deploy.go b/internal/service/deploy/deploy.go index 1c3f191..a98cf0e 100644 --- a/internal/service/deploy/deploy.go +++ b/internal/service/deploy/deploy.go @@ -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()