From 9035a8284cfcf7555578469f2efecea9cc90e7e5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 24 May 2025 23:05:37 +0200 Subject: [PATCH] Add Flush function to send remaining achievements and call it in saveAchievements --- dbwriter.go | 17 +++++++++++++++++ main.go | 1 + 2 files changed, 18 insertions(+) diff --git a/dbwriter.go b/dbwriter.go index 44e6341..77158e3 100644 --- a/dbwriter.go +++ b/dbwriter.go @@ -111,6 +111,23 @@ func Save(message NSQMessage) error { return nil } +// Flush sends any remaining achievements in the buffer +func Flush() error { + batchMutex.Lock() + if len(batchBuffer) == 0 { + batchMutex.Unlock() + return nil + } + + batch := make([]NSQMessage, len(batchBuffer)) + copy(batch, batchBuffer) + batchBuffer = batchBuffer[:0] + batchMutex.Unlock() + + logger.Info("Flushing final batch of %d achievements to %s", len(batch), backendEndpoint) + return sendBatch(batch) +} + func sendBatch(batch []NSQMessage) error { logger.Debug("Preparing to send batch of %d achievements", len(batch)) diff --git a/main.go b/main.go index e21319f..b7368a2 100644 --- a/main.go +++ b/main.go @@ -187,6 +187,7 @@ func saveAchievements(achievements *sync.Map) { } return true }) + Flush() logger.Info("Saved %d achievements", count) }