diff --git a/dkron/run.go b/dkron/run.go index 5ff638239..ddd22752e 100644 --- a/dkron/run.go +++ b/dkron/run.go @@ -2,6 +2,7 @@ package dkron import ( "fmt" + "sync" "github.com/sirupsen/logrus" ) @@ -38,9 +39,13 @@ func (a *Agent) Run(jobName string, ex *Execution) (*Job, error) { } log.WithField("nodes", filterMap).Debug("agent: Filtered nodes to run") + + var wg sync.WaitGroup for _, v := range filterMap { // Call here client GRPC AgentRun - go func(node string) { + wg.Add(1) + go func(node string, wg *sync.WaitGroup) { + defer wg.Done() log.WithFields(logrus.Fields{ "job_name": job.Name, "node": node, @@ -53,8 +58,9 @@ func (a *Agent) Run(jobName string, ex *Execution) (*Job, error) { "node": node, }).Error("agent: Error calling AgentRun") } - }(v) + }(v, &wg) } + wg.Wait() return job, nil }