/
schedulers.go
63 lines (46 loc) · 1.32 KB
/
schedulers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package schedulers
import (
"net/http"
"time"
"github.com/rootsami/terradrift/pkg/config"
log "github.com/sirupsen/logrus"
"github.com/go-co-op/gocron"
)
// The whole idea behind the schedular is to invoke all necessary api calls to the running server.
// It doesn't have to be tightly coupled with the server
// It always has to have the ability to be taken outside of the server to be ran seperately
// in case we decided to move the schedulars to a seperate app
func PullScheduler(host string, interval int) error {
url := host + "/api/sync"
job := gocron.NewScheduler(time.UTC)
_, err := job.Every(interval).Seconds().Do(apiCaller, url)
if err != nil {
return err
}
job.StartAsync()
return nil
}
func ScanScheduler(host, worksapce, configPath string, interval int) error {
stacks, err := config.ConfigLoader(worksapce, configPath)
if err != nil {
log.Fatalf("error loading config file: %s", err)
}
for _, s := range stacks.Stacks {
url := host + "/api/plan?stack=" + s.Name
job := gocron.NewScheduler(time.UTC)
_, err := job.Every(interval).Seconds().Do(apiCaller, url)
if err != nil {
log.WithFields(log.Fields{"stack": s.Name}).Error(err)
return err
}
job.StartAsync()
}
return nil
}
func apiCaller(url string) error {
_, err := http.Get(url)
if err != nil {
return err
}
return nil
}