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