Add Flush function to send remaining achievements and call it in saveAchievements

This commit is contained in:
2025-05-24 23:05:37 +02:00
parent 4fbc49c52f
commit 9035a8284c
2 changed files with 18 additions and 0 deletions

View File

@@ -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))

View File

@@ -187,6 +187,7 @@ func saveAchievements(achievements *sync.Map) {
}
return true
})
Flush()
logger.Info("Saved %d achievements", count)
}