github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/utils/clock"
    11  
    12  	"github.com/juju/juju/worker"
    13  )
    14  
    15  // TransactionPruner defines the interface for types capable of
    16  // pruning transactions.
    17  type TransactionPruner interface {
    18  	MaybePruneTransactions() error
    19  }
    20  
    21  // New returns a worker which periodically prunes the data for
    22  // completed transactions.
    23  func New(tp TransactionPruner, interval time.Duration, clock clock.Clock) worker.Worker {
    24  	return worker.NewSimpleWorker(func(stopCh <-chan struct{}) error {
    25  		for {
    26  			select {
    27  			case <-clock.After(interval):
    28  				err := tp.MaybePruneTransactions()
    29  				if err != nil {
    30  					return errors.Annotate(err, "pruning failed, txnpruner stopping")
    31  				}
    32  			case <-stopCh:
    33  				return nil
    34  			}
    35  		}
    36  	})
    37  }