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 <pieterjan.briers@gmail.com>
This commit is contained in:
@@ -875,10 +875,41 @@ INSERT INTO player_round (players_id, rounds_id) VALUES ({players[player]}, {id}
|
|||||||
|
|
||||||
public async Task AddAdminLogs(List<AdminLog> logs)
|
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.");
|
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);
|
var attempt = 0;
|
||||||
await db.DbContext.SaveChangesAsync();
|
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);
|
protected abstract IQueryable<AdminLog> StartAdminLogsQuery(ServerDbContext db, LogFilter? filter = null);
|
||||||
|
|||||||
Reference in New Issue
Block a user