13
14
// ValidateSchedule validates a cron schedule spec
16
>
if cronSchedule == "" {
17
return nil
18
}
20
>
if err != nil {
21
return serviceerror.NewInvalidArgument("invalid CronSchedule.")
22
}
24
>
if nextTime.IsZero() {
25
// no time can be found to satisfy the schedule
26
return serviceerror.NewInvalidArgument("invalid CronSchedule, no time can be found to satisfy the schedule")
27
}
29
}
30
31
// GetBackoffForNextSchedule calculates the backoff time for the next run given
32
// a cronSchedule, current scheduled time, and now.
33
>
func GetBackoffForNextSchedule(cronSchedule string, scheduledTime time.Time, now time.Time) time.Duration {
cron.go
34
>
if len(cronSchedule) == 0 {
35
return NoBackoff
36
}
37
39
>
if err != nil {
40
return NoBackoff
41
}
42
44
>
nowUTC := now.UTC()
45
>
46
>
var nextScheduleTime time.Time
47
>
if nowUTC.Before(scheduledUTCTime) {
48
nextScheduleTime = scheduledUTCTime
51
>
// Calculate the next schedule start time which is nearest to now (right after now).
52
>
for !nextScheduleTime.IsZero() && nextScheduleTime.Before(nowUTC) {
53
nextScheduleTime = schedule.Next(nextScheduleTime)
54
}
55
}
57
// no time can be found to satisfy the schedule
58
return NoBackoff
59
}
60
62
>
roundedInterval := time.Second * time.Duration(convert.Int64Ceil(backoffInterval.Seconds()))
63
>
return roundedInterval
64
}
65