Skip to content

Commit f04f45c

Browse files
committed
agent: worker.Subscribe: start at most one log stream per task
Track task IDs for which a subscription has already started a log stream. A task may be present in the initial snapshot and also be delivered by the watcher after it is registered. Use a shared helper for both paths to ensure that only one log stream is started for each task. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 3af02d0 commit f04f45c

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

agent/worker.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,24 @@ func (w *worker) Subscribe(ctx context.Context, subscription *api.SubscriptionMe
641641
}
642642
w.mu.RUnlock()
643643
var wg sync.WaitGroup
644-
for _, tm := range taskManagers {
644+
645+
// A task may be present in the initial snapshot and also be delivered by
646+
// the watcher. Start at most one log stream per task for this subscription.
647+
started := make(map[string]struct{})
648+
startLogs := func(tm *taskManager) {
649+
taskID := tm.task.ID
650+
if _, ok := started[taskID]; ok {
651+
return
652+
}
653+
started[taskID] = struct{}{}
654+
645655
wg.Go(func() {
646656
tm.Logs(ctx, options, publisher)
647657
})
648658
}
659+
for _, tm := range taskManagers {
660+
startLogs(tm)
661+
}
649662

650663
// In follow mode, watch for new matching tasks until the subscription
651664
// context is cancelled.
@@ -660,9 +673,7 @@ func (w *worker) Subscribe(ctx context.Context, subscription *api.SubscriptionMe
660673
continue
661674
}
662675

663-
wg.Go(func() {
664-
tm.Logs(ctx, options, publisher)
665-
})
676+
startLogs(tm)
666677
}
667678
}
668679

0 commit comments

Comments
 (0)