From 109e0bcf967b2413f8a94c256f92b653459899bb Mon Sep 17 00:00:00 2001 From: MendaxxDev <153332064+MendaxxDev@users.noreply.github.com> Date: Sat, 19 Oct 2024 12:28:49 +0200 Subject: [PATCH] exponential backoff for admin logs db update (#32865) * exponential backoff for admin logs db update * Update Content.Server/Database/ServerDbBase.cs --------- Co-authored-by: Pieter-Jan Briers --- Content.Server/Database/ServerDbBase.cs | 37 +++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/Content.Server/Database/ServerDbBase.cs b/Content.Server/Database/ServerDbBase.cs index b750c13e212..c85b774e381 100644 --- a/Content.Server/Database/ServerDbBase.cs +++ b/Content.Server/Database/ServerDbBase.cs @@ -875,10 +875,41 @@ public async Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel) public async Task AddAdminLogs(List logs) { + const int maxRetryAttempts = 5; + var initialRetryDelay = TimeSpan.FromSeconds(5); + DebugTools.Assert(logs.All(x => x.RoundId > 0), "Adding logs with invalid round ids."); - await using var db = await GetDb(); - db.DbContext.AdminLog.AddRange(logs); - await db.DbContext.SaveChangesAsync(); + + var attempt = 0; + var retryDelay = initialRetryDelay; + + while (attempt < maxRetryAttempts) + { + try + { + await using var db = await GetDb(); + db.DbContext.AdminLog.AddRange(logs); + await db.DbContext.SaveChangesAsync(); + _opsLog.Debug($"Successfully saved {logs.Count} admin logs."); + break; + } + catch (Exception ex) + { + attempt += 1; + _opsLog.Error($"Attempt {attempt} failed to save logs: {ex}"); + + if (attempt >= maxRetryAttempts) + { + _opsLog.Error($"Max retry attempts reached. Failed to save {logs.Count} admin logs."); + return; + } + + _opsLog.Warning($"Retrying in {retryDelay.TotalSeconds} seconds..."); + await Task.Delay(retryDelay); + + retryDelay *= 2; + } + } } protected abstract IQueryable StartAdminLogsQuery(ServerDbContext db, LogFilter? filter = null);