Skip to content

Commit

Permalink
Use ctx for checking cancellation of ctx
Browse files Browse the repository at this point in the history
I think #289 introduced some severe flakiness in the ability to just run jobs, since monitor could time out and return context.DeadlineExceeded wrapping errors without the surrounding context actually being cancelled. I think. I'm not sure.

Also I changed my mind on closing the errors channel - reading controller.go, I don't think it needs to be closed at all.
  • Loading branch information
DrJosh9000 committed Apr 4, 2024
1 parent 58a7874 commit 673e729
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions internal/controller/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,13 @@ func (m *Monitor) Start(ctx context.Context, handler JobHandler) <-chan error {
var ok bool
if queue, ok = agentTags["queue"]; !ok {
errs <- errors.New("missing required tag: queue")
close(errs)
return errs
}

go func() {
logger.Info("started")
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
defer close(errs)

first := make(chan struct{}, 1)
first <- struct{}{}
Expand All @@ -136,10 +134,11 @@ func (m *Monitor) Start(ctx context.Context, handler JobHandler) <-chan error {
}

resp, err := m.getScheduledCommandJobs(ctx, queue)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return
}
if err != nil {
// Avoid logging if the context is already closed.
if ctx.Err() != nil {
return
}
logger.Warn("failed to get scheduled command jobs", zap.Error(err))
continue
}
Expand Down Expand Up @@ -168,7 +167,7 @@ func (m *Monitor) Start(ctx context.Context, handler JobHandler) <-chan error {

logger.Debug("creating job", zap.String("uuid", job.Uuid))
if err := handler.Create(ctx, &job.CommandJob); err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
if ctx.Err() != nil {
return
}
logger.Error("failed to create job", zap.Error(err))
Expand Down

0 comments on commit 673e729

Please sign in to comment.