Implement main, optional seconds, and from file

This commit is contained in:
bel
2020-03-13 04:40:02 +00:00
parent a1cea7d1cb
commit 877ec310bd
7 changed files with 32 additions and 7 deletions

View File

@@ -4,7 +4,8 @@ import cron "github.com/robfig/cron/v3"
func getParser() cron.Parser {
return cron.NewParser(
cron.Minute |
cron.SecondOptional |
cron.Minute |
cron.Hour |
cron.Dom |
cron.Month |

View File

@@ -6,7 +6,6 @@ func TestValidCronFail(t *testing.T) {
cases := []string{
"a 1 1 1 1",
"1 1 1 1",
"1 1 1 1 1 1",
"@minutely",
}
@@ -22,10 +21,12 @@ func TestValidCronPass(t *testing.T) {
"1 1 1 1 1",
"* * 1 1 1",
"@hourly",
"1 1 1 1 1 1",
"@daily",
"@yearly",
"@weekly",
"* */5 1 1 1",
"*/5 * */5 1 1 1",
}
for _, c := range cases {

View File

@@ -28,6 +28,7 @@ func New() *Scheduler {
cron.SkipIfStillRunning(l),
cron.Recover(l),
),
cron.WithParser(getParser()),
)
return &Scheduler{
cron: c,
@@ -79,7 +80,7 @@ func cleanLine(b []byte) []byte {
}
func splitScheduleCommand(b []byte) (string, string) {
re := regexp.MustCompile(`^((\d+|\*\/\d+|(\d,)*\d+|\*) [ ]*){5}`)
re := regexp.MustCompile(`^((\d+|\*\/\d+|(\d,)*\d+|\*) [ ]*){5,6}`)
schedule := string(re.Find(b))
if len(schedule) == 0 {
return "", ""
@@ -150,13 +151,13 @@ func (s *Scheduler) Add(j *Job) error {
if err != nil {
return err
}
if err := config.Store.Set(j.Name, b); err != nil {
return err
}
entryID, err := s.cron.AddJob(j.Schedule, j)
if err != nil {
return err
}
if err := config.Store.Set(j.Name, b); err != nil {
return err
}
s.running[j.Name] = entryID
return nil
}

View File

@@ -75,6 +75,10 @@ func TestSchedulerFromFile(t *testing.T) {
content: `10 */12 * * * /bin/bash -c "hostname"`,
want: 1,
},
"just a job with seconds": {
content: `*/2 10 */12 * * * /bin/bash -c "hostname"`,
want: 1,
},
"all wild": {
content: `* * * * * /bin/bash -c "hostname"`,
want: 1,