Skip to content

Commit

Permalink
feat: jitter on cron jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Aug 19, 2024
1 parent 2b3ecd0 commit 249574c
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package job
import (
"container/ring"
gocontext "context"
"crypto/rand"
"fmt"
"math/big"
"strconv"
"sync"
"time"
Expand All @@ -26,6 +28,14 @@ const (
ResourceTypeUpstream = "upstream"
)

const (
// initialJitterPercent sets by how much the initial scheduling of the job should be delayed
initialJitterPercent = 100

Check failure on line 33 in job/job.go

View workflow job for this annotation

GitHub Actions / lint

const `initialJitterPercent` is unused (unused)

// iterationJitterPercent sets the maximum percent by which to jitter each subsequent invocation of a periodic job
iterationJitterPercent = 10
)

var evictedJobs chan uuid.UUID

// deleteEvictedJobs deletes job_history rows from the DB every job.eviction.period(1m),
Expand Down Expand Up @@ -511,20 +521,30 @@ func (j *Job) AddToScheduler(cronRunner *cron.Cron) error {
j.Context.Infof("skipping scheduling")
return nil
}

j.Context.Logger.Named(j.GetResourcedName()).V(1).Infof("scheduled %s", schedule)
entryID, err := cronRunner.AddJob(schedule, j)

delayedJob := cron.FuncJob(func() {
randomDelay, _ := rand.Int(rand.Reader, big.NewInt(int64(iterationJitterPercent)))
jitterDuration := time.Duration(randomDelay.Int64())
j.Context.Logger.Named(j.GetResourcedName()).V(2).Infof("jitter: %s", jitterDuration)
time.Sleep(jitterDuration)
j.Run()
})
entryID, err := cronRunner.AddJob(schedule, delayedJob)
if err != nil {
return fmt.Errorf("[%s] failed to schedule job: %s", j.Label(), err)
}
j.entryID = &entryID

if j.RunNow {
// Run in a goroutine since AddToScheduler should be non-blocking
defer func() { go j.Run() }()
}

j.unschedule = func() {
cronRunner.Remove(*j.entryID)
}

return nil
}

Expand Down

0 comments on commit 249574c

Please sign in to comment.