- Notifications
You must be signed in to change notification settings - Fork928
feat: add crontab package for supporting autostart/stop.#844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
50a9b26
feat: add crontab package for supporting autostart/stop.
johnstcnb8ec4fc
fixup! feat: add crontab package for supporting autostart/stop. This …
johnstcnc92c8eb
fixup! feat: add crontab package for supporting autostart/stop. This …
johnstcnbd02289
fixup! fixup! feat: add crontab package for supporting autostart/stop…
johnstcn45253a9
fix: return struct instead of interface
johnstcnb9b7a5a
remove unnecessary interface and export struct
johnstcn91cc3b6
fix: doc comments
johnstcn193d600
rename package to autostart/schedule
johnstcn32f0cb2
address PR comments
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletionscoderd/autostart/schedule/crontab_test.go
This file contains hidden or 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,67 @@ | ||
package schedule_test | ||
import ( | ||
"testing" | ||
"time" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/coderd/autostart/schedule" | ||
) | ||
func Test_Weekly(t *testing.T) { | ||
t.Parallel() | ||
testCases := []struct { | ||
name string | ||
spec string | ||
at time.Time | ||
expectedNext time.Time | ||
expectedError string | ||
}{ | ||
{ | ||
name: "with timezone", | ||
spec: "CRON_TZ=US/Central 30 9 1-5", | ||
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC), | ||
expectedNext: time.Date(2022, 4, 1, 14, 30, 0, 0, time.UTC), | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "without timezone", | ||
spec: "30 9 1-5", | ||
at: time.Date(2022, 4, 1, 9, 29, 0, 0, time.Local), | ||
expectedNext: time.Date(2022, 4, 1, 9, 30, 0, 0, time.Local), | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "invalid schedule", | ||
spec: "asdfasdfasdfsd", | ||
at: time.Time{}, | ||
expectedNext: time.Time{}, | ||
expectedError: "parse schedule: expected exactly 3 fields, found 1: [asdfasdfasdfsd]", | ||
}, | ||
{ | ||
name: "invalid location", | ||
spec: "CRON_TZ=Fictional/Country 30 9 1-5", | ||
at: time.Time{}, | ||
expectedNext: time.Time{}, | ||
expectedError: "parse schedule: provided bad location Fictional/Country: unknown time zone Fictional/Country", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.name, func(t *testing.T) { | ||
t.Parallel() | ||
actual, err := schedule.Weekly(testCase.spec) | ||
if testCase.expectedError == "" { | ||
nextTime := actual.Next(testCase.at) | ||
require.NoError(t, err) | ||
require.Equal(t, testCase.expectedNext, nextTime) | ||
require.Equal(t, testCase.spec, actual.String()) | ||
} else { | ||
require.EqualError(t, err, testCase.expectedError) | ||
require.Nil(t, actual) | ||
} | ||
}) | ||
} | ||
} |
66 changes: 66 additions & 0 deletionscoderd/autostart/schedule/schedule.go
This file contains hidden or 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,66 @@ | ||
// package schedule provides utilities for parsing and deserializing | ||
// cron-style expressions. | ||
package schedule | ||
import ( | ||
"time" | ||
"github.com/robfig/cron/v3" | ||
"golang.org/x/xerrors" | ||
) | ||
// For the purposes of this library, we only need minute, hour, and | ||
// day-of-week. | ||
constparserFormatWeekly=cron.Minute|cron.Hour|cron.Dow | ||
vardefaultParser=cron.NewParser(parserFormatWeekly) | ||
// Weekly parses a Schedule from spec scoped to a recurring weekly event. | ||
// Spec consists of the following space-delimited fields, in the following order: | ||
// - timezone e.g. CRON_TZ=US/Central (optional) | ||
// - minutes of hour e.g. 30 (required) | ||
// - hour of day e.g. 9 (required) | ||
// - day of week e.g. 1 (required) | ||
// | ||
// Example Usage: | ||
// local_sched, _ := schedule.Weekly("59 23 *") | ||
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339))) | ||
// // Output: 2022-04-04T23:59:00Z | ||
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5") | ||
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339)) | ||
// // Output: 2022-04-04T14:30:00Z | ||
funcWeekly(specstring) (*Schedule,error) { | ||
specSched,err:=defaultParser.Parse(spec) | ||
iferr!=nil { | ||
returnnil,xerrors.Errorf("parse schedule: %w",err) | ||
} | ||
schedule,ok:=specSched.(*cron.SpecSchedule) | ||
if!ok { | ||
returnnil,xerrors.Errorf("expected *cron.SpecSchedule but got %T",specSched) | ||
} | ||
cronSched:=&Schedule{ | ||
sched:schedule, | ||
spec:spec, | ||
} | ||
returncronSched,nil | ||
} | ||
// Schedule represents a cron schedule. | ||
// It's essentially a thin wrapper for robfig/cron/v3 that implements Stringer. | ||
typeSchedulestruct { | ||
sched*cron.SpecSchedule | ||
// XXX: there isn't any nice way for robfig/cron to serialize | ||
specstring | ||
} | ||
// String serializes the schedule to its original human-friendly format. | ||
func (sSchedule)String()string { | ||
returns.spec | ||
} | ||
// Next returns the next time in the schedule relative to t. | ||
func (sSchedule)Next(t time.Time) time.Time { | ||
returns.sched.Next(t) | ||
} |
1 change: 1 addition & 0 deletionsgo.mod
This file contains hidden or 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
2 changes: 2 additions & 0 deletionsgo.sum
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.