statping/types/checkins/routine.go

48 lines
1.1 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package checkins
import (
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
"github.com/statping/statping/utils"
2020-03-04 10:29:00 +00:00
"time"
)
2020-04-15 02:04:50 +00:00
var log = utils.Log.WithField("type", "checkin")
// checkinRoutine for checking if the last Checkin was within its interval
func (c *Checkin) checkinRoutine() {
2020-03-04 10:29:00 +00:00
reCheck := c.Period()
2020-03-04 10:29:00 +00:00
CheckinLoop:
for {
select {
case <-c.Running:
2020-06-10 17:46:32 +00:00
log.Infoln(fmt.Sprintf("Stopping checkin routine: %s", c.Name))
2020-03-04 10:29:00 +00:00
c.Failing = false
break CheckinLoop
case <-time.After(reCheck):
lastHit := c.LastHit()
ago := utils.Now().Sub(lastHit.CreatedAt)
log.Infoln(fmt.Sprintf("Checkin '%s' expects a request every %s last request was %s ago", c.Name, c.Period(), utils.DurationReadable(ago)))
if ago.Seconds() > c.Period().Seconds() {
issue := fmt.Sprintf("Checkin expects a request every %d seconds", c.Interval)
log.Warnln(issue)
2020-03-04 10:29:00 +00:00
fail := &failures.Failure{
2020-04-16 17:32:54 +00:00
Issue: issue,
Method: "checkin",
Service: c.ServiceId,
PingTime: ago.Milliseconds(),
2020-03-04 10:29:00 +00:00
}
if err := c.CreateFailure(fail); err != nil {
log.Errorln(err)
}
2020-03-04 10:29:00 +00:00
}
reCheck = c.Period()
}
}
}