From bd2f103743c92900bb2ff46f6e6b28038f78afc0 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Tue, 6 Jun 2023 16:40:27 +0300 Subject: [PATCH] Wait for addPendingInstances to finish Signed-off-by: Gabriel Adrian Samfira --- runner/pool/pool.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/runner/pool/pool.go b/runner/pool/pool.go index 4764e0fe..082ae3a7 100644 --- a/runner/pool/pool.go +++ b/runner/pool/pool.go @@ -1068,7 +1068,7 @@ func (r *basePoolManager) addPendingInstances() { log.Printf("failed to fetch instances from store: %s", err) return } - + g, _ := errgroup.WithContext(r.ctx) for _, instance := range instances { if instance.Status != providerCommon.InstancePendingCreate { // not in pending_create status. Skip. @@ -1082,7 +1082,8 @@ func (r *basePoolManager) addPendingInstances() { // when the loop runs again and we end up with multiple instances. continue } - go func(instance params.Instance) { + instance := instance + g.Go(func() error { log.Printf("creating instance %s in pool %s", instance.Name, instance.PoolID) if err := r.addInstanceToProvider(instance); err != nil { log.Printf("failed to add instance to provider: %s", err) @@ -1092,7 +1093,11 @@ func (r *basePoolManager) addPendingInstances() { } log.Printf("failed to create instance in provider: %s", err) } - }(instance) + return nil + }) + } + if err := g.Wait(); err != nil { + log.Printf("failed to add pending instances: %s", err) } }