127 lines
2.7 KiB
Go
Raw Normal View History

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"
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{
2017-10-11 08:37:33 +00:00
"anime-ratings": 10 * time.Minute,
"avatars": 1 * time.Hour,
"test": 1 * time.Hour,
"twist": 2 * time.Hour,
"search-index": 2 * time.Hour,
"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
// Get the directory the executable is in
2017-06-25 15:47:48 +00:00
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
2017-06-25 17:14:50 +00:00
// Log paths
logsPath := path.Join(root, "../", "logs")
jobLogsPath := path.Join(root, "../", "logs", "jobs")
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
2017-06-25 17:14:50 +00:00
executable := path.Join(root, jobName, jobName)
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()
2017-06-25 17:14:50 +00:00
var err error
2017-06-25 15:47:48 +00:00
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
err = cmd.Start()
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
}