From b17cb6dd63cf03d1a4357a78bb90abe862efa7fd Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Mon, 25 Sep 2023 22:30:34 +0300 Subject: [PATCH] removing the wg and log the errors in the routine --- pkg/perf/monitor.go | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/pkg/perf/monitor.go b/pkg/perf/monitor.go index f4d7c71ca..4d7c40318 100644 --- a/pkg/perf/monitor.go +++ b/pkg/perf/monitor.go @@ -2,11 +2,11 @@ package perf import ( "context" - "sync" "time" "github.com/go-co-op/gocron" "github.com/gomodule/redigo/redis" + "github.com/rs/zerolog/log" "github.com/pkg/errors" "github.com/threefoldtech/zos/pkg/utils" @@ -61,45 +61,28 @@ func (pm *PerformanceMonitor) runTask(ctx context.Context, task Task) error { // Run adds the tasks to the corn queue and start the scheduler func (pm *PerformanceMonitor) Run(ctx context.Context) error { - var wg sync.WaitGroup - errChan := make(chan error, len(pm.tasks)) - for _, task := range pm.tasks { - _, err := pm.scheduler.CronWithSeconds(task.Cron()).Do(func() error { + if _, err := pm.scheduler.CronWithSeconds(task.Cron()).Do(func() error { return pm.runTask(ctx, task) - }) - if err != nil { + }); err != nil { return errors.Wrapf(err, "failed to schedule the task: %s", task.ID()) } - wg.Add(1) go func(task Task) { - var routineErr error - defer wg.Done() ok, err := pm.exists(task.ID()) if err != nil { - routineErr = errors.Wrapf(err, "failed to find key %s", task.ID()) + log.Error().Err(err).Msgf("failed to find key %s", task.ID()) } if !ok { if err := pm.runTask(ctx, task); err != nil { - routineErr = errors.Wrapf(err, "failed to run task: %s", task.ID()) + log.Error().Err(err).Msgf("failed to run task: %s", task.ID()) } } - errChan <- routineErr }(task) } - wg.Wait() - close(errChan) - - for err := range errChan { - if err != nil { - return err - } - } - pm.scheduler.StartAsync() return nil }