add cron setting
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Jay 2021-07-02 17:00:18 +08:00
parent a487ae9019
commit 9b680530d5
6 changed files with 32 additions and 4 deletions

View File

@ -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
}
}
}

View File

@ -7,3 +7,7 @@ listens:
path: /t
headers: {}
script: /tmp/run.sh
cron_jobs:
- cron_time: '0 1 * * *'
script: /tmp/run.sh

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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)
}