github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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 "github.com/juju/juju/worker" 11 ) 12 13 // TransactionPruner defines the interface for types capable of 14 // pruning transactions. 15 type TransactionPruner interface { 16 MaybePruneTransactions() error 17 } 18 19 // New returns a worker which periodically prunes the data for 20 // completed transactions. 21 func New(tp TransactionPruner, interval time.Duration) worker.Worker { 22 return worker.NewSimpleWorker(func(stopCh <-chan struct{}) error { 23 // Use a timer rather than a ticker because pruning could 24 // sometimes take a while and we don't want pruning attempts 25 // to occur back-to-back. 26 timer := time.NewTimer(interval) 27 defer timer.Stop() 28 for { 29 select { 30 case <-timer.C: 31 err := tp.MaybePruneTransactions() 32 if err != nil { 33 return errors.Annotate(err, "pruning failed, txnpruner stopping") 34 } 35 timer.Reset(interval) 36 case <-stopCh: 37 return nil 38 } 39 } 40 }) 41 }