2022-03-26 13:23:39 +05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-14 14:16:49 +05:00
|
|
|
"io"
|
2022-03-26 13:23:39 +05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-03-28 18:24:23 +05:00
|
|
|
formatter "github.com/antonfisher/nested-logrus-formatter"
|
2022-10-23 10:23:35 +05:00
|
|
|
"github.com/kardianos/service"
|
2022-03-26 13:23:39 +05:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-03-27 12:39:24 +05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:39:24 +05:00
|
|
|
err = initLogFile()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2022-03-26 13:23:39 +05:00
|
|
|
|
2022-03-28 18:24:23 +05:00
|
|
|
log.SetFormatter(&formatter.Formatter{
|
|
|
|
TimestampFormat: config.TimeFormat,
|
|
|
|
HideKeys: true,
|
|
|
|
NoColors: true,
|
|
|
|
TrimMessages: true})
|
2022-05-14 14:16:49 +05:00
|
|
|
|
2022-10-23 10:23:35 +05:00
|
|
|
log.SetOutput(io.MultiWriter(os.Stderr, mainLogFile))
|
2022-03-26 13:23:39 +05:00
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
|
|
|
|
initTemplate()
|
|
|
|
|
2022-03-27 12:39:24 +05:00
|
|
|
go httpServer(config.HttpListenAddr)
|
2022-03-26 13:23:39 +05:00
|
|
|
}
|
|
|
|
|
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...")
|
2022-03-27 12:39:24 +05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-23 10:23:35 +05:00
|
|
|
_, err = kernel.c.AddJob(job.JobConfig.Cron, job)
|
2022-03-26 13:23:39 +05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-23 10:23:35 +05:00
|
|
|
if len(kernel.c.Entries()) == 0 {
|
2022-03-26 20:20:00 +05:00
|
|
|
log.Warn("No jobs loaded.")
|
|
|
|
} else {
|
2022-10-23 10:23:35 +05:00
|
|
|
log.Infof("Loaded jobs count: %d", len(kernel.c.Entries()))
|
2022-03-26 13:23:39 +05:00
|
|
|
}
|
|
|
|
|
2022-03-26 20:20:00 +05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-10-23 10:23:35 +05:00
|
|
|
kernel = NewKernel()
|
2022-03-26 20:20:00 +05:00
|
|
|
|
2022-10-23 10:23:35 +05:00
|
|
|
s, err := service.New(kernel, kernel.svcConfig)
|
2022-03-26 20:20:00 +05:00
|
|
|
if err != nil {
|
2022-10-23 10:23:35 +05:00
|
|
|
log.Fatal(err)
|
2022-03-26 20:20:00 +05:00
|
|
|
}
|
2022-03-26 13:23:39 +05:00
|
|
|
|
2022-10-23 10:23:35 +05:00
|
|
|
logger, err := s.Logger(nil)
|
2022-03-26 13:23:39 +05:00
|
|
|
if err != nil {
|
2022-03-26 20:20:00 +05:00
|
|
|
log.Fatal(err)
|
2022-03-26 13:23:39 +05:00
|
|
|
}
|
2022-10-23 10:23:35 +05:00
|
|
|
|
|
|
|
err = s.Run()
|
|
|
|
if err != nil {
|
2022-11-05 14:07:55 +05:00
|
|
|
_ = logger.Error(err)
|
2022-10-23 10:23:35 +05:00
|
|
|
}
|
2022-03-26 13:23:39 +05:00
|
|
|
}
|