Fix: Implement FixPlan part A9, A10, B

This commit is contained in:
Richard
2026-07-10 12:04:27 +02:00
parent 44f48284a2
commit 1787422243
28 changed files with 1025 additions and 79 deletions
@@ -52,4 +52,14 @@ public class JobRepository : IJobRepository
_db.BackgroundJobs.Update(job);
await _db.SaveChangesAsync(ct);
}
public async Task ResetHungJobsAsync(CancellationToken ct = default)
{
var cutoff = DateTime.UtcNow.AddMinutes(-15);
await _db.BackgroundJobs
.Where(j => j.Status == JobStatus.InProgress && j.StartedAt < cutoff)
.ExecuteUpdateAsync(s => s
.SetProperty(j => j.Status, JobStatus.Pending)
.SetProperty(j => j.StartedAt, (DateTime?)null), ct);
}
}
@@ -68,7 +68,7 @@ public class TradeRepository : ITradeRepository
t.TransactionHash = StringHelper.Truncate(t.TransactionHash, 66);
}
foreach (var chunk in tradeList.Chunk(1000))
foreach (var chunk in tradeList.Chunk(500))
{
var sb = new System.Text.StringBuilder("INSERT IGNORE INTO Trades (PlatformTradeId, MarketId, AssetId, Outcome, Side, Price, Size, Amount, ExecutedAt, TransactionHash, TraderId, MarketOutcomeId, DbMarketId, Platform, IsContextEnriched) VALUES ");
var parameters = new List<object>();
@@ -99,8 +99,37 @@ public class TradeRepository : ITradeRepository
parameters.Add(t.IsContextEnriched);
}
var rowsInserted = await _db.Database.ExecuteSqlRawAsync(sb.ToString(), parameters.ToArray(), ct);
_logger.LogInformation("Inserted {RowsInserted} trades into the database.", rowsInserted);
int maxRetries = 3;
var backoffs = new[] { 250, 500, 1000 };
for (int retry = 0; retry <= maxRetries; retry++)
{
try
{
var rowsInserted = await _db.Database.ExecuteSqlRawAsync(sb.ToString(), parameters.ToArray(), ct);
_logger.LogInformation("Inserted {RowsInserted} trades into the database.", rowsInserted);
break;
}
catch (Exception ex)
{
var mysqlEx = ex as MySqlConnector.MySqlException ?? ex.InnerException as MySqlConnector.MySqlException;
if (mysqlEx != null && (mysqlEx.Number == 1213 || mysqlEx.Number == 1205))
{
if (retry == maxRetries)
{
_logger.LogError(ex, "Failed to insert {Count} trades after {Retries} retries due to deadlocks.", chunk.Length, maxRetries);
}
else
{
_logger.LogWarning("Deadlock detected during trade insertion. Retrying in {Delay}ms... (Attempt {Attempt}/{Max})", backoffs[retry], retry + 1, maxRetries);
await Task.Delay(backoffs[retry], ct);
}
}
else
{
throw;
}
}
}
}
}