Skip to content

Commit

Permalink
update penultimate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed Nov 24, 2021
1 parent 0afe649 commit 3701974
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ func heartBeatLoop(window *window.Window) {
}
log.Printf("Sending initial heart beat in %v", initialDelay)
time.Sleep(initialDelay)

penultimate := true
done := false
for {
attempt := 0
done := hb.updateNextBeat()
if !penultimate {
penultimate = hb.updateNextBeat()
} else {
done = true
}

for attempt < maxAttempts {
err := sendHeartbeat(hb.api, hb.nextEvent)
if err == nil {
Expand All @@ -51,12 +57,12 @@ func heartBeatLoop(window *window.Window) {
time.Sleep(attemptDelay)
}
if done {
log.Printf("Sent final heartbeat")
log.Printf("Sent penultimate heartbeat")
return
}

nextEventIn := hb.nextEvent.Sub(time.Now())
if nextEventIn >= 2*time.Hour {
if !penultimate && nextEventIn >= 2*time.Hour {
nextEventIn = nextEventIn - 1*time.Hour
} else {
// 5 minutes to give a bit of leeway
Expand Down Expand Up @@ -84,11 +90,12 @@ func NewHeartbeat(window *window.Window) (*Heartbeat, error) {
return h, nil
}

//updates next heart beat time, returns true if this was the final event
//updates next heart beat time, returns true if will be the final event
func (h *Heartbeat) updateNextBeat() bool {
h.nextEvent = time.Now().Add(interval)
if !h.window.NoWindow && h.nextEvent.After(h.end.Add(-time.Hour)) {
h.nextEvent = h.end
// always wwant an event 1 hour before end
h.nextEvent = h.end.Add(-time.Hour)
return true
}
return false
Expand Down

0 comments on commit 3701974

Please sign in to comment.