Skip to content

Commit

Permalink
Merge pull request #429 from buildkite/add-waitgroup-around-jobhandle…
Browse files Browse the repository at this point in the history
…rworker

Add waitgroup around jobHandlerWorker
  • Loading branch information
DrJosh9000 authored Nov 19, 2024
2 parents 0591ab8 + 36ce037 commit 09c2a28
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/controller/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math/rand/v2"
"reflect"
"sync"
"time"

"github.com/Khan/genqlient/graphql"
Expand Down Expand Up @@ -208,8 +209,13 @@ func (m *Monitor) passJobsToNextHandler(ctx context.Context, logger *zap.Logger,
jobsCh := make(chan *api.JobJobTypeCommand)
defer close(jobsCh)

var wg sync.WaitGroup
for range min(m.cfg.JobCreationConcurrency, len(jobs)) {
go jobHandlerWorker(ctx, staleCtx, logger, handler, agentTags, jobsCh)
wg.Add(1)
go func() {
defer wg.Done()
jobHandlerWorker(ctx, staleCtx, logger, handler, agentTags, jobsCh)
}()
}

for _, job := range jobs {
Expand All @@ -221,6 +227,8 @@ func (m *Monitor) passJobsToNextHandler(ctx context.Context, logger *zap.Logger,
case jobsCh <- job:
}
}

wg.Wait()
}

func jobHandlerWorker(ctx, staleCtx context.Context, logger *zap.Logger, handler model.JobHandler, agentTags map[string]string, jobsCh <-chan *api.JobJobTypeCommand) {
Expand Down

0 comments on commit 09c2a28

Please sign in to comment.