From 2db4fc7176c810d47b089e423977e8ccf628bd70 Mon Sep 17 00:00:00 2001 From: bel Date: Fri, 13 Mar 2020 04:40:02 +0000 Subject: [PATCH] Implement main, optional seconds, and from file --- TODO.md | 4 +++- main.go | 15 +++++++++++++++ scheduler/parser.go | 3 ++- scheduler/parser_test.go | 3 ++- scheduler/scheduler.go | 9 +++++---- scheduler/scheduler_test.go | 4 ++++ testdata/comment_only.cron | 1 + 7 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 testdata/comment_only.cron diff --git a/TODO.md b/TODO.md index ba44eba..de637ad 100644 --- a/TODO.md +++ b/TODO.md @@ -17,7 +17,9 @@ # Backend -1. load from file +x load from file 1. interrupt running jobs 1. temporarily disable jobs 1. json API +1. change cron to load the full job from storage so not holding big queued jobs in ram +1. add optional second for test main diff --git a/main.go b/main.go index 5cd3a64..4f86f37 100755 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "local/firestormy/config" + "local/firestormy/scheduler" "local/firestormy/server" "local/lastn/lastn" "log" @@ -13,11 +14,24 @@ import ( ) func main() { + var err error + s := scheduler.New() + if config.Config != "" { + s, err = scheduler.NewFromFile(config.Config) + } else { + err = s.Start() + } + if err != nil { + panic(err) + } + server := server.New() if err := server.Routes(); err != nil { panic(err) } + go EnqueueBackups() + go func() { log.Printf("Serving on %q", config.Port) if err := http.ListenAndServe(config.Port, server); err != nil { @@ -29,6 +43,7 @@ func main() { stop := make(chan os.Signal) signal.Notify(stop, os.Interrupt) <-stop + s.Stop() } func EnqueueBackups() { diff --git a/scheduler/parser.go b/scheduler/parser.go index 5ceeef5..92c25f1 100644 --- a/scheduler/parser.go +++ b/scheduler/parser.go @@ -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 | diff --git a/scheduler/parser_test.go b/scheduler/parser_test.go index 4fcf659..6c5731a 100644 --- a/scheduler/parser_test.go +++ b/scheduler/parser_test.go @@ -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 { diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 7d7199e..d3a55d6 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -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 } diff --git a/scheduler/scheduler_test.go b/scheduler/scheduler_test.go index 11036cb..5d7b8bc 100644 --- a/scheduler/scheduler_test.go +++ b/scheduler/scheduler_test.go @@ -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, diff --git a/testdata/comment_only.cron b/testdata/comment_only.cron new file mode 100644 index 0000000..91ca88c --- /dev/null +++ b/testdata/comment_only.cron @@ -0,0 +1 @@ +# comment only