github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/statushistorypruner/worker.go (about)

     1  // Copyright 2015 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  
    11  	"github.com/juju/juju/state"
    12  	"github.com/juju/juju/worker"
    13  )
    14  
    15  // HistoryPrunerParams specifies how history logs should be prunned.
    16  type HistoryPrunerParams struct {
    17  	// TODO(perrito666) We might want to have some sort of limitation of the collection size too.
    18  	MaxLogsPerState int
    19  	PruneInterval   time.Duration
    20  }
    21  
    22  const DefaultMaxLogsPerState = 100
    23  const DefaultPruneInterval = 5 * time.Minute
    24  
    25  // NewHistoryPrunerParams returns a HistoryPrunerParams initialized with default parameter.
    26  func NewHistoryPrunerParams() *HistoryPrunerParams {
    27  	return &HistoryPrunerParams{
    28  		MaxLogsPerState: DefaultMaxLogsPerState,
    29  		PruneInterval:   DefaultPruneInterval,
    30  	}
    31  }
    32  
    33  type pruneHistoryFunc func(*state.State, int) error
    34  
    35  type pruneWorker struct {
    36  	st     *state.State
    37  	params *HistoryPrunerParams
    38  	pruner pruneHistoryFunc
    39  }
    40  
    41  // New returns a worker.Worker for history Pruner.
    42  func New(st *state.State, params *HistoryPrunerParams) worker.Worker {
    43  	w := &pruneWorker{
    44  		st:     st,
    45  		params: params,
    46  		pruner: state.PruneStatusHistory,
    47  	}
    48  	return worker.NewPeriodicWorker(w.doPruning, w.params.PruneInterval, worker.NewTimer)
    49  }
    50  
    51  func (w *pruneWorker) doPruning(stop <-chan struct{}) error {
    52  	err := w.pruner(w.st, w.params.MaxLogsPerState)
    53  	if err != nil {
    54  		return errors.Trace(err)
    55  	}
    56  	return nil
    57  }