diff --git a/README.md b/README.md index d1c0273..b8aaf4c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# go-cron +# gron *cron-like job scheduler* diff --git a/config.go b/config.go new file mode 100644 index 0000000..6d3d741 --- /dev/null +++ b/config.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/BurntSushi/toml" + "github.com/creasty/defaults" +) + +var config Config + +type Config struct { + TimeFormat string `default:"02.01.2006 15:04:05"` + JobConfigsPath string `default:"gron.d"` + LogFilePath string `default:"gron.log"` // core log file path + LogFilesPath string `default:"logs"` // job log files path + HttpListenAddr string `default:"127.0.0.1:9876"` +} + +func initConfig() error { + ex, err := os.Executable() + if err != nil { + return err + } + + if len(os.Args) > 2 { + return fmt.Errorf("Usage: %s [path to config]", filepath.Base(ex)) + } + + configFilePath := defaultConfigFilePath + if len(os.Args) == 2 { + configFilePath = os.Args[1] + } + + _, err = toml.DecodeFile(configFilePath, &config) + if err != nil { + return err + } + + // Set defaults + if err := defaults.Set(&config); err != nil { + return err + } + + return nil +} diff --git a/consts.go b/consts.go index 9f816e7..be0a40e 100644 --- a/consts.go +++ b/consts.go @@ -3,15 +3,12 @@ package main import formatter "github.com/antonfisher/nested-logrus-formatter" const ( - timeFormat = "02.01.2006 15:04:05" - logFileName = "log.txt" - logFilesPath = "logs" - listenAddress = "127.0.0.1:9876" + defaultConfigFilePath = "gron.conf" ) var ( logFormat = &formatter.Formatter{ - TimestampFormat: timeFormat, + TimestampFormat: config.TimeFormat, HideKeys: true, NoColors: true, TrimMessages: true} diff --git a/go.mod b/go.mod index 156448d..cb7efc9 100644 --- a/go.mod +++ b/go.mod @@ -11,8 +11,9 @@ require ( ) require ( + github.com/creasty/defaults v1.5.2 github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect + golang.org/x/sys v0.0.0-20220325203850-36772127a21f // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index a02f865..d576446 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ= github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA= +github.com/creasty/defaults v1.5.2 h1:/VfB6uxpyp6h0fr7SPp7n8WJBoV8jfxQXPCnkVSjyls= +github.com/creasty/defaults v1.5.2/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -15,8 +17,11 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220325203850-36772127a21f h1:TrmogKRsSOxRMJbLYGrB4SBbW+LJcEllYBLME5Zk5pU= +golang.org/x/sys v0.0.0-20220325203850-36772127a21f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/gron.conf b/gron.conf new file mode 100644 index 0000000..f5d6541 --- /dev/null +++ b/gron.conf @@ -0,0 +1,5 @@ +TimeFormat = "02.01.2006 15:04:05" +JobConfigsPath = "gron.d" +LogFilePath = "gron.log" +LogFilesPath = "logs" +HttpListenAddr = "127.0.0.1:9876" diff --git a/httpserver.go b/httpserver.go index 5651d19..69dea76 100644 --- a/httpserver.go +++ b/httpserver.go @@ -11,6 +11,10 @@ import ( ) func httpServer(listenAddress string) { + if listenAddress == "none" { + return + } + http.HandleFunc("/", handler) http.HandleFunc("/reloadJobs", handleReloadJobs) http.HandleFunc("/shutdown", handleShutdown) diff --git a/job.go b/job.go index 570abe1..6187454 100644 --- a/job.go +++ b/job.go @@ -71,10 +71,10 @@ func (j *Job) Run() { globalMutex.Lock() j.CurrentRunningCount++ - j.LastStartTime = startTime.Format(timeFormat) + j.LastStartTime = startTime.Format(config.TimeFormat) globalMutex.Unlock() - jobLogFile, _ := os.OpenFile(filepath.Join(logFilesPath, j.FileName+".txt"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) + jobLogFile, _ := os.OpenFile(filepath.Join(config.LogFilesPath, j.FileName+".txt"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) defer jobLogFile.Close() defer jobLogFile.WriteString("\n") @@ -109,7 +109,7 @@ func (j *Job) Run() { globalMutex.Lock() j.CurrentRunningCount-- - j.LastEndTime = endTime.Format(timeFormat) + j.LastEndTime = endTime.Format(config.TimeFormat) j.LastExecutionDuration = endTime.Sub(startTime).Truncate(time.Second).String() globalMutex.Unlock() } diff --git a/log.go b/log.go index f358439..dbf23bf 100644 --- a/log.go +++ b/log.go @@ -2,16 +2,13 @@ package main import ( "os" - - log "github.com/sirupsen/logrus" ) var logFile *os.File -func initLogFile() { +func initLogFile() error { var err error - logFile, err = os.OpenFile(logFileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - if err != nil { - log.Fatalln(err) - } + logFile, err = os.OpenFile(config.LogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) + + return err } diff --git a/main.go b/main.go index 575cdf3..e116c19 100644 --- a/main.go +++ b/main.go @@ -13,12 +13,20 @@ import ( var c *cron.Cron func init() { - err := os.MkdirAll(logFilesPath, 0644) + err := initConfig() + if err != nil { + panic(err) + } + + err = os.MkdirAll(config.LogFilesPath, 0644) if err != nil { log.Fatalln(err) } - initLogFile() + err = initLogFile() + if err != nil { + log.Fatalln(err) + } log.SetFormatter(logFormat) //multiWriter := io.MultiWriter(os.Stderr, logFile) @@ -28,7 +36,7 @@ func init() { initTemplate() - go httpServer(listenAddress) + go httpServer(config.HttpListenAddr) c = cron.New() } @@ -37,7 +45,7 @@ func initJobs() error { log := log.WithField("job", "core") log.Infoln("Reading jobs...") - err := filepath.Walk("jobs.d", func(path string, info os.FileInfo, err error) error { + err := filepath.Walk(config.JobConfigsPath, func(path string, info os.FileInfo, err error) error { if err != nil { return err } diff --git a/make.bat b/make.bat index d121840..d8d144b 100644 --- a/make.bat +++ b/make.bat @@ -1 +1 @@ -go build -ldflags "-s -w -H windowsgui" \ No newline at end of file +go build -ldflags "-s -w -H windowsgui" -buildmode=pie -trimpath