Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
exponential backoff for admin logs db update (#32865)
Browse files Browse the repository at this point in the history
* exponential backoff for admin logs db update

* Update Content.Server/Database/ServerDbBase.cs

---------

Co-authored-by: Pieter-Jan Briers <[email protected]>
  • Loading branch information
MendaxxDev and PJB3005 authored Oct 19, 2024
1 parent 7e0e641 commit 109e0bc
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions Content.Server/Database/ServerDbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,41 @@ public async Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel)

public async Task AddAdminLogs(List<AdminLog> 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<AdminLog> StartAdminLogsQuery(ServerDbContext db, LogFilter? filter = null);
Expand Down

0 comments on commit 109e0bc

Please sign in to comment.