github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 PrometheusRegisterer prometheus.Registerer 67 } 68 69 // Validate returns an error if the configuration contains invalid information 70 // or missing resources. 71 func (config ManagerConfig) Validate() error { 72 if config.Secretary == nil { 73 return errors.NotValidf("nil Secretary") 74 } 75 if config.Store == nil { 76 return errors.NotValidf("nil Store") 77 } 78 if config.Logger == nil { 79 return errors.NotValidf("nil Logger") 80 } 81 if config.Clock == nil { 82 return errors.NotValidf("nil Clock") 83 } 84 if config.MaxSleep <= 0 { 85 return errors.NotValidf("non-positive MaxSleep") 86 } 87 // TODO: make the PrometheusRegisterer required when we no longer 88 // have state workers managing leases. 89 return nil 90 }