From 9b680530d52acbaac951787efc629dfe9f8dc11c Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 2 Jul 2021 17:00:18 +0800 Subject: [PATCH] add cron setting --- cmd/svc/main.go | 2 +- config.yml.sample | 4 ++++ go.mod | 1 + go.sum | 2 ++ pkg/config/config.go | 25 +++++++++++++++++++++++-- pkg/set/set.go | 2 +- 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/cmd/svc/main.go b/cmd/svc/main.go index 52f4a55..88a9ff6 100644 --- a/cmd/svc/main.go +++ b/cmd/svc/main.go @@ -22,7 +22,6 @@ import ( var cfgPath string = "./config.yml" func main() { - flag.StringVar(&cfgPath, "c", "./config.yml", "config yaml path") flag.Parse() @@ -106,6 +105,7 @@ func runService(reload <-chan struct{}) { if err := stopServer(svc); err != nil { log.Fatal(err) } + svc = nil } } } diff --git a/config.yml.sample b/config.yml.sample index 02386f6..c41812c 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -7,3 +7,7 @@ listens: path: /t headers: {} script: /tmp/run.sh + +cron_jobs: + - cron_time: '0 1 * * *' + script: /tmp/run.sh diff --git a/go.mod b/go.mod index 051b937..0bb4798 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/otakukaze/envconfig v1.0.4 // indirect + github.com/robfig/cron/v3 v3.0.0 // indirect github.com/ugorji/go/codec v1.2.6 // indirect golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect diff --git a/go.sum b/go.sum index f7d8b09..3677b92 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= +github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/pkg/config/config.go b/pkg/config/config.go index 3931f42..f152798 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -7,19 +7,26 @@ import ( "strings" errors "github.com/pkg/errors" + "github.com/robfig/cron/v3" confLoader "github.com/otakukaze/config-loader" ) type Config struct { - Server Server `yaml:"server"` - Listens []Listen `yaml:"listens"` + Server Server `yaml:"server"` + Listens []Listen `yaml:"listens"` + CronJobs []CronJob `yaml:"cron_jobs"` } type Server struct { Port int `yaml:"port" env:"SERVER_PORT" default:"10230"` } +type CronJob struct { + CronTime string `yaml:"cron_time"` + Script string `yaml:"script"` +} + type Listen struct { HTTP *HTTPListen `yaml:"http"` } @@ -88,5 +95,19 @@ func Load(p ...string) (*Config, error) { } } + // check cronJob + cronParser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow) + for _, v := range cfg.CronJobs { + _, err := cronParser.Parse(v.CronTime) + if err != nil { + return nil, err + } + + // check script exists + if !tools.CheckExists(v.Script, false) { + return nil, errors.WithStack(fmt.Errorf("cron (%s) script not exists: %s\n", v.CronTime, v.Script)) + } + } + return cfg, nil } diff --git a/pkg/set/set.go b/pkg/set/set.go index 6cd08c8..d0a9366 100644 --- a/pkg/set/set.go +++ b/pkg/set/set.go @@ -22,7 +22,7 @@ func (s Set) Size() int { func (s Set) Keys() []interface{} { keys := make([]interface{}, 0) - for k, _ := range s { + for k := range s { keys = append(keys, k) }