go.temporal.io/server/common/backoff/cron.go

73 LOC · 42 covered · 31 uncovered · 18 ranges · 161 concepts · 13 introducers · 91 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package backoff
2
3 import (
4 "time"
5
6 "github.com/robfig/cron/v3"
7 "go.temporal.io/api/serviceerror"
8 "go.temporal.io/server/common/convert"
9 )
10
11 // NoBackoff is used to represent backoff when no cron backoff is needed
12 const NoBackoff = time.Duration(-1)
13
14 // ValidateSchedule validates a cron schedule spec
15 > func ValidateSchedule(cronSchedule string) error { cron.go ×1
16 > if cronSchedule == "" {
17 > return nil cron.go ×1
18 > }
19 > schedule, err := cron.ParseStandard(cronSchedule) cron.go ×1
20 > if err != nil {
21 > return serviceerror.NewInvalidArgument("invalid CronSchedule.") cron.go ×1
22 > }
23 > nextTime := schedule.Next(time.Now().UTC()) cron.go ×1
24 > if nextTime.IsZero() {
25 > // no time can be found to satisfy the schedule cron.go ×1
26 > return serviceerror.NewInvalidArgument("invalid CronSchedule, no time can be found to satisfy the schedule")
27 > }
28 > return nil cron.go ×1
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 ×1
34 > if len(cronSchedule) == 0 {
35 > return NoBackoff util.go ×5
36 > }
37
38 > schedule, err := cron.ParseStandard(cronSchedule) cron.go ×5
39 > if err != nil {
40 return NoBackoff
41 }
42
43 > scheduledUTCTime := scheduledTime.UTC() cron.go ×5
44 > nowUTC := now.UTC()
45 >
46 > var nextScheduleTime time.Time
47 > if nowUTC.Before(scheduledUTCTime) {
48 > nextScheduleTime = scheduledUTCTime cron.go ×1
49 > } else { cron.go ×5
50 > nextScheduleTime = schedule.Next(scheduledUTCTime) cron.go ×1
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) cron.go ×1
54 > }
55 }
56 > if nextScheduleTime.IsZero() { cron.go ×5
57 // no time can be found to satisfy the schedule
58 return NoBackoff
59 }
60
61 > backoffInterval := nextScheduleTime.Sub(nowUTC) cron.go ×5
62 > roundedInterval := time.Second * time.Duration(convert.Int64Ceil(backoffInterval.Seconds()))
63 > return roundedInterval
64 }
65
66 // GetBackoffForNextScheduleNonNegative calculates the backoff time and ensures a non-negative duration.
67 > func GetBackoffForNextScheduleNonNegative(cronSchedule string, scheduledTime time.Time, now time.Time) time.Duration { util.go ×5
68 > backoffDuration := GetBackoffForNextSchedule(cronSchedule, scheduledTime, now)
69 > if backoffDuration == NoBackoff || backoffDuration < 0 {
70 > backoffDuration = 0
71 > }
72 > return backoffDuration
73 }