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-06-27 22:16:45 +00:00
|
|
|
"active-users": 1 * time.Minute,
|
2017-06-29 18:52:30 +00:00
|
|
|
"forum-activity": 1 * time.Minute,
|
2017-06-30 19:52:11 +00:00
|
|
|
"anime-ratings": 10 * time.Minute,
|
|
|
|
"airing-anime": 10 * time.Minute,
|
2017-07-07 10:22:43 +00:00
|
|
|
"statistics": 15 * time.Minute,
|
2017-06-30 23:16:25 +00:00
|
|
|
"popular-anime": 20 * time.Minute,
|
2017-06-30 18:00:56 +00:00
|
|
|
"avatars": 30 * time.Minute,
|
2017-07-02 13:11:42 +00:00
|
|
|
"sync-shoboi": 8 * time.Hour,
|
|
|
|
"refresh-episodes": 10 * time.Hour,
|
2017-06-27 22:16:45 +00:00
|
|
|
"refresh-track-titles": 10 * time.Hour,
|
2017-07-02 13:11:42 +00:00
|
|
|
"refresh-osu": 12 * time.Hour,
|
2017-06-27 22:16:45 +00:00
|
|
|
"sync-anime": 12 * time.Hour,
|
|
|
|
"search-index": 12 * 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
|
|
|
|
}
|