Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions internal/nonkube/controller/collector_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type CollectorLifecycleHandler struct {
logger *slog.Logger
running bool
mux sync.Mutex
startMethod func()
startMethod func(ctx context.Context)
}

func (c *CollectorLifecycleHandler) Start(stopCh <-chan struct{}) {
Expand All @@ -40,17 +40,18 @@ func (c *CollectorLifecycleHandler) Start(stopCh <-chan struct{}) {
if c.startMethod == nil {
c.startMethod = c.startCollectorLite
}
go c.startMethod()
go c.handleShutdown(stopCh)
ctx := c.ctx
go c.startMethod(ctx)
go c.handleShutdown(ctx, stopCh)
}

func (c *CollectorLifecycleHandler) handleShutdown(stopCh <-chan struct{}) {
func (c *CollectorLifecycleHandler) handleShutdown(ctx context.Context, stopCh <-chan struct{}) {
const stopMsg = "Stopping collector lifecycle handler"
select {
case <-stopCh:
c.logger.Info(stopMsg)
c.Stop()
case <-c.ctx.Done():
case <-ctx.Done():
c.logger.Info(stopMsg)
}
}
Expand All @@ -72,9 +73,9 @@ func (c *CollectorLifecycleHandler) Id() string {
return "collector.handler"
}

func (c *CollectorLifecycleHandler) startCollectorLite() {
func (c *CollectorLifecycleHandler) startCollectorLite(ctx context.Context) {
c.logger.Info("Starting collector lite")
err := flow.StartCollector(c.ctx, c.namespace)
err := flow.StartCollector(ctx, c.namespace)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if err != nil {
c.logger.Error("error starting collector", slog.Any("error", err.Error()))
return
Expand Down
5 changes: 3 additions & 2 deletions internal/nonkube/controller/collector_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"context"
"testing"
"time"

Expand All @@ -13,10 +14,10 @@ func TestCollectorHandler(t *testing.T) {
stopCh := make(chan struct{})

ch := NewCollectorLifecycleHandler("sample-namespace")
fakeStart := func() {
fakeStart := func(ctx context.Context) {
close(started)
go func() {
<-ch.ctx.Done()
<-ctx.Done()
close(done)
}()
}
Expand Down