github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/dblogpruner/worker.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package dblogpruner 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "gopkg.in/tomb.v1" 11 12 "github.com/juju/juju/state" 13 "github.com/juju/juju/worker" 14 ) 15 16 // LogPruneParams specifies how logs should be pruned. 17 type LogPruneParams struct { 18 MaxLogAge time.Duration 19 MaxCollectionMB int 20 PruneInterval time.Duration 21 } 22 23 const DefaultMaxLogAge = 3 * 24 * time.Hour // 3 days 24 const DefaultMaxCollectionMB = 4 * 1024 // 4 GB 25 const DefaultPruneInterval = 5 * time.Minute 26 27 // NewLogPruneParams returns a LogPruneParams initialised with default 28 // values. 29 func NewLogPruneParams() *LogPruneParams { 30 return &LogPruneParams{ 31 MaxLogAge: DefaultMaxLogAge, 32 MaxCollectionMB: DefaultMaxCollectionMB, 33 PruneInterval: DefaultPruneInterval, 34 } 35 } 36 37 // New returns a worker which periodically wakes up to remove old log 38 // entries stored in MongoDB. This worker is intended to run just 39 // once, on the MongoDB master. 40 func New(st *state.State, params *LogPruneParams) worker.Worker { 41 w := &pruneWorker{ 42 st: st, 43 params: params, 44 } 45 return worker.NewSimpleWorker(w.loop) 46 } 47 48 type pruneWorker struct { 49 st *state.State 50 params *LogPruneParams 51 } 52 53 func (w *pruneWorker) loop(stopCh <-chan struct{}) error { 54 p := w.params 55 for { 56 select { 57 case <-stopCh: 58 return tomb.ErrDying 59 case <-time.After(p.PruneInterval): 60 // TODO(fwereade): 2016-03-17 lp:1558657 61 minLogTime := time.Now().Add(-p.MaxLogAge) 62 err := state.PruneLogs(w.st, minLogTime, p.MaxCollectionMB) 63 if err != nil { 64 return errors.Trace(err) 65 } 66 } 67 } 68 }