-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
db_sweeper.go
59 lines (48 loc) · 1.05 KB
/
db_sweeper.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
package sweeper
import (
"fmt"
"github.com/goharbor/harbor/src/common/dao"
"time"
)
var dbInit = make(chan int, 1)
var isDBInit = false
// DBSweeper is used to sweep the DB logs
type DBSweeper struct {
duration int
}
// NewDBSweeper is constructor of DBSweeper
func NewDBSweeper(duration int) *DBSweeper {
return &DBSweeper{
duration: duration,
}
}
// Sweep logs
func (dbs *DBSweeper) Sweep() (int, error) {
// DB initialization not completed, waiting
WaitingDBInit()
// Start to sweep logs
before := time.Now().Add(time.Duration(dbs.duration) * oneDay * -1)
count, err := dao.DeleteJobLogsBefore(before)
if err != nil {
return 0, fmt.Errorf("sweep logs in DB failed before %s with error: %s", before, err)
}
return int(count), nil
}
// Duration for sweeping
func (dbs *DBSweeper) Duration() int {
return dbs.duration
}
// WaitingDBInit waiting DB init
func WaitingDBInit() {
if !isDBInit {
<-dbInit
}
}
// PrepareDBSweep invoked after DB init
func PrepareDBSweep() error {
if !isDBInit {
isDBInit = true
dbInit <- 1
}
return nil
}