Skip to content

Commit

Permalink
job: handle ctx cancellation in complete function
Browse files Browse the repository at this point in the history
Currently, the termination of an observer job due to context
cancellation is logged as error. This might be confusing and lead
to unnecessary noise (e.g. in case of graceful shutdown).

The next function already handles the context cancellation differently
too.

Therefore, this commit only logs the error, if it's not of type
`context.Canceled`.

Signed-off-by: Marco Hofstetter <[email protected]>
  • Loading branch information
mhofstetter authored and joamaki committed Nov 4, 2024
1 parent d66ad09 commit a606391
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions job/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"sync"
"time"

"github.com/cilium/stream"

"github.com/cilium/hive"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/internal"
"github.com/cilium/stream"
)

// Observer jobs invoke the given `fn` for each item observed on `observable`.
Expand Down Expand Up @@ -75,9 +76,7 @@ func (jo *jobObserver[T]) start(ctx context.Context, wg *sync.WaitGroup, health

done := make(chan struct{})

var (
err error
)
var err error
jo.observable.Observe(ctx, func(t T) {
start := time.Now()
err := jo.fn(ctx, t)
Expand Down Expand Up @@ -122,7 +121,7 @@ func (jo *jobObserver[T]) start(ctx context.Context, wg *sync.WaitGroup, health
<-done

jo.health.Stopped("observer job done")
if err != nil {
if err != nil && !errors.Is(err, context.Canceled) {
l.Error("Observer job stopped with an error", "error", err)
} else {
l.Debug("Observer job stopped")
Expand Down

0 comments on commit a606391

Please sign in to comment.