github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/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  	"github.com/juju/worker/v3"
    11  	"github.com/juju/worker/v3/catacomb"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/api/controller/statushistory"
    15  	"github.com/juju/juju/environs/config"
    16  	"github.com/juju/juju/worker/pruner"
    17  )
    18  
    19  // logger is here to stop the desire of creating a package level logger.
    20  // Don't do this, instead pass one through as config to the worker.
    21  type logger interface{}
    22  
    23  var _ logger = struct{}{}
    24  
    25  // Worker prunes status history records at regular intervals.
    26  type Worker struct {
    27  	pruner.PrunerWorker
    28  }
    29  
    30  // NewClient returns a new status history facade.
    31  func NewClient(caller base.APICaller) pruner.Facade {
    32  	return statushistory.NewClient(caller)
    33  }
    34  
    35  func (w *Worker) loop() error {
    36  	return w.Work(func(config *config.Config) (time.Duration, uint) {
    37  		return config.MaxStatusHistoryAge(), config.MaxStatusHistorySizeMB()
    38  	})
    39  }
    40  
    41  // New creates a new status history pruner.
    42  func New(conf pruner.Config) (worker.Worker, error) {
    43  	if err := conf.Validate(); err != nil {
    44  		return nil, errors.Trace(err)
    45  	}
    46  
    47  	w := &Worker{
    48  		pruner.New(conf),
    49  	}
    50  
    51  	err := catacomb.Invoke(catacomb.Plan{
    52  		Site: w.Catacomb(),
    53  		Work: w.loop,
    54  	})
    55  
    56  	return w, errors.Trace(err)
    57  }