/
worker.go
67 lines (57 loc) · 1.55 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
58
59
60
61
62
63
64
65
66
67
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package dblogpruner
import (
"time"
"github.com/juju/errors"
"launchpad.net/tomb"
"github.com/juju/juju/state"
"github.com/juju/juju/worker"
)
// LogPruneParams specifies how logs should be pruned.
type LogPruneParams struct {
MaxLogAge time.Duration
MaxCollectionMB int
PruneInterval time.Duration
}
const DefaultMaxLogAge = 3 * 24 * time.Hour // 3 days
const DefaultMaxCollectionMB = 4 * 1024 // 4 GB
const DefaultPruneInterval = 5 * time.Minute
// NewLogPruneParams returns a LogPruneParams initialised with default
// values.
func NewLogPruneParams() *LogPruneParams {
return &LogPruneParams{
MaxLogAge: DefaultMaxLogAge,
MaxCollectionMB: DefaultMaxCollectionMB,
PruneInterval: DefaultPruneInterval,
}
}
// New returns a worker which periodically wakes up to remove old log
// entries stored in MongoDB. This worker is intended to run just
// once, on the MongoDB master.
func New(st *state.State, params *LogPruneParams) worker.Worker {
w := &pruneWorker{
st: st,
params: params,
}
return worker.NewSimpleWorker(w.loop)
}
type pruneWorker struct {
st *state.State
params *LogPruneParams
}
func (w *pruneWorker) loop(stopCh <-chan struct{}) error {
p := w.params
for {
select {
case <-stopCh:
return tomb.ErrDying
case <-time.After(p.PruneInterval):
minLogTime := time.Now().Add(-p.MaxLogAge)
err := state.PruneLogs(w.st, minLogTime, p.MaxCollectionMB)
if err != nil {
return errors.Trace(err)
}
}
}
}