mirror of
https://github.com/nxshock/gron.git
synced 2024-11-27 03:41:00 +05:00
More changes
* new project name is gron * add config file support * add ability to disable webui * makefile creates position independent executable
This commit is contained in:
parent
d3b1c75a7b
commit
252b2c7e80
@ -1,4 +1,4 @@
|
|||||||
# go-cron
|
# gron
|
||||||
|
|
||||||
*cron-like job scheduler*
|
*cron-like job scheduler*
|
||||||
|
|
||||||
|
48
config.go
Normal file
48
config.go
Normal file
@ -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
|
||||||
|
}
|
@ -3,15 +3,12 @@ package main
|
|||||||
import formatter "github.com/antonfisher/nested-logrus-formatter"
|
import formatter "github.com/antonfisher/nested-logrus-formatter"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
timeFormat = "02.01.2006 15:04:05"
|
defaultConfigFilePath = "gron.conf"
|
||||||
logFileName = "log.txt"
|
|
||||||
logFilesPath = "logs"
|
|
||||||
listenAddress = "127.0.0.1:9876"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logFormat = &formatter.Formatter{
|
logFormat = &formatter.Formatter{
|
||||||
TimestampFormat: timeFormat,
|
TimestampFormat: config.TimeFormat,
|
||||||
HideKeys: true,
|
HideKeys: true,
|
||||||
NoColors: true,
|
NoColors: true,
|
||||||
TrimMessages: true}
|
TrimMessages: true}
|
||||||
|
3
go.mod
3
go.mod
@ -11,8 +11,9 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/creasty/defaults v1.5.2
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // 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
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||||
)
|
)
|
||||||
|
7
go.sum
7
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/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 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
|
||||||
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
|
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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
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-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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
5
gron.conf
Normal file
5
gron.conf
Normal file
@ -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"
|
@ -11,6 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func httpServer(listenAddress string) {
|
func httpServer(listenAddress string) {
|
||||||
|
if listenAddress == "none" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", handler)
|
http.HandleFunc("/", handler)
|
||||||
http.HandleFunc("/reloadJobs", handleReloadJobs)
|
http.HandleFunc("/reloadJobs", handleReloadJobs)
|
||||||
http.HandleFunc("/shutdown", handleShutdown)
|
http.HandleFunc("/shutdown", handleShutdown)
|
||||||
|
6
job.go
6
job.go
@ -71,10 +71,10 @@ func (j *Job) Run() {
|
|||||||
|
|
||||||
globalMutex.Lock()
|
globalMutex.Lock()
|
||||||
j.CurrentRunningCount++
|
j.CurrentRunningCount++
|
||||||
j.LastStartTime = startTime.Format(timeFormat)
|
j.LastStartTime = startTime.Format(config.TimeFormat)
|
||||||
globalMutex.Unlock()
|
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.Close()
|
||||||
defer jobLogFile.WriteString("\n")
|
defer jobLogFile.WriteString("\n")
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ func (j *Job) Run() {
|
|||||||
|
|
||||||
globalMutex.Lock()
|
globalMutex.Lock()
|
||||||
j.CurrentRunningCount--
|
j.CurrentRunningCount--
|
||||||
j.LastEndTime = endTime.Format(timeFormat)
|
j.LastEndTime = endTime.Format(config.TimeFormat)
|
||||||
j.LastExecutionDuration = endTime.Sub(startTime).Truncate(time.Second).String()
|
j.LastExecutionDuration = endTime.Sub(startTime).Truncate(time.Second).String()
|
||||||
globalMutex.Unlock()
|
globalMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
11
log.go
11
log.go
@ -2,16 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var logFile *os.File
|
var logFile *os.File
|
||||||
|
|
||||||
func initLogFile() {
|
func initLogFile() error {
|
||||||
var err error
|
var err error
|
||||||
logFile, err = os.OpenFile(logFileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
logFile, err = os.OpenFile(config.LogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
16
main.go
16
main.go
@ -13,12 +13,20 @@ import (
|
|||||||
var c *cron.Cron
|
var c *cron.Cron
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
err := os.MkdirAll(logFilesPath, 0644)
|
err := initConfig()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.MkdirAll(config.LogFilesPath, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
initLogFile()
|
err = initLogFile()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
log.SetFormatter(logFormat)
|
log.SetFormatter(logFormat)
|
||||||
//multiWriter := io.MultiWriter(os.Stderr, logFile)
|
//multiWriter := io.MultiWriter(os.Stderr, logFile)
|
||||||
@ -28,7 +36,7 @@ func init() {
|
|||||||
|
|
||||||
initTemplate()
|
initTemplate()
|
||||||
|
|
||||||
go httpServer(listenAddress)
|
go httpServer(config.HttpListenAddr)
|
||||||
|
|
||||||
c = cron.New()
|
c = cron.New()
|
||||||
}
|
}
|
||||||
@ -37,7 +45,7 @@ func initJobs() error {
|
|||||||
log := log.WithField("job", "core")
|
log := log.WithField("job", "core")
|
||||||
|
|
||||||
log.Infoln("Reading jobs...")
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user