Started working on job scheduler

This commit is contained in:
Eduard Urbach 2017-06-25 17:47:48 +02:00
parent e0a4db6c47
commit 8001b30436

57
jobs/main.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"fmt"
"os"
"os/signal"
"path"
"syscall"
"time"
"github.com/fatih/color"
)
var jobs = map[string]time.Duration{
"popular-anime": 1 * time.Second,
}
func main() {
// Start all jobs defined in the map above
startJobs()
// Wait for program termination
wait()
}
func startJobs() {
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
for job, interval := range jobs {
jobInterval := interval
executable := path.Join(root, job, job)
fmt.Printf("Registered job %s for execution every %v\n", color.YellowString(job), interval)
go func() {
ticker := time.NewTicker(jobInterval)
defer ticker.Stop()
for {
fmt.Println(executable)
<-ticker.C
}
}()
}
}
func wait() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
}