Skip to content

Commit 109e0bc

Browse files
MendaxxDevPJB3005
andauthored
exponential backoff for admin logs db update (space-wizards#32865)
* exponential backoff for admin logs db update * Update Content.Server/Database/ServerDbBase.cs --------- Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>
1 parent 7e0e641 commit 109e0bc

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

‎Content.Server/Database/ServerDbBase.cs‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,41 @@ public async Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel)
875875

876876
public async Task AddAdminLogs(List<AdminLog> logs)
877877
{
878+
const int maxRetryAttempts = 5;
879+
var initialRetryDelay = TimeSpan.FromSeconds(5);
880+
878881
DebugTools.Assert(logs.All(x => x.RoundId > 0), "Adding logs with invalid round ids.");
879-
await using var db = await GetDb();
880-
db.DbContext.AdminLog.AddRange(logs);
881-
await db.DbContext.SaveChangesAsync();
882+
883+
var attempt = 0;
884+
var retryDelay = initialRetryDelay;
885+
886+
while (attempt < maxRetryAttempts)
887+
{
888+
try
889+
{
890+
await using var db = await GetDb();
891+
db.DbContext.AdminLog.AddRange(logs);
892+
await db.DbContext.SaveChangesAsync();
893+
_opsLog.Debug($"Successfully saved {logs.Count} admin logs.");
894+
break;
895+
}
896+
catch (Exception ex)
897+
{
898+
attempt += 1;
899+
_opsLog.Error($"Attempt {attempt} failed to save logs: {ex}");
900+
901+
if (attempt >= maxRetryAttempts)
902+
{
903+
_opsLog.Error($"Max retry attempts reached. Failed to save {logs.Count} admin logs.");
904+
return;
905+
}
906+
907+
_opsLog.Warning($"Retrying in {retryDelay.TotalSeconds} seconds...");
908+
await Task.Delay(retryDelay);
909+
910+
retryDelay *= 2;
911+
}
912+
}
882913
}
883914

884915
protected abstract IQueryable<AdminLog> StartAdminLogsQuery(ServerDbContext db, LogFilter? filter = null);

0 commit comments

Comments
 (0)