-
Notifications
You must be signed in to change notification settings - Fork 894
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50a9b26
feat: add crontab package for supporting autostart/stop.
johnstcn b8ec4fc
fixup! feat: add crontab package for supporting autostart/stop. This …
johnstcn c92c8eb
fixup! feat: add crontab package for supporting autostart/stop. This …
johnstcn bd02289
fixup! fixup! feat: add crontab package for supporting autostart/stop…
johnstcn 45253a9
fix: return struct instead of interface
johnstcn b9b7a5a
remove unnecessary interface and export struct
johnstcn 91cc3b6
fix: doc comments
johnstcn 193d600
rename package to autostart/schedule
johnstcn 32f0cb2
address PR comments
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: add crontab package for supporting autostart/stop.
This is basically a small wrapper around robfig/cron/v3. Fixes #817.
- Loading branch information
commit 50a9b26055c9ace125e1cc6a675a291fad2bc1ce
There are no files selected for viewing
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,64 @@ | ||
package crontab | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/robfig/cron/v3" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
const parserFormat = cron.Minute | cron.Hour | cron.Dow | ||
|
||
var defaultParser = cron.NewParser(parserFormat) | ||
|
||
// WeeklySchedule represents a weekly cron schedule serializable to and from a string. | ||
// | ||
// Example Usage: | ||
// local_sched, _ := cron.Parse("59 23 *") | ||
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339))) | ||
// // Output: 2022-04-04T23:59:00Z | ||
// us_sched, _ := cron.Parse("TZ=US/Central 30 9 1-5") | ||
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339)) | ||
// // Output: 2022-04-04T14:30:00Z | ||
type WeeklySchedule interface { | ||
String() string | ||
Next(time.Time) time.Time | ||
} | ||
|
||
// cronSchedule is a thin wrapper for cron.SpecSchedule that implements Stringer. | ||
type cronSchedule struct { | ||
sched *cron.SpecSchedule | ||
// XXX: there isn't any nice way for robfig/cron to serialize | ||
spec string | ||
} | ||
|
||
var _ WeeklySchedule = (*cronSchedule)(nil) | ||
|
||
// String serializes the schedule to its original human-friendly format. | ||
func (s cronSchedule) String() string { | ||
return s.spec | ||
} | ||
|
||
// Next returns the next time in the schedule relative to t. | ||
func (s cronSchedule) Next(t time.Time) time.Time { | ||
return s.sched.Next(t) | ||
} | ||
|
||
func Parse(spec string) (WeeklySchedule, error) { | ||
s, err := defaultParser.Parse(spec) | ||
if err != nil { | ||
return nil, xerrors.Errorf("parse schedule: %w", err) | ||
} | ||
|
||
schedule, ok := s.(*cron.SpecSchedule) | ||
if !ok { | ||
return nil, xerrors.Errorf("expected cron.SpecSchedule but got %T", s) | ||
} | ||
|
||
cs := &cronSchedule{ | ||
sched: schedule, | ||
spec: spec, | ||
} | ||
return cs, nil | ||
|
||
} |
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,65 @@ | ||
package crontab | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Parse(t *testing.T) { | ||
t.Parallel() | ||
testCases := []struct { | ||
name string | ||
spec string | ||
at time.Time | ||
expectedNext time.Time | ||
expectedError string | ||
}{ | ||
{ | ||
name: "with timezone", | ||
spec: "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: "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 _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
actual, err := Parse(tc.spec) | ||
if tc.expectedError == "" { | ||
nextTime := actual.Next(tc.at) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expectedNext, nextTime) | ||
require.Equal(t, tc.spec, actual.String()) | ||
} else { | ||
require.EqualError(t, err, tc.expectedError) | ||
require.Nil(t, actual) | ||
} | ||
}) | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.