-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
source-mysql-batch: Add 'daily at 6:00Z' polling schedule
Previously we just had duration strings to specify polling intervals for batch captures. This commit factors out that duration handling into a separate 'schedule' helper package and defines another type of schedule: `"daily at HH:MMZ"` which consistently executes at the requested time of day (in UTC). This refactoring will be applied to other batch SQL captures in a followup commit.
- Loading branch information
1 parent
3e06e50
commit b4ef86d
Showing
6 changed files
with
169 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package schedule | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// A Schedule represents a sequence of points in time when an action should be performed. | ||
type Schedule interface { | ||
// Next returns the earliest instant in time greater than `afterTime` which | ||
// satisfies the schedule. | ||
Next(afterTime time.Time) time.Time | ||
} | ||
|
||
// Validate checks whether the provided schedule description is valid and returns an error if it's incorrect. | ||
func Validate(desc string) error { | ||
var _, err = Parse(desc) | ||
return err | ||
} | ||
|
||
// Parse turns a textual schedule description into an object with the Schedule interface. | ||
func Parse(desc string) (Schedule, error) { | ||
if pollInterval, err := time.ParseDuration(desc); err == nil { | ||
return &periodicSchedule{Period: pollInterval}, nil | ||
} | ||
if strings.HasPrefix(desc, "daily at ") { | ||
var timeOfDay, err = time.Parse("15:04Z", strings.TrimPrefix(desc, "daily at ")) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid time %q (time of day should look like '13:00Z'): %w", timeOfDay, err) | ||
} | ||
return &dailySchedule{TimeOfDay: timeOfDay}, nil | ||
} | ||
return nil, fmt.Errorf("invalid polling schedule %q", desc) | ||
} | ||
|
||
type periodicSchedule struct { | ||
Period time.Duration | ||
} | ||
|
||
func (s *periodicSchedule) Next(after time.Time) time.Time { | ||
return after.Add(s.Period) | ||
} | ||
|
||
type dailySchedule struct { | ||
TimeOfDay time.Time | ||
} | ||
|
||
func (s *dailySchedule) Next(after time.Time) time.Time { | ||
// Construct a timestamp with the appropriate time of day, on the same day as the | ||
// 'after' timestamp. Then increment it day by day until it's actually greater than | ||
// the 'after' timestamp. | ||
var yyyy, mm, dd = after.UTC().Date() | ||
var t = time.Date(yyyy, mm, dd, s.TimeOfDay.Hour(), s.TimeOfDay.Minute(), s.TimeOfDay.Second(), 0, time.UTC) | ||
if !t.After(after) { | ||
t = t.AddDate(0, 0, 1) | ||
} | ||
return t | ||
} | ||
|
||
// WaitForNext sleeps until the next scheduled execution time, or until the | ||
// context is cancelled. | ||
func WaitForNext(ctx context.Context, s Schedule, after time.Time) error { | ||
var d = time.Until(s.Next(after)) | ||
if d <= 0 { | ||
return nil | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-time.After(d): | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package schedule | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPeriodicSchedule(t *testing.T) { | ||
for _, tc := range []struct { | ||
Schedule string | ||
After string | ||
Expect string | ||
}{ | ||
{"1h", "2024-02-15T05:00:00Z", "2024-02-15T06:00:00Z"}, | ||
{"2h", "2024-02-15T12:34:56Z", "2024-02-15T14:34:56Z"}, | ||
{"6h", "2024-02-15T19:34:56Z", "2024-02-16T01:34:56Z"}, | ||
{"24h", "2024-02-15T19:34:56Z", "2024-02-16T19:34:56Z"}, | ||
{"168h", "2024-02-15T19:34:56Z", "2024-02-22T19:34:56Z"}, | ||
{"30m", "2024-02-15T19:34:56Z", "2024-02-15T20:04:56Z"}, | ||
{"10m", "2024-02-15T19:34:56Z", "2024-02-15T19:44:56Z"}, | ||
{"1m", "2024-02-15T19:34:56Z", "2024-02-15T19:35:56Z"}, | ||
{"30s", "2024-02-15T19:34:56Z", "2024-02-15T19:35:26Z"}, | ||
{"10s", "2024-02-15T19:34:56Z", "2024-02-15T19:35:06Z"}, | ||
{"1s", "2024-02-15T19:34:56Z", "2024-02-15T19:34:57Z"}, | ||
} { | ||
sched, err := Parse(tc.Schedule) | ||
require.NoError(t, err) | ||
after, err := time.Parse(time.RFC3339, tc.After) | ||
require.NoError(t, err) | ||
var ts = sched.Next(after) | ||
require.Equal(t, tc.Expect, ts.Format(time.RFC3339)) | ||
} | ||
} | ||
|
||
func TestDailySchedule(t *testing.T) { | ||
for _, tc := range []struct { | ||
Schedule string | ||
After string | ||
Expect string | ||
}{ | ||
{"daily at 6:00Z", "2024-02-15T01:00:00Z", "2024-02-15T06:00:00Z"}, | ||
{"daily at 6:00Z", "2024-02-15T05:00:00Z", "2024-02-15T06:00:00Z"}, | ||
{"daily at 6:00Z", "2024-02-15T06:00:00Z", "2024-02-16T06:00:00Z"}, | ||
{"daily at 6:00Z", "2024-02-15T07:00:00Z", "2024-02-16T06:00:00Z"}, | ||
{"daily at 06:00Z", "2024-02-15T01:00:00Z", "2024-02-15T06:00:00Z"}, | ||
{"daily at 06:00Z", "2024-02-15T05:00:00Z", "2024-02-15T06:00:00Z"}, | ||
{"daily at 06:00Z", "2024-02-15T06:00:00Z", "2024-02-16T06:00:00Z"}, | ||
{"daily at 06:00Z", "2024-02-15T07:00:00Z", "2024-02-16T06:00:00Z"}, | ||
{"daily at 13:55Z", "2024-02-15T01:00:00Z", "2024-02-15T13:55:00Z"}, | ||
{"daily at 13:55Z", "2024-02-15T13:54:00Z", "2024-02-15T13:55:00Z"}, | ||
{"daily at 13:55Z", "2024-02-15T13:55:00Z", "2024-02-16T13:55:00Z"}, | ||
{"daily at 13:55Z", "2024-02-15T13:56:00Z", "2024-02-16T13:55:00Z"}, | ||
{"daily at 13:55Z", "2024-02-15T23:00:00Z", "2024-02-16T13:55:00Z"}, | ||
} { | ||
sched, err := Parse(tc.Schedule) | ||
require.NoError(t, err) | ||
after, err := time.Parse(time.RFC3339, tc.After) | ||
require.NoError(t, err) | ||
var ts = sched.Next(after) | ||
require.Equal(t, tc.Expect, ts.Format(time.RFC3339)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters