forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txnpruner.go
45 lines (38 loc) · 1.13 KB
/
txnpruner.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package txnpruner
import (
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/worker/v3"
jworker "github.com/DavinZhang/juju/worker"
)
var logger = loggo.GetLogger("juju.worker.txnpruner")
// TransactionPruner defines the interface for types capable of
// pruning transactions.
type TransactionPruner interface {
MaybePruneTransactions() error
}
// New returns a worker which periodically prunes the data for
// completed transactions.
func New(tp TransactionPruner, interval time.Duration, clock clock.Clock) worker.Worker {
return jworker.NewSimpleWorker(func(stopCh <-chan struct{}) error {
for {
select {
case <-clock.After(interval):
logger.Infof("starting txn pruner")
start := time.Now()
err := tp.MaybePruneTransactions()
if err != nil {
return errors.Annotate(err, "pruning failed, txnpruner stopping")
}
elapsed := time.Since(start)
logger.Infof("txn pruner completed in %d seconds", elapsed/time.Second)
case <-stopCh:
return nil
}
}
})
}