Harden API memory and CPU usage across Phases 19–22.
CI / changes (push) Successful in 17s
dotnet8-ci / build-and-test (push) Successful in 8m23s
CI / backend (push) Successful in 8m24s
node-ci / build (push) Failing after 2m1s
CI / frontend (push) Failing after 2m2s

Bound list/PDF payloads, stream MinIO I/O with cancellation, paginate history and FHIR $everything, tighten OCR lifetimes/buffers, and add phase tracking docs.
This commit is contained in:
2026-11-30 23:55:56 +08:00
parent 192cb36dc7
commit a17bc2b734
46 changed files with 1677 additions and 215 deletions
@@ -1,7 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Minio;
using Minio.DataModel.Args;
/// <summary>
/// Runs migrate / demo seed / MinIO bucket ensure after Kestrel is listening.
@@ -38,19 +35,12 @@ public sealed class AppBootstrapHostedService : BackgroundService
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.Database.MigrateAsync(stoppingToken);
await DataSeeder.SeedAsync(db);
var minio = scope.ServiceProvider.GetRequiredService<IMinioClient>();
var minioOpts = scope.ServiceProvider.GetRequiredService<IOptions<MinioOptions>>().Value;
if (!await minio.BucketExistsAsync(
new BucketExistsArgs().WithBucket(minioOpts.BucketName), stoppingToken))
{
await minio.MakeBucketAsync(
new MakeBucketArgs().WithBucket(minioOpts.BucketName), stoppingToken);
_logger.LogInformation("Created MinIO bucket {Bucket}", minioOpts.BucketName);
}
await DataSeeder.SeedAsync(db, stoppingToken);
var storage = scope.ServiceProvider.GetRequiredService<IDocumentStorageService>();
await storage.EnsureBucketAsync(stoppingToken);
_logger.LogInformation("MinIO bucket ensure completed.");
await SeedDocumentStorage.EnsureAsync(db, storage, _logger, stoppingToken);
_startupHealthCheck.StartupCompleted = true;
@@ -147,10 +147,10 @@ public class OcrProcessingService : BackgroundService
try
{
await using var documentStream = await storage.DownloadAsync(document.ObjectKey);
var extraction = await ocr.ExtractAsync(documentStream, document.ContentType);
await using var documentStream = await storage.DownloadAsync(document.ObjectKey, ct);
var extraction = await ocr.ExtractAsync(documentStream, document.ContentType, ct);
await preFiller.PreFillAsync(batch.Id, batch.BatchType, extraction);
await preFiller.PreFillAsync(batch.Id, batch.BatchType, extraction, ct);
var fieldConfidences = extraction.Fields
.GroupBy(f => f.FieldName, StringComparer.Ordinal)
@@ -163,7 +163,7 @@ public class OcrProcessingService : BackgroundService
BatchId = batch.Id,
Provider = _options.Provider,
FieldConfidencesJson = JsonSerializer.Serialize(fieldConfidences),
RawText = extraction.RawText,
RawText = TruncateRawText(extraction.RawText),
DurationMs = extraction.DurationMs,
ProcessedAt = processedAt
});
@@ -213,4 +213,16 @@ public class OcrProcessingService : BackgroundService
batch.Id, ex.Message);
}
}
/// <summary>Caps persisted OCR text to limit DB/LOH growth; draft GET uses confidences only.</summary>
internal const int MaxRawTextChars = 64 * 1024;
private static string? TruncateRawText(string? rawText)
{
if (string.IsNullOrEmpty(rawText))
return rawText;
return rawText.Length <= MaxRawTextChars
? rawText
: rawText[..MaxRawTextChars];
}
}
@@ -133,7 +133,7 @@ public class PromotionRetryService : BackgroundService
$"Batch {batchId} is in Approved status but has no ApprovedByUserId");
// Attempt promotion with a unique idempotency key per attempt
await promotionService.PromoteAsync(batchId, approverUserId);
await promotionService.PromoteAsync(batchId, approverUserId, ct);
// --- Success path ---
DiagnosticsMetrics.PromotionRetryTotal.WithLabels("success").Inc();