github.com/Axway/agent-sdk@v1.1.101/pkg/config/statusconfig.go (about) 1 package config 2 3 import ( 4 "time" 5 6 "github.com/Axway/agent-sdk/pkg/cmd/properties" 7 ) 8 9 // StatusConfig - Interface for status config 10 type StatusConfig interface { 11 GetPort() int 12 GetHealthCheckPeriod() time.Duration 13 GetHealthCheckInterval() time.Duration 14 ValidateCfg() error 15 } 16 17 // StatusConfiguration - 18 type StatusConfiguration struct { 19 StatusConfig 20 Port int `config:"port"` 21 HealthCheckPeriod time.Duration `config:"healthCheckPeriod"` 22 HealthCheckInterval time.Duration `config:"healthCheckInterval"` // this for binary agents only 23 } 24 25 // NewStatusConfig - create a new status config 26 func NewStatusConfig() StatusConfig { 27 return &StatusConfiguration{ 28 Port: 8989, 29 HealthCheckPeriod: 3 * time.Minute, 30 HealthCheckInterval: 30 * time.Second, 31 } 32 } 33 34 // GetPort - Returns the status port 35 func (a *StatusConfiguration) GetPort() int { 36 return a.Port 37 } 38 39 // GetHealthCheckPeriod - Returns the timeout before exiting discovery agent 40 func (a *StatusConfiguration) GetHealthCheckPeriod() time.Duration { 41 return a.HealthCheckPeriod 42 } 43 44 // GetHealthCheckInterval - Returns the interval between running periodic health checks (binary agents only) 45 func (a *StatusConfiguration) GetHealthCheckInterval() time.Duration { 46 return a.HealthCheckInterval 47 } 48 49 const ( 50 pathPort = "status.port" 51 pathHealthcheckPeriod = "status.healthCheckPeriod" 52 pathHealthcheckInterval = "status.healthCheckInterval" 53 ) 54 55 // AddStatusConfigProperties - Adds the command properties needed for Status Config 56 func AddStatusConfigProperties(props properties.Properties) { 57 props.AddIntProperty(pathPort, 8989, "The port that will serve the status endpoints") 58 props.AddDurationProperty(pathHealthcheckPeriod, 3*time.Minute, "Time in minutes allotted for services to be ready before exiting discovery agent") 59 props.AddDurationProperty(pathHealthcheckInterval, 30*time.Second, "Time between running periodic health checker. Can be between 30 seconds and 5 minutes (binary agents only)") 60 props.AddBoolFlag("status", "Get the status of all the Health Checks") 61 } 62 63 // ParseStatusConfig - Parses the Status Config values form teh command line 64 func ParseStatusConfig(props properties.Properties) (StatusConfig, error) { 65 cfg := &StatusConfiguration{ 66 Port: props.IntPropertyValue(pathPort), 67 HealthCheckPeriod: props.DurationPropertyValue(pathHealthcheckPeriod), 68 HealthCheckInterval: props.DurationPropertyValue(pathHealthcheckInterval), 69 } 70 return cfg, nil 71 } 72 73 // ValidateCfg - Validates the config, implementing IConfigInterface 74 func (a *StatusConfiguration) ValidateCfg() error { 75 mins := a.GetHealthCheckPeriod().Minutes() 76 if mins < 1 || mins > 5 { 77 return ErrStatusHealthCheckPeriod 78 } 79 80 secs := a.GetHealthCheckInterval().Seconds() 81 if secs < 30 || secs > 300 { 82 return ErrStatusHealthCheckInterval 83 } 84 return nil 85 }