From b48e6804077cd8ed45dce54199b5cdd78c343df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20K=C3=B6ken?= Date: Tue, 27 Sep 2022 23:42:33 +0300 Subject: [PATCH] Fix the bug that causes scheduled task running multiple times in a second (#19) Fix the bug that causes scheduled task running multiple times in a second --- trigger.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/trigger.go b/trigger.go index 32e16c6..5cfd742 100644 --- a/trigger.go +++ b/trigger.go @@ -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) }