github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/lease/config.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lease
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/clock"
    10  	"github.com/juju/errors"
    11  	"github.com/prometheus/client_golang/prometheus"
    12  
    13  	"github.com/juju/juju/core/lease"
    14  )
    15  
    16  // Secretary is responsible for validating the sanity of lease and holder names
    17  // before bothering the manager with them.
    18  type Secretary interface {
    19  
    20  	// CheckLease returns an error if the supplied lease name is not valid.
    21  	CheckLease(key lease.Key) error
    22  
    23  	// CheckHolder returns an error if the supplied holder name is not valid.
    24  	CheckHolder(name string) error
    25  
    26  	// CheckDuration returns an error if the supplied duration is not valid.
    27  	CheckDuration(duration time.Duration) error
    28  }
    29  
    30  // Logger represents the logging methods we use from a loggo.Logger.
    31  type Logger interface {
    32  	Tracef(string, ...interface{})
    33  	Debugf(string, ...interface{})
    34  	Infof(string, ...interface{})
    35  	Warningf(string, ...interface{})
    36  	Errorf(string, ...interface{})
    37  }
    38  
    39  // ManagerConfig contains the resources and information required to create a
    40  // Manager.
    41  type ManagerConfig struct {
    42  
    43  	// Secretary determines validation given a namespace. The
    44  	// secretary returned is responsible for validating lease names
    45  	// and holder names for that namespace.
    46  	Secretary func(namespace string) (Secretary, error)
    47  
    48  	// Store is responsible for recording, retrieving, and expiring leases.
    49  	Store lease.Store
    50  
    51  	// Logger is used to report debugging/status information as the
    52  	// manager runs.
    53  	Logger Logger
    54  
    55  	// Clock is responsible for reporting the passage of time.
    56  	Clock clock.Clock
    57  
    58  	// MaxSleep is the longest time the Manager should sleep before
    59  	// refreshing its store's leases and checking for expiries.
    60  	MaxSleep time.Duration
    61  
    62  	// EntityUUID is the entity that we are running this Manager for. Used for
    63  	// logging purposes.
    64  	EntityUUID string
    65  
    66  	// LogDir is the directory to write a debugging log file in the
    67  	// case that the worker times out waiting to shut down.
    68  	LogDir string
    69  
    70  	PrometheusRegisterer prometheus.Registerer
    71  }
    72  
    73  // Validate returns an error if the configuration contains invalid information
    74  // or missing resources.
    75  func (config ManagerConfig) Validate() error {
    76  	if config.Secretary == nil {
    77  		return errors.NotValidf("nil Secretary")
    78  	}
    79  	if config.Store == nil {
    80  		return errors.NotValidf("nil Store")
    81  	}
    82  	if config.Logger == nil {
    83  		return errors.NotValidf("nil Logger")
    84  	}
    85  	if config.Clock == nil {
    86  		return errors.NotValidf("nil Clock")
    87  	}
    88  	if config.MaxSleep <= 0 {
    89  		return errors.NotValidf("non-positive MaxSleep")
    90  	}
    91  	// TODO: make the PrometheusRegisterer required when we no longer
    92  	// have state workers managing leases.
    93  	return nil
    94  }