File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -42,8 +42,10 @@ type Runner[TaskType Task] struct {
42
42
ctx context.Context
43
43
cancel context.CancelFunc
44
44
45
- running sync.WaitGroup
46
- workers * hashMap
45
+ running sync.WaitGroup
46
+ workers * hashMap
47
+ workerErrs []error
48
+ workerErrMut sync.RWMutex
47
49
}
48
50
49
51
// Internal types used to implement the Runner.
@@ -163,7 +165,12 @@ func (s *Runner[TaskType]) ApplyTasks(ctx context.Context, tt []TaskType) error
163
165
go func () {
164
166
defer s .running .Done ()
165
167
defer close (newWorker .Exited )
166
- newWorker .Worker .Run (workerCtx )
168
+ // Gather error encountered by worker when running
169
+ if err := newWorker .Worker .Run (workerCtx ); err != nil {
170
+ s .workerErrMut .Lock ()
171
+ s .workerErrs = append (s .workerErrs , err )
172
+ s .workerErrMut .Unlock ()
173
+ }
167
174
}()
168
175
169
176
_ = s .workers .Add (newTask )
@@ -203,3 +210,10 @@ func (s *Runner[TaskType]) Stop() {
203
210
s .cancel ()
204
211
s .running .Wait ()
205
212
}
213
+
214
+ // GetWorkerErrors returns errors encountered by workers when they run assigned tasks
215
+ func (s * Runner [TaskType ]) GetWorkerErrors () []error {
216
+ s .workerErrMut .RLock ()
217
+ defer s .workerErrMut .RUnlock ()
218
+ return s .workerErrs
219
+ }
You can’t perform that action at this time.
0 commit comments