diff --git a/core/checkin.go b/core/checkin.go index 47486549..8f9e2ff6 100644 --- a/core/checkin.go +++ b/core/checkin.go @@ -39,8 +39,8 @@ func (c *Checkin) Select() *types.Checkin { // Routine for checking if the last Checkin was within its interval func CheckinRoutine(checkin database.Checkiner) { - c := checkin.Object() - if c.Hits().Last() == nil { + c := checkin.Model() + if checkin.Hits().Last() == nil { return } reCheck := c.Period() @@ -53,11 +53,11 @@ CheckinLoop: break CheckinLoop case <-time.After(reCheck): log.Infoln(fmt.Sprintf("Checkin %v is expected at %v, checking every %v", c.Name, utils.FormatDuration(c.Expected()), utils.FormatDuration(c.Period()))) - if c.Expected().Seconds() <= 0 { - issue := fmt.Sprintf("Checkin %v is failing, no request since %v", c.Name, c.Hits().Last().CreatedAt) + if c.Expected() <= 0 { + issue := fmt.Sprintf("Checkin %v is failing, no request since %v", c.Name, checkin.Hits().Last().CreatedAt) log.Errorln(issue) - CreateCheckinFailure(c) + CreateCheckinFailure(checkin) } reCheck = c.Period() } @@ -71,8 +71,8 @@ func (c *Checkin) String() string { } func CreateCheckinFailure(checkin database.Checkiner) (int64, error) { - c := checkin.Object() - service := c.Service() + c := checkin.Model() + service := checkin.Service() c.Failing = true fail := &types.Failure{ Issue: fmt.Sprintf("Checkin %v was not reported %v ago, it expects a request every %v", c.Name, utils.FormatDuration(c.Expected()), utils.FormatDuration(c.Period())), diff --git a/database/checkins.go b/database/checkins.go index f7022f99..2177b220 100644 --- a/database/checkins.go +++ b/database/checkins.go @@ -18,7 +18,7 @@ type Checkiner interface { Hits() *CheckinHitObj Failures() *FailureObj Model() *types.Checkin - Object() *CheckinObj + Service() *ServiceObj } func (c *CheckinObj) BeforeCreate() (err error) { @@ -67,11 +67,11 @@ func AllCheckins() []*CheckinObj { func (s *CheckinObj) Service() *ServiceObj { var srv *types.Service - q := database.Checkins().Where("service = ?", s.ServiceId) + q := database.Services().Where("id = ?", s.ServiceId) q.Find(&srv) return &ServiceObj{ Service: srv, - o: wrapObject(srv.Id, srv, q), + o: wrapObject(s.ServiceId, srv, q), } } @@ -90,10 +90,6 @@ func (c *CheckinObj) Model() *types.Checkin { return c.Checkin } -func (c *CheckinObj) Object() *CheckinObj { - return c -} - // Period will return the duration of the Checkin interval func (c *CheckinObj) Period() time.Duration { duration, _ := time.ParseDuration(fmt.Sprintf("%vs", c.Interval)) diff --git a/types/checkin.go b/types/checkin.go index 192d3cdb..fff7e431 100644 --- a/types/checkin.go +++ b/types/checkin.go @@ -52,6 +52,16 @@ func (c *CheckinHit) BeforeCreate() (err error) { return } +func (s *Checkin) Expected() time.Duration { + last := s.LastHit + now := time.Now().UTC() + return time.Duration(now.Second() - last.Second()) +} + +func (s *Checkin) Period() time.Duration { + return time.Duration(s.Interval) * time.Second +} + // Start will create a channel for the checkin checking go routine func (s *Checkin) Start() { s.Running = make(chan bool)