Skip to content

Commit 0c36214

Browse files
author
Timothy Dodd
committed
Improve log cleanup, DB handling, and JSON highlighting
- Execute `RunRetentionCleanupAsync` immediately on startup in `LogRetentionService` with added exception handling. - Reduce log deletion batch size from 10,000 to 5,000 to lower DB load. - Add retry logic with exponential backoff for deadlocks during log deletion. - Explicitly open DB connection in `UpdateCurrentHourSummaryAsync` to ensure readiness before transactions. - Refine JSON number regex in `HighlightLogPipe` to avoid matching times.
1 parent e41a572 commit 0c36214

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/LogMkApi/Services/LogRetentionService.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2727
{
2828
_logger.LogInformation("LogRetentionService started");
2929

30+
// Run immediately on startup
31+
try
32+
{
33+
await RunRetentionCleanupAsync(stoppingToken);
34+
}
35+
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
36+
{
37+
return;
38+
}
39+
catch (Exception ex)
40+
{
41+
_logger.LogError(ex, "Error during initial retention cleanup");
42+
}
43+
3044
while (!stoppingToken.IsCancellationRequested)
3145
{
3246
var now = DateTime.UtcNow;
@@ -81,16 +95,30 @@ private async Task RunRetentionCleanupAsync(CancellationToken cancellationToken)
8195
retentionDays, cutoffDate);
8296

8397
var totalDeleted = 0L;
84-
const int batchSize = 10000;
98+
const int batchSize = 5000;
8599

86100
using var db = _dbFactory.CreateConnection();
87101

88102
// Batch delete from Log table
103+
const int maxRetries = 3;
89104
while (!cancellationToken.IsCancellationRequested)
90105
{
91-
var deleted = await db.ExecuteAsync(
92-
$"DELETE FROM {db.GetQuotedTableName<Log>()} WHERE LogDate < @cutoffDate LIMIT {batchSize}",
93-
new { cutoffDate });
106+
int deleted = 0;
107+
for (var attempt = 1; attempt <= maxRetries; attempt++)
108+
{
109+
try
110+
{
111+
deleted = await db.ExecuteAsync(
112+
$"DELETE FROM {db.GetQuotedTableName<Log>()} WHERE LogDate < @cutoffDate LIMIT {batchSize}",
113+
new { cutoffDate });
114+
break;
115+
}
116+
catch (MySql.Data.MySqlClient.MySqlException ex) when (ex.Number == 1213 && attempt < maxRetries)
117+
{
118+
_logger.LogWarning("Deadlock on retention delete attempt {Attempt}, retrying after delay...", attempt);
119+
await Task.Delay(500 * attempt, cancellationToken);
120+
}
121+
}
94122

95123
if (deleted == 0)
96124
break;

src/LogMkApi/Services/LogSummaryHourlyBackgroundService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ private async Task UpdateCurrentHourSummaryAsync()
5858
{
5959
using (var connection = _dbFactory.CreateConnection())
6060
{
61+
connection.Open();
6162
var date = DateTime.UtcNow.Date;
6263
var hour = DateTime.UtcNow.Hour - 1;
6364

src/LogMkWeb/src/app/_pages/main-log-page/_services/highlight.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export class HighlightLogPipe implements PipeTransform {
4141
// JSON string values (quoted strings not followed by colon)
4242
[/:\s*"([^"]*)"/g, ': "<span class="json-string">$1</span>"'],
4343

44-
// JSON numbers
45-
[/:\s*(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)\b/g, ': <span class="json-number">$1</span>'],
44+
// JSON numbers (negative lookbehind for digits to avoid matching times like 02:04:07)
45+
[/(?<!\d):\s*(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)\b/g, ': <span class="json-number">$1</span>'],
4646

4747
// JSON booleans
4848
[/:\s*(true|false)\b/g, ': <span class="json-boolean">$1</span>'],

0 commit comments

Comments
 (0)