-
Notifications
You must be signed in to change notification settings - Fork 66
/
proplogcleaner.go
53 lines (47 loc) · 1.34 KB
/
proplogcleaner.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
package modules
import (
"context"
"time"
"github.com/filecoin-project/boost/db"
logging "github.com/ipfs/go-log/v2"
"go.uber.org/fx"
)
var dplcLog = logging.Logger("dplc")
// Boost inserts a row into the DB for each deal proposal accepted or rejected.
// This method periodically cleans up the rows.
func HandleProposalLogCleaner(duration time.Duration) func(lc fx.Lifecycle, plDB *db.ProposalLogsDB) {
return func(lc fx.Lifecycle, plDB *db.ProposalLogsDB) {
var cancel context.CancelFunc
var cleanerCtx context.Context
run := func() {
frequency := time.Minute
dplcLog.Debugf("Starting deal proposal log cleaner to clean up logs older than %s every %s", duration, frequency)
timer := time.NewTicker(frequency)
defer timer.Stop()
for {
select {
case <-cleanerCtx.Done():
return
case <-timer.C:
count, err := plDB.DeleteOlderThan(cleanerCtx, time.Now().Add(-duration))
if err == nil {
dplcLog.Debugf("Deleted %d deal proposal logs older than %s", count, duration)
} else {
dplcLog.Warnf("Failed to delete old deal proposal logs: %s", err)
}
}
}
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
cleanerCtx, cancel = context.WithCancel(ctx)
go run()
return nil
},
OnStop: func(ctx context.Context) error {
cancel()
return nil
},
})
}
}