From a793476152ff53e481271fe3ba250e97e376fe58 Mon Sep 17 00:00:00 2001 From: Shan Kiyani Date: Sun, 12 Jan 2025 16:32:53 -0500 Subject: [PATCH] fix: dont override checkpoint if list deliveries is empty --- pkg/retry/retry.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index b3a2f56..1dcbe43 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -110,6 +110,7 @@ func (s *Server) handleRetry() http.Handler { // ensure we run the loop at least once for ok := true; ok; ok = (cursor != "" && !found) { // call list deliveries API, first call is intentionally an empty string + // data is returned sorted by created_at in descending order (latest event first) deliveries, res, err := s.github.ListDeliveries(ctx, &github.ListCursorOptions{ Cursor: cursor, PerPage: 100, @@ -144,7 +145,7 @@ func (s *Server) handleRetry() http.Handler { // update the cursor cursor = res.Cursor - // for each failed delivery, redeliver + // for each delivery, load the failed deliveries into a list for later processing for i := 0; i < len(deliveries); i++ { // append to the total events counter totalEventCount += 1 @@ -225,11 +226,20 @@ func (s *Server) handleRetry() http.Handler { } // advance the checkpoint to the first entry read on this run to avoid - // redundant processing - newCheckpoint = firstCheckpoint + // redundant processing, handle edge case where the list deliveries API + // does not return any events + if firstCheckpoint == "" { + logger.WarnContext(ctx, "ListDeliveries request did not return any deliveries, skipping checkpoint update") + } else { + newCheckpoint = firstCheckpoint + + logger.DebugContext(ctx, "updating checkpoint", + "new_checkpoint", newCheckpoint, + ) - s.writeMostRecentCheckpoint(ctx, w, newCheckpoint, prevCheckpoint, now, - totalEventCount, failedEventCount, redeliveredEventCount) + s.writeMostRecentCheckpoint(ctx, w, newCheckpoint, prevCheckpoint, now, + totalEventCount, failedEventCount, redeliveredEventCount) + } logger.InfoContext(ctx, "successful", "code", http.StatusAccepted,