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"
|
2018-03-20 19:21:39 +00:00
|
|
|
"github.com/animenotifier/arn"
|
2017-06-25 15:47:48 +00:00
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
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-03-21 04:15:03 +00:00
|
|
|
"test": 1 * time.Hour,
|
|
|
|
"twist": 4 * time.Hour,
|
2018-03-20 19:21:39 +00:00
|
|
|
// "refresh-episodes": 10 * time.Hour,
|
|
|
|
// "refresh-osu": 12 * time.Hour,
|
|
|
|
// "sync-anime": 12 * time.Hour,
|
|
|
|
// "sync-shoboi": 24 * 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()
|
|
|
|
mainLog.AddOutput(os.Stdout)
|
|
|
|
mainLog.AddOutput(log.File(path.Join(logsPath, "scheduler.log")))
|
|
|
|
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()
|
|
|
|
jobLog.AddOutput(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
|
2017-06-25 17:14:50 +00:00
|
|
|
schedulerLog.Info("Starting " + jobColor(jobName))
|
|
|
|
|
|
|
|
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 {
|
2017-06-25 17:29:12 +00:00
|
|
|
schedulerLog.Error("Error starting job", jobColor(jobName), err)
|
2017-06-25 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
|
|
|
|
if err != nil {
|
2017-06-25 17:29:12 +00:00
|
|
|
schedulerLog.Error("Job exited with error", jobColor(jobName), err)
|
2017-06-25 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
schedulerLog.Info("Finished " + jobColor(jobName))
|
|
|
|
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
|
|
|
|
}
|