add cron setting
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
+23 -2
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
}