-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
57 lines (43 loc) · 1.24 KB
/
cron.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
package reconcile
import (
"context"
"time"
"github.com/caapim/layer7-operator/pkg/util"
"github.com/go-co-op/gocron"
)
var s = gocron.NewScheduler(time.Local)
var syncCache = util.NewSyncCache(3 * time.Second)
func Jobs(ctx context.Context, params Params) error {
registerJobs(ctx, params)
for _, j := range s.Jobs() {
for _, t := range j.Tags() {
params.Log.V(2).Info("starting job", "job", t, "namespace", params.Instance.Namespace)
err := s.RunByTag(t)
if err != nil {
params.Log.V(2).Info("no job with given tag", "job", t, "namespace", params.Instance.Namespace)
}
}
}
if !s.IsRunning() {
s.StartAsync()
}
return nil
}
func registerJobs(ctx context.Context, params Params) {
s.TagsUnique()
portalSyncInterval := 10
if params.Instance.Spec.SyncIntervalSeconds != 0 {
portalSyncInterval = params.Instance.Spec.SyncIntervalSeconds
}
_, err := s.Every(portalSyncInterval).Seconds().Tag(params.Instance.Name+"-sync-portal").Do(syncPortal, ctx, params)
if err != nil {
params.Log.V(2).Info("portal sync job already registered", "name", params.Instance.Name, "namespace", params.Instance.Namespace)
}
}
func removeJob(tag string) error {
err := s.RemoveByTag(tag)
if err != nil {
return err
}
return nil
}