Skip to content

Commit

Permalink
Refresh status of in-progress jobs/runs periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngkaokis committed Jul 11, 2022
1 parent 223d8dc commit 36944ae
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/github/jobs/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jobs
import (
"context"
"fmt"
"math/rand"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -77,6 +78,8 @@ func (s *Synchronizer) run(

s.loadState(ctx, runs, jobs)

syncInterval := s.config.GetSyncInterval()

for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 36944ae

Please sign in to comment.