forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
57 lines (44 loc) · 1.32 KB
/
worker.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package statushistorypruner
import (
"time"
"github.com/juju/errors"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/catacomb"
"github.com/juju/juju/api/base"
"github.com/juju/juju/api/controller/statushistory"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/worker/pruner"
)
// logger is here to stop the desire of creating a package level logger.
// Don't do this, instead pass one through as config to the worker.
type logger interface{}
var _ logger = struct{}{}
// Worker prunes status history records at regular intervals.
type Worker struct {
pruner.PrunerWorker
}
// NewClient returns a new status history facade.
func NewClient(caller base.APICaller) pruner.Facade {
return statushistory.NewClient(caller)
}
func (w *Worker) loop() error {
return w.Work(func(config *config.Config) (time.Duration, uint) {
return config.MaxStatusHistoryAge(), config.MaxStatusHistorySizeMB()
})
}
// New creates a new status history pruner.
func New(conf pruner.Config) (worker.Worker, error) {
if err := conf.Validate(); err != nil {
return nil, errors.Trace(err)
}
w := &Worker{
pruner.New(conf),
}
err := catacomb.Invoke(catacomb.Plan{
Site: w.Catacomb(),
Work: w.loop,
})
return w, errors.Trace(err)
}