diff --git a/pkg/github/jobs/synchronizer.go b/pkg/github/jobs/synchronizer.go index 22bf81d..5348fb4 100644 --- a/pkg/github/jobs/synchronizer.go +++ b/pkg/github/jobs/synchronizer.go @@ -3,6 +3,7 @@ package jobs import ( "context" "fmt" + "math/rand" "net/http" "strconv" "strings" @@ -77,6 +78,8 @@ func (s *Synchronizer) run( s.loadState(ctx, runs, jobs) + syncInterval := s.config.GetSyncInterval() + for { select { case <-ctx.Done(): @@ -110,6 +113,59 @@ func (s *Synchronizer) run( UpdatedAt: time.Now(), Object: run, } + case <-time.After(syncInterval): + // 0: runs; 1: jobs + choosenType := rand.Intn(2) + if len(runs) == 0 { + continue + } + if choosenType == 0 { + randRun := rand.Intn(len(runs)) + var choosenKey Key + for key, _ := range runs { + if randRun == 0 { + choosenKey = key + } + randRun-- + } + updatedRun, _, err := s.github.Actions.GetWorkflowRunByID(ctx, choosenKey.RepoOwner, choosenKey.RepoName, choosenKey.ID) + if err != nil { + s.logger.Warn("failed to get workflow run", + zap.Error(err), + zap.String("owner", choosenKey.RepoOwner), + zap.String("repo", choosenKey.RepoName), + zap.Int64("id", choosenKey.ID), + ) + } + //fmt.Printf("%+v\n", updatedRun) + runs[choosenKey] = cell[github.WorkflowRun]{ + UpdatedAt: runs[choosenKey].UpdatedAt, + Object: updatedRun, + } + } else if choosenType == 1 { + randJob := rand.Intn(len(jobs)) + var choosenKey Key + for key, _ := range jobs { + if randJob == 0 { + choosenKey = key + } + randJob-- + } + udatedJob, _, err := s.github.Actions.GetWorkflowJobByID(ctx, choosenKey.RepoOwner, choosenKey.RepoName, choosenKey.ID) + if err != nil { + s.logger.Warn("failed to get workflow job", + zap.Error(err), + zap.String("owner", choosenKey.RepoOwner), + zap.String("repo", choosenKey.RepoName), + zap.Int64("id", choosenKey.ID), + ) + } + //fmt.Printf("%+v\n", udatedJob) + jobs[choosenKey] = cell[github.WorkflowJob]{ + UpdatedAt: jobs[choosenKey].UpdatedAt, + Object: udatedJob, + } + } } retentionLimit := time.Now().Add(-s.config.GetRetentionPeriod())