1
0
mirror of https://github.com/nxshock/gron.git synced 2024-11-27 03:41:00 +05:00
gron/main.go

106 lines
1.6 KiB
Go
Raw Normal View History

2022-03-26 13:23:39 +05:00
package main
import (
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
var c *cron.Cron
func init() {
err := initConfig()
if err != nil {
panic(err)
}
err = os.MkdirAll(config.LogFilesPath, 0644)
2022-03-26 13:23:39 +05:00
if err != nil {
log.Fatalln(err)
}
err = initLogFile()
if err != nil {
log.Fatalln(err)
}
2022-03-26 13:23:39 +05:00
log.SetFormatter(logFormat)
//multiWriter := io.MultiWriter(os.Stderr, logFile)
//log.SetOutput(multiWriter)
log.SetOutput(logFile)
log.SetLevel(log.InfoLevel)
initTemplate()
go httpServer(config.HttpListenAddr)
2022-03-26 13:23:39 +05:00
c = cron.New()
}
2022-03-26 20:20:00 +05:00
func initJobs() error {
2022-03-26 13:23:39 +05:00
log := log.WithField("job", "core")
2022-03-26 20:20:00 +05:00
log.Infoln("Reading jobs...")
err := filepath.Walk(config.JobConfigsPath, func(path string, info os.FileInfo, err error) error {
2022-03-26 13:23:39 +05:00
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) == ".conf" {
job, err := readJob(path)
if err != nil {
return err
}
_, err = c.AddJob(job.Cron, job)
if err != nil {
return err
}
}
return nil
})
if err != nil {
2022-03-26 20:20:00 +05:00
return err
2022-03-26 13:23:39 +05:00
}
if len(c.Entries()) == 0 {
2022-03-26 20:20:00 +05:00
log.Warn("No jobs loaded.")
} else {
log.Infof("Loaded jobs count: %d", len(c.Entries()))
2022-03-26 13:23:39 +05:00
}
2022-03-26 20:20:00 +05:00
return nil
}
func main() {
log := log.WithField("job", "core")
log.Info("Started.")
err := initJobs()
if err != nil {
log.Fatalln(err)
}
2022-03-26 13:23:39 +05:00
c.Start()
intChan := make(chan os.Signal)
signal.Notify(intChan, syscall.SIGTERM)
<-intChan
2022-03-26 20:20:00 +05:00
log.Info("Got stop signal.")
2022-03-26 13:23:39 +05:00
err = logFile.Close()
if err != nil {
2022-03-26 20:20:00 +05:00
log.Fatal(err)
2022-03-26 13:23:39 +05:00
}
}