2017-06-25 15:47:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-06-25 17:14:50 +00:00
|
|
|
"os/exec"
|
2017-06-25 15:47:48 +00:00
|
|
|
"os/signal"
|
|
|
|
"path"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2017-06-25 17:14:50 +00:00
|
|
|
"github.com/aerogo/log"
|
2019-04-23 05:45:17 +00:00
|
|
|
"github.com/akyoto/color"
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-06-25 15:47:48 +00:00
|
|
|
)
|
|
|
|
|
2017-06-25 17:14:50 +00:00
|
|
|
var colorPool = []*color.Color{
|
2017-06-25 17:29:12 +00:00
|
|
|
color.New(color.FgHiGreen),
|
|
|
|
color.New(color.FgHiYellow),
|
|
|
|
color.New(color.FgHiBlue),
|
|
|
|
color.New(color.FgHiMagenta),
|
|
|
|
color.New(color.FgHiCyan),
|
2017-06-25 17:14:50 +00:00
|
|
|
color.New(color.FgGreen),
|
|
|
|
}
|
|
|
|
|
2017-06-25 15:47:48 +00:00
|
|
|
var jobs = map[string]time.Duration{
|
2018-03-20 19:21:39 +00:00
|
|
|
"anime-ratings": 10 * time.Minute,
|
2018-12-06 03:00:39 +00:00
|
|
|
"twist": 2 * time.Hour,
|
2018-12-06 03:05:29 +00:00
|
|
|
"refresh-games": 6 * time.Hour,
|
2017-06-25 15:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Start all jobs defined in the map above
|
|
|
|
startJobs()
|
|
|
|
|
|
|
|
// Wait for program termination
|
|
|
|
wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func startJobs() {
|
2017-06-25 17:14:50 +00:00
|
|
|
// Log paths
|
2018-03-20 19:21:39 +00:00
|
|
|
logsPath := path.Join(arn.Root, "logs")
|
|
|
|
jobLogsPath := path.Join(arn.Root, "logs", "jobs")
|
2017-06-25 17:14:50 +00:00
|
|
|
os.Mkdir(jobLogsPath, 0777)
|
|
|
|
|
|
|
|
// Scheduler log
|
|
|
|
mainLog := log.New()
|
2019-05-08 09:05:30 +00:00
|
|
|
mainLog.AddWriter(os.Stdout)
|
|
|
|
mainLog.AddWriter(log.File(path.Join(logsPath, "scheduler.log")))
|
2017-06-25 17:14:50 +00:00
|
|
|
schedulerLog := mainLog
|
|
|
|
|
|
|
|
// Color index
|
|
|
|
colorIndex := 0
|
|
|
|
|
|
|
|
// Start each job
|
2017-06-25 15:47:48 +00:00
|
|
|
for job, interval := range jobs {
|
2017-06-25 17:14:50 +00:00
|
|
|
jobName := job
|
2017-06-25 15:47:48 +00:00
|
|
|
jobInterval := interval
|
2018-03-20 19:21:39 +00:00
|
|
|
executable := path.Join(arn.Root, "jobs", jobName, jobName)
|
2017-06-25 17:14:50 +00:00
|
|
|
jobColor := colorPool[colorIndex].SprintFunc()
|
|
|
|
|
|
|
|
jobLog := log.New()
|
2019-05-08 09:05:30 +00:00
|
|
|
jobLog.AddWriter(log.File(path.Join(jobLogsPath, jobName+".log")))
|
2017-06-25 15:47:48 +00:00
|
|
|
|
2017-06-25 17:14:50 +00:00
|
|
|
fmt.Printf("Registered job %s for execution every %v\n", jobColor(jobName), interval)
|
2017-06-25 15:47:48 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(jobInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
2017-06-25 17:29:12 +00:00
|
|
|
// Wait for the given interval first
|
|
|
|
<-ticker.C
|
|
|
|
|
|
|
|
// Now start
|
2019-05-08 09:05:30 +00:00
|
|
|
schedulerLog.Info("Starting %s", jobColor(jobName))
|
2017-06-25 17:14:50 +00:00
|
|
|
|
|
|
|
cmd := exec.Command(executable)
|
|
|
|
cmd.Stdout = jobLog
|
|
|
|
cmd.Stderr = jobLog
|
|
|
|
|
2018-03-20 19:21:39 +00:00
|
|
|
err := cmd.Start()
|
2017-06-25 17:14:50 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-05-08 09:05:30 +00:00
|
|
|
schedulerLog.Error("Error starting job %s %v", jobColor(jobName), err)
|
2017-06-25 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
|
|
|
|
if err != nil {
|
2019-05-08 09:05:30 +00:00
|
|
|
schedulerLog.Error("Job %s exited with error %v", jobColor(jobName), err)
|
2017-06-25 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 09:05:30 +00:00
|
|
|
schedulerLog.Info("Finished %s", jobColor(jobName))
|
2017-06-25 17:14:50 +00:00
|
|
|
jobLog.Info("--------------------------------------------------------------------------------")
|
2017-06-25 15:47:48 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-06-25 17:14:50 +00:00
|
|
|
|
|
|
|
colorIndex = (colorIndex + 1) % len(colorPool)
|
2017-06-25 15:47:48 +00:00
|
|
|
}
|
2017-06-25 17:14:50 +00:00
|
|
|
|
|
|
|
// Finished job registration
|
|
|
|
println("--------------------------------------------------------------------------------")
|
2017-06-25 15:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func wait() {
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-stop
|
|
|
|
}
|