go.temporal.io/server@v1.23.0/common/backoff/cron.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package backoff 26 27 import ( 28 "time" 29 30 "github.com/robfig/cron/v3" 31 "go.temporal.io/api/serviceerror" 32 33 "go.temporal.io/server/common/convert" 34 ) 35 36 // NoBackoff is used to represent backoff when no cron backoff is needed 37 const NoBackoff = time.Duration(-1) 38 39 // ValidateSchedule validates a cron schedule spec 40 func ValidateSchedule(cronSchedule string) error { 41 if cronSchedule == "" { 42 return nil 43 } 44 schedule, err := cron.ParseStandard(cronSchedule) 45 if err != nil { 46 return serviceerror.NewInvalidArgument("invalid CronSchedule.") 47 } 48 nextTime := schedule.Next(time.Now().UTC()) 49 if nextTime.IsZero() { 50 // no time can be found to satisfy the schedule 51 return serviceerror.NewInvalidArgument("invalid CronSchedule, no time can be found to satisfy the schedule") 52 } 53 return nil 54 } 55 56 // GetBackoffForNextSchedule calculates the backoff time for the next run given 57 // a cronSchedule, current scheduled time, and now. 58 func GetBackoffForNextSchedule(cronSchedule string, scheduledTime time.Time, now time.Time) time.Duration { 59 if len(cronSchedule) == 0 { 60 return NoBackoff 61 } 62 63 schedule, err := cron.ParseStandard(cronSchedule) 64 if err != nil { 65 return NoBackoff 66 } 67 68 scheduledUTCTime := scheduledTime.UTC() 69 nowUTC := now.UTC() 70 71 var nextScheduleTime time.Time 72 if nowUTC.Before(scheduledUTCTime) { 73 nextScheduleTime = scheduledUTCTime 74 } else { 75 nextScheduleTime = schedule.Next(scheduledUTCTime) 76 // Calculate the next schedule start time which is nearest to now (right after now). 77 for !nextScheduleTime.IsZero() && nextScheduleTime.Before(nowUTC) { 78 nextScheduleTime = schedule.Next(nextScheduleTime) 79 } 80 } 81 if nextScheduleTime.IsZero() { 82 // no time can be found to satisfy the schedule 83 return NoBackoff 84 } 85 86 backoffInterval := nextScheduleTime.Sub(nowUTC) 87 roundedInterval := time.Second * time.Duration(convert.Int64Ceil(backoffInterval.Seconds())) 88 return roundedInterval 89 } 90 91 // GetBackoffForNextScheduleNonNegative calculates the backoff time and ensures a non-negative duration. 92 func GetBackoffForNextScheduleNonNegative(cronSchedule string, scheduledTime time.Time, now time.Time) time.Duration { 93 backoffDuration := GetBackoffForNextSchedule(cronSchedule, scheduledTime, now) 94 if backoffDuration == NoBackoff || backoffDuration < 0 { 95 backoffDuration = 0 96 } 97 return backoffDuration 98 }