github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/txnpruner/txnpruner.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package txnpruner 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 11 "github.com/juju/juju/worker" 12 ) 13 14 // TransactionPruner defines the interface for types capable of 15 // pruning transactions. 16 type TransactionPruner interface { 17 MaybePruneTransactions() error 18 } 19 20 // New returns a worker which periodically prunes the data for 21 // completed transactions. 22 func New(tp TransactionPruner, interval time.Duration) worker.Worker { 23 return worker.NewSimpleWorker(func(stopCh <-chan struct{}) error { 24 // Use a timer rather than a ticker because pruning could 25 // sometimes take a while and we don't want pruning attempts 26 // to occur back-to-back. 27 timer := time.NewTimer(interval) 28 defer timer.Stop() 29 for { 30 select { 31 case <-timer.C: 32 err := tp.MaybePruneTransactions() 33 if err != nil { 34 return errors.Annotate(err, "pruning failed, txnpruner stopping") 35 } 36 timer.Reset(interval) 37 case <-stopCh: 38 return nil 39 } 40 } 41 }) 42 }