Skip to content

Commit

Permalink
fix: exclude jitter duration
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 5, 2024
1 parent e3655c3 commit d2e654d
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,26 @@ func (j *Job) SetID(id string) *Job {
}

func (j *Job) Run() {
if !j.Context.Properties().On(false, "job.jitter.disable") && j.Schedule != "" {
// Attempt to get a fixed interval from the schedule to measure the appropriate jitter.
// NOTE: Only works for fixed interval schedules.
parsedSchedule, err := cron.ParseStandard(j.Schedule)
if err != nil {
j.Debugf("failed to parse schedule (%s): %s", j.Schedule, err)
} else {
interval := time.Until(parsedSchedule.Next(time.Now()))
if interval > maxJitterDuration {
interval = maxJitterDuration
}

delayPercent := rand.Intn(iterationJitterPercent)
jitterDuration := time.Duration((int64(interval) * int64(delayPercent)) / 100)
j.Context.Logger.V(4).Infof("jitter %v", jitterDuration)

time.Sleep(jitterDuration)
}
}

ctx, span := j.Context.StartSpan(j.Name)
ctx = ctx.WithName("job." + j.PK())
defer span.End()
Expand Down Expand Up @@ -357,26 +377,6 @@ func (j *Job) Run() {
defer j.lock.Unlock()
}

if !j.Context.Properties().On(false, "job.jitter.disable") && j.Schedule != "" {
// Attempt to get a fixed interval from the schedule to measure the appropriate jitter.
// NOTE: Only works for fixed interval schedules.
parsedSchedule, err := cron.ParseStandard(j.Schedule)
if err != nil {
j.Debugf("failed to parse schedule (%s): %s", j.Schedule, err)
} else {
interval := time.Until(parsedSchedule.Next(time.Now()))
if interval > maxJitterDuration {
interval = maxJitterDuration
}

delayPercent := rand.Intn(iterationJitterPercent)
jitterDuration := time.Duration((int64(interval) * int64(delayPercent)) / 100)
j.Context.Logger.V(4).Infof("jitter %v", jitterDuration)

time.Sleep(jitterDuration)
}
}

for i, lock := range j.Semaphores {
ctx.Logger.V(6).Infof("[%s] acquiring sempahore [%d/%d]", j.ID, i+1, len(j.Semaphores))
if err := lock.Acquire(ctx, 1); err != nil {
Expand Down

0 comments on commit d2e654d

Please sign in to comment.