Skip to content

Commit

Permalink
removing the wg and log the errors in the routine
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Sep 25, 2023
1 parent fef5daf commit b17cb6d
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions pkg/perf/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit b17cb6d

Please sign in to comment.