github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/statushistorypruner/worker.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package statushistorypruner
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"gopkg.in/juju/worker.v1"
    11  	"gopkg.in/juju/worker.v1/catacomb"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/api/statushistory"
    15  	"github.com/juju/juju/environs/config"
    16  	"github.com/juju/juju/worker/pruner"
    17  )
    18  
    19  // Worker prunes status history records at regular intervals.
    20  type Worker struct {
    21  	pruner.PrunerWorker
    22  }
    23  
    24  // NewFacade returns a new status history facade.
    25  func NewFacade(caller base.APICaller) pruner.Facade {
    26  	return statushistory.NewFacade(caller)
    27  }
    28  
    29  func (w *Worker) loop() error {
    30  	return w.Work(func(config *config.Config) (time.Duration, uint) {
    31  		return config.MaxStatusHistoryAge(), config.MaxStatusHistorySizeMB()
    32  	})
    33  }
    34  
    35  // New creates a new status history pruner.
    36  func New(conf pruner.Config) (worker.Worker, error) {
    37  	if err := conf.Validate(); err != nil {
    38  		return nil, errors.Trace(err)
    39  	}
    40  
    41  	w := &Worker{
    42  		pruner.New(conf),
    43  	}
    44  
    45  	err := catacomb.Invoke(catacomb.Plan{
    46  		Site: w.Catacomb(),
    47  		Work: w.loop,
    48  	})
    49  
    50  	return w, errors.Trace(err)
    51  }