statping/types/checkins/routine.go

61 lines
1.5 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")
2020-03-04 10:29:00 +00:00
// RecheckCheckinFailure will check if a Service Checkin has been reported yet
func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) {
between := utils.Now().Sub(utils.Now()).Seconds()
if between > float64(c.Interval) {
fmt.Println("rechecking every 15 seconds!")
time.Sleep(15 * time.Second)
guard <- struct{}{}
c.RecheckCheckinFailure(guard)
} else {
fmt.Println("i recovered!!")
}
<-guard
}
// Routine for checking if the last Checkin was within its interval
func (c *Checkin) CheckinRoutine() {
lastHit := c.LastHit()
if lastHit == nil {
return
}
reCheck := c.Period()
CheckinLoop:
for {
select {
case <-c.Running:
log.Infoln(fmt.Sprintf("Stopping checkin routine: %v", c.Name))
c.Failing = false
break CheckinLoop
case <-time.After(reCheck):
2020-04-16 17:32:54 +00:00
log.Infoln(fmt.Sprintf("Checkin '%s' expects a request every %v", c.Name, utils.FormatDuration(c.Period())))
2020-03-04 10:29:00 +00:00
if c.Expected() <= 0 {
2020-04-16 17:32:54 +00:00
issue := fmt.Sprintf("Checkin '%s' is failing, no request since %v", c.Name, lastHit.CreatedAt)
//log.Errorln(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,
Checkin: c.Id,
PingTime: c.Expected().Milliseconds(),
2020-03-04 10:29:00 +00:00
}
c.CreateFailure(fail)
}
reCheck = c.Period()
}
continue
}
}