From bbf29e946a23e90591d3e59d2228d678778066bc Mon Sep 17 00:00:00 2001 From: Oleksandr Ieremeev Date: Sun, 19 Aug 2018 19:06:59 +0300 Subject: [PATCH] Calculate and sleep checkpoint intervals for services --- core/checker.go | 8 +++++++- types/service.go | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/checker.go b/core/checker.go index 45a62da2..ca349b3e 100644 --- a/core/checker.go +++ b/core/checker.go @@ -40,6 +40,8 @@ func CheckServices() { } func CheckQueue(s *Service, record bool) { + s.Checkpoint = time.Now() + CheckLoop: for { select { @@ -49,7 +51,11 @@ CheckLoop: default: utils.Log(1, fmt.Sprintf("Checking service: %v", s.Name)) ServiceCheck(s, record) - time.Sleep(time.Duration(s.Interval) * time.Second) + // Set next time checkpoint and maybe sleep. + s.Checkpoint = s.Checkpoint.Add(time.Duration(s.Interval) * time.Second) + if sleepDuration := s.Checkpoint.Sub(time.Now()); sleepDuration > 0 { + time.Sleep(sleepDuration) + } continue } } diff --git a/types/service.go b/types/service.go index e49eca19..45afcdbc 100644 --- a/types/service.go +++ b/types/service.go @@ -41,6 +41,7 @@ type Service struct { Failures []*Failure `json:"failures"` Checkins []*Checkin `json:"checkins"` Running chan bool `json:"-"` + Checkpoint time.Time `json:"-"` LastResponse string LastStatusCode int LastOnline time.Time