Skip to content

Commit

Permalink
Fix the bug that causes scheduled task running multiple times in a se…
Browse files Browse the repository at this point in the history
…cond (#19)

Fix the bug that causes scheduled task running multiple times in a second
  • Loading branch information
burakkoken authored Sep 27, 2022
1 parent 0b90d33 commit b48e680
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,27 @@ func (trigger *CronTrigger) NextExecutionTime(ctx TriggerContext) time.Time {
originalLocation := now.Location()

convertedTime := now.In(trigger.location)
newTime := time.Date(convertedTime.Year(),
convertedTime = time.Date(convertedTime.Year(),
convertedTime.Month(),
convertedTime.Day(),
convertedTime.Hour(),
convertedTime.Minute(),
convertedTime.Second(),
0,
convertedTime.Nanosecond(),
trigger.location)

next := trigger.cronExpression.NextTime(convertedTime)

// there is a bug causes timezone changing when an operation is performed on time value like add, subtraction
// to resolve this issue, we use a workaround solution
next = time.Date(next.Year(),
next.Month(),
next.Day(),
next.Hour(),
next.Minute(),
next.Second(),
next.Nanosecond(),
trigger.location)

next := trigger.cronExpression.NextTime(newTime)
return next.In(originalLocation)
}

0 comments on commit b48e680

Please sign in to comment.