Improved scheduler

This commit is contained in:
Eduard Urbach 2018-03-20 20:21:39 +01:00
parent 87e8e16d35
commit 56ad815dcc

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/aerogo/log" "github.com/aerogo/log"
"github.com/animenotifier/arn"
"github.com/fatih/color" "github.com/fatih/color"
) )
@ -23,14 +24,13 @@ var colorPool = []*color.Color{
} }
var jobs = map[string]time.Duration{ var jobs = map[string]time.Duration{
"anime-ratings": 10 * time.Minute, "anime-ratings": 10 * time.Minute,
"avatars": 1 * time.Hour, // "test": 1 * time.Hour,
"test": 1 * time.Hour, "twist": 4 * time.Hour,
"twist": 2 * time.Hour, // "refresh-episodes": 10 * time.Hour,
"refresh-episodes": 10 * time.Hour, // "refresh-osu": 12 * time.Hour,
"refresh-osu": 12 * time.Hour, // "sync-anime": 12 * time.Hour,
"sync-anime": 12 * time.Hour, // "sync-shoboi": 24 * time.Hour,
"sync-shoboi": 24 * time.Hour,
} }
func main() { func main() {
@ -42,18 +42,9 @@ func main() {
} }
func startJobs() { func startJobs() {
// Get the directory the executable is in
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
// Log paths // Log paths
logsPath := path.Join(root, "../", "logs") logsPath := path.Join(arn.Root, "logs")
jobLogsPath := path.Join(root, "../", "logs", "jobs") jobLogsPath := path.Join(arn.Root, "logs", "jobs")
os.Mkdir(jobLogsPath, 0777) os.Mkdir(jobLogsPath, 0777)
// Scheduler log // Scheduler log
@ -69,7 +60,7 @@ func startJobs() {
for job, interval := range jobs { for job, interval := range jobs {
jobName := job jobName := job
jobInterval := interval jobInterval := interval
executable := path.Join(root, jobName, jobName) executable := path.Join(arn.Root, "jobs", jobName, jobName)
jobColor := colorPool[colorIndex].SprintFunc() jobColor := colorPool[colorIndex].SprintFunc()
jobLog := log.New() jobLog := log.New()
@ -81,8 +72,6 @@ func startJobs() {
ticker := time.NewTicker(jobInterval) ticker := time.NewTicker(jobInterval)
defer ticker.Stop() defer ticker.Stop()
var err error
for { for {
// Wait for the given interval first // Wait for the given interval first
<-ticker.C <-ticker.C
@ -94,7 +83,7 @@ func startJobs() {
cmd.Stdout = jobLog cmd.Stdout = jobLog
cmd.Stderr = jobLog cmd.Stderr = jobLog
err = cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
schedulerLog.Error("Error starting job", jobColor(jobName), err) schedulerLog.Error("Error starting job", jobColor(jobName), err)