Keep the hosted AI queue polling after transient database failures

This commit is contained in:
cesnimda
2026-12-02 18:48:47 +01:00
parent ac64d2ee22
commit 562b66c962
4 changed files with 92 additions and 6 deletions
+14 -3
View File
@@ -1,3 +1,4 @@
using System.Data.Common;
using JobTrackerApi.Data;
using JobTrackerApi.Models;
using Microsoft.AspNetCore.Identity;
@@ -251,7 +252,8 @@ public sealed class AiOperationWorker(
private static TimeSpan RetryDelay(int attempt) => TimeSpan.FromSeconds(Math.Min(60, (1 << Math.Min(attempt, 5)) + Random.Shared.Next(0, 4)));
}
public sealed class AiOperationHostedService(AiOperationWorker worker, IConfiguration configuration) : BackgroundService
public sealed class AiOperationHostedService(
AiOperationWorker worker, IConfiguration configuration, ILogger<AiOperationHostedService> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
@@ -266,8 +268,17 @@ public sealed class AiOperationHostedService(AiOperationWorker worker, IConfigur
var idleDelayMs = Math.Clamp(configuration.GetValue("AiQueue:IdleDelayMs", 1000), 100, 10_000);
while (!stoppingToken.IsCancellationRequested)
{
if (!await worker.RunOnceAsync(stoppingToken, documentImportsOnly))
await Task.Delay(idleDelayMs, stoppingToken);
try
{
if (await worker.RunOnceAsync(stoppingToken, documentImportsOnly)) continue;
}
catch (DbException failure) when (failure.IsTransient ||
failure is Microsoft.Data.Sqlite.SqliteException { SqliteErrorCode: 5 or 6 })
{
// The store owns recovery of any persisted lease when polling resumes.
logger.LogWarning("AI operation polling paused after a transient database error.");
}
await Task.Delay(idleDelayMs, stoppingToken);
}
}
}