Remove 'SecondsOrDuration' for check_interval
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
Now requires an explicit duration unit. Eg. 30s
This commit is contained in:
parent
f3f7c215a7
commit
a03f430d0e
30
config.go
30
config.go
@ -13,7 +13,7 @@ var errInvalidConfig = errors.New("Invalid configuration")
|
|||||||
|
|
||||||
// Config type is contains all provided user configuration
|
// Config type is contains all provided user configuration
|
||||||
type Config struct {
|
type Config struct {
|
||||||
CheckInterval SecondsOrDuration `yaml:"check_interval"`
|
CheckInterval time.Duration `yaml:"check_interval"`
|
||||||
DefaultAlertAfter int16 `yaml:"default_alert_after"`
|
DefaultAlertAfter int16 `yaml:"default_alert_after"`
|
||||||
DefaultAlertEvery *int16 `yaml:"default_alert_every"`
|
DefaultAlertEvery *int16 `yaml:"default_alert_every"`
|
||||||
DefaultAlertDown []string `yaml:"default_alert_down"`
|
DefaultAlertDown []string `yaml:"default_alert_down"`
|
||||||
@ -56,34 +56,6 @@ func (cos *CommandOrShell) UnmarshalYAML(unmarshal func(interface{}) error) erro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SecondsOrDuration wraps a duration value for parsing a duration or seconds from YAML
|
|
||||||
// NOTE: This should be removed in favor of only parsing durations once compatibility is broken
|
|
||||||
type SecondsOrDuration struct {
|
|
||||||
value time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// Value returns a duration value
|
|
||||||
func (sod SecondsOrDuration) Value() time.Duration {
|
|
||||||
return sod.value
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalYAML allows unmarshalling a duration value or seconds if an int was provided
|
|
||||||
func (sod *SecondsOrDuration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
||||||
var seconds int64
|
|
||||||
err := unmarshal(&seconds)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
sod.value = time.Second * time.Duration(seconds)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error indicates that we don't have an int
|
|
||||||
err = unmarshal(&sod.value)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid checks config validity and returns true if valid
|
// IsValid checks config validity and returns true if valid
|
||||||
func (config Config) IsValid() (isValid bool) {
|
func (config Config) IsValid() (isValid bool) {
|
||||||
isValid = true
|
isValid = true
|
||||||
|
@ -47,15 +47,15 @@ func TestIntervalParsing(t *testing.T) {
|
|||||||
oneMinute := time.Minute
|
oneMinute := time.Minute
|
||||||
|
|
||||||
// validate top level interval seconds represented as an int
|
// validate top level interval seconds represented as an int
|
||||||
if config.CheckInterval.Value() != oneSecond {
|
if config.CheckInterval != oneSecond {
|
||||||
t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Monitors[0].CheckInterval.Value() != tenSeconds {
|
if config.Monitors[0].CheckInterval != tenSeconds {
|
||||||
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Monitors[1].CheckInterval.Value() != oneMinute {
|
if config.Monitors[1].CheckInterval != oneMinute {
|
||||||
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
main.go
2
main.go
@ -120,6 +120,6 @@ func main() {
|
|||||||
err = checkMonitors(&config)
|
err = checkMonitors(&config)
|
||||||
slog.OnErrPanicf(err, "Error checking monitors")
|
slog.OnErrPanicf(err, "Error checking monitors")
|
||||||
|
|
||||||
time.Sleep(config.CheckInterval.Value())
|
time.Sleep(config.CheckInterval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ type Monitor struct { //nolint:maligned
|
|||||||
// Config values
|
// Config values
|
||||||
AlertAfter int16 `yaml:"alert_after"`
|
AlertAfter int16 `yaml:"alert_after"`
|
||||||
AlertEvery *int16 `yaml:"alert_every"`
|
AlertEvery *int16 `yaml:"alert_every"`
|
||||||
CheckInterval SecondsOrDuration `yaml:"check_interval"`
|
CheckInterval time.Duration `yaml:"check_interval"`
|
||||||
Name string
|
Name string
|
||||||
AlertDown []string `yaml:"alert_down"`
|
AlertDown []string `yaml:"alert_down"`
|
||||||
AlertUp []string `yaml:"alert_up"`
|
AlertUp []string `yaml:"alert_up"`
|
||||||
@ -45,7 +45,7 @@ func (monitor Monitor) ShouldCheck() bool {
|
|||||||
|
|
||||||
sinceLastCheck := time.Since(monitor.lastCheck)
|
sinceLastCheck := time.Since(monitor.lastCheck)
|
||||||
|
|
||||||
return sinceLastCheck >= monitor.CheckInterval.Value()
|
return sinceLastCheck >= monitor.CheckInterval
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check will run the command configured by the Monitor and return a status
|
// Check will run the command configured by the Monitor and return a status
|
||||||
|
@ -45,9 +45,9 @@ func TestMonitorShouldCheck(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
}{
|
}{
|
||||||
{Monitor{}, true, "Empty"},
|
{Monitor{}, true, "Empty"},
|
||||||
{Monitor{lastCheck: timeNow, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "Just checked"},
|
{Monitor{lastCheck: timeNow, CheckInterval: time.Second * 15}, false, "Just checked"},
|
||||||
{Monitor{lastCheck: timeTenSecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "-10s"},
|
{Monitor{lastCheck: timeTenSecAgo, CheckInterval: time.Second * 15}, false, "-10s"},
|
||||||
{Monitor{lastCheck: timeTwentySecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, true, "-20s"},
|
{Monitor{lastCheck: timeTwentySecAgo, CheckInterval: time.Second * 15}, true, "-20s"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
Loading…
Reference in New Issue
Block a user