Skip to content

Commit 32f0cb2

Browse files
committed
address PR comments
1 parent 193d600 commit 32f0cb2

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

coderd/autostart/schedule/crontab_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package crontab_test
1+
package schedule_test
22

33
import (
44
"testing"
55
"time"
66

77
"github.com/stretchr/testify/require"
88

9-
"github.com/coder/coder/coderd/crontab"
9+
"github.com/coder/coder/coderd/autostart/schedule"
1010
)
1111

12-
func Test_Parse(t *testing.T) {
12+
func Test_Weekly(t *testing.T) {
1313
t.Parallel()
1414
testCases := []struct {
1515
name string
@@ -52,7 +52,7 @@ func Test_Parse(t *testing.T) {
5252
testCase := testCase
5353
t.Run(testCase.name, func(t *testing.T) {
5454
t.Parallel()
55-
actual, err := crontab.Parse(testCase.spec)
55+
actual, err := schedule.Weekly(testCase.spec)
5656
if testCase.expectedError == "" {
5757
nextTime := actual.Next(testCase.at)
5858
require.NoError(t, err)
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// package crontab provides utilities for parsing and deserializing
1+
// package schedule provides utilities for parsing and deserializing
22
// cron-style expressions.
3-
package crontab
3+
package schedule
44

55
import (
66
"time"
@@ -10,21 +10,26 @@ import (
1010
)
1111

1212
// For the purposes of this library, we only need minute, hour, and
13-
//day-of-week.
14-
const parserFormat = cron.Minute | cron.Hour | cron.Dow
13+
// day-of-week.
14+
const parserFormatWeekly = cron.Minute | cron.Hour | cron.Dow
1515

16-
var defaultParser = cron.NewParser(parserFormat)
16+
var defaultParser = cron.NewParser(parserFormatWeekly)
1717

18-
// Parse parses a WeeklySchedule from spec.
18+
// Weekly parses a Schedule from spec scoped to a recurring weekly event.
19+
// Spec consists of the following space-delimited fields, in the following order:
20+
// - timezone e.g. CRON_TZ=US/Central (optional)
21+
// - minutes of hour e.g. 30 (required)
22+
// - hour of day e.g. 9 (required)
23+
// - day of week e.g. 1 (required)
1924
//
2025
// Example Usage:
21-
// local_sched, _ := cron.Parse("59 23 *")
26+
// local_sched, _ := schedule.Weekly("59 23 *")
2227
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))
2328
// // Output: 2022-04-04T23:59:00Z
24-
// us_sched, _ := cron.Parse("CRON_TZ=US/Central 30 9 1-5")
29+
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5")
2530
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
2631
// // Output: 2022-04-04T14:30:00Z
27-
func Parse(spec string) (*WeeklySchedule, error) {
32+
func Weekly(spec string) (*Schedule, error) {
2833
specSched, err := defaultParser.Parse(spec)
2934
if err != nil {
3035
return nil, xerrors.Errorf("parse schedule: %w", err)
@@ -35,27 +40,27 @@ func Parse(spec string) (*WeeklySchedule, error) {
3540
return nil, xerrors.Errorf("expected *cron.SpecSchedule but got %T", specSched)
3641
}
3742

38-
cronSched := &WeeklySchedule{
43+
cronSched := &Schedule{
3944
sched: schedule,
4045
spec: spec,
4146
}
4247
return cronSched, nil
4348
}
4449

45-
// WeeklySchedule represents a weekly cron schedule.
50+
// Schedule represents a cron schedule.
4651
// It's essentially a thin wrapper for robfig/cron/v3 that implements Stringer.
47-
type WeeklySchedule struct {
52+
type Schedule struct {
4853
sched *cron.SpecSchedule
4954
// XXX: there isn't any nice way for robfig/cron to serialize
5055
spec string
5156
}
5257

5358
// String serializes the schedule to its original human-friendly format.
54-
func (s WeeklySchedule) String() string {
59+
func (s Schedule) String() string {
5560
return s.spec
5661
}
5762

5863
// Next returns the next time in the schedule relative to t.
59-
func (s WeeklySchedule) Next(t time.Time) time.Time {
64+
func (s Schedule) Next(t time.Time) time.Time {
6065
return s.sched.Next(t)
6166
}

0 commit comments

Comments
 (0)