forked from yuval-k/harbor
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sweeper_controller.go
98 lines (81 loc) · 1.86 KB
/
sweeper_controller.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package logger
import (
"context"
"fmt"
"reflect"
"time"
"github.com/goharbor/harbor/src/jobservice/logger/sweeper"
)
const (
oneDay = 24 * time.Hour
)
// SweeperController is an unified sweeper entry and built on top of multiple sweepers.
// It's responsible for starting the configured sweepers.
type SweeperController struct {
context context.Context
sweepers []sweeper.Interface
errChan chan error
}
// NewSweeperController is constructor of controller.
func NewSweeperController(ctx context.Context, sweepers []sweeper.Interface) *SweeperController {
return &SweeperController{
context: ctx,
sweepers: sweepers,
errChan: make(chan error, 1),
}
}
// Sweep logs
func (c *SweeperController) Sweep() (int, error) {
// Start to process errors
go func() {
for {
select {
case err := <-c.errChan:
Error(err)
case <-c.context.Done():
return
}
}
}()
for _, s := range c.sweepers {
go func(sw sweeper.Interface) {
c.startSweeper(sw)
}(s)
}
return 0, nil
}
// Duration = -1 for controller
func (c *SweeperController) Duration() int {
return -1
}
func (c *SweeperController) startSweeper(s sweeper.Interface) {
d := s.Duration()
if d <= 0 {
d = 1
}
// Use the type name as a simple ID
sid := reflect.TypeOf(s).String()
defer Infof("sweeper %s exit", sid)
// First run
go c.doSweeping(sid, s)
// Loop
ticker := time.NewTicker(time.Duration(d) * oneDay)
defer ticker.Stop()
for {
select {
case <-ticker.C:
go c.doSweeping(sid, s)
case <-c.context.Done():
return
}
}
}
func (c *SweeperController) doSweeping(sid string, s sweeper.Interface) {
Debugf("Sweeper %s is under working", sid)
count, err := s.Sweep()
if err != nil {
c.errChan <- fmt.Errorf("sweep logs error in %s at %d: %s", sid, time.Now().Unix(), err)
return
}
Infof("%d outdated log entries are sweepped by sweeper %s", count, sid)
}