diff --git a/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs b/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs index 9a617aa..724893c 100644 --- a/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs +++ b/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs @@ -1507,6 +1507,7 @@ public sealed class ComprehensiveAssessmentsController( outbox.ProcessingToken = null; outbox.LeaseExpiresAt = null; outbox.LastError = null; + outbox.ProcessingAttempts = 0; } } diff --git a/src/Jiaowu.Api/Controllers/OperationsController.cs b/src/Jiaowu.Api/Controllers/OperationsController.cs index 6407430..7571c97 100644 --- a/src/Jiaowu.Api/Controllers/OperationsController.cs +++ b/src/Jiaowu.Api/Controllers/OperationsController.cs @@ -515,15 +515,19 @@ public sealed class OperationsController( var ids = pageItems.Select(x => x.Id).ToArray(); var attemptRows = await db.BackgroundJobOutboxMessages.AsNoTracking() .WhereIn(ids, x => x.JobId) - .Select(x => new { x.JobKind, x.JobId, x.ProcessingAttempts }) + .Select(x => new { x.JobKind, x.JobId, x.ProcessingAttempts, x.LastError }) .ToListAsync(cancellationToken); var attempts = attemptRows.ToDictionary( x => (x.JobKind.ToString(), x.JobId), - x => x.ProcessingAttempts); + x => new { x.ProcessingAttempts, x.LastError }); pageItems = pageItems .Select(x => x with { - ProcessingAttempts = attempts.GetValueOrDefault((x.Kind, x.Id)) + ProcessingAttempts = attempts.GetValueOrDefault((x.Kind, x.Id))?.ProcessingAttempts ?? 0, + ErrorMessage = x.Kind == nameof(BackgroundJobKind.ComprehensiveAssessmentCalculation) && + !string.IsNullOrWhiteSpace(attempts.GetValueOrDefault((x.Kind, x.Id))?.LastError) + ? attempts[(x.Kind, x.Id)].LastError! + : x.ErrorMessage }) .ToArray(); } diff --git a/src/Jiaowu.Api/Infrastructure/Assessments/ComprehensiveAssessmentLiveRefreshWorker.cs b/src/Jiaowu.Api/Infrastructure/Assessments/ComprehensiveAssessmentLiveRefreshWorker.cs index 3acc312..ffee51b 100644 --- a/src/Jiaowu.Api/Infrastructure/Assessments/ComprehensiveAssessmentLiveRefreshWorker.cs +++ b/src/Jiaowu.Api/Infrastructure/Assessments/ComprehensiveAssessmentLiveRefreshWorker.cs @@ -48,6 +48,7 @@ public sealed class ComprehensiveAssessmentLiveRefreshWorker( x.CalculationStatus != ComprehensiveAssessmentCalculationStatus.Running && x.LiveCalculationStatus != ComprehensiveAssessmentCalculationStatus.Queued && x.LiveCalculationStatus != ComprehensiveAssessmentCalculationStatus.Running && + x.LiveCalculationStatus != ComprehensiveAssessmentCalculationStatus.Failed && (!x.LiveCalculatedAt.HasValue || x.LiveCalculatedAt < staleBefore) && x.DimensionLocks.Any(d => d.Status != ComprehensiveAssessmentDimensionLockStatus.Locked)) .OrderBy(x => x.LiveCalculatedAt) @@ -76,6 +77,7 @@ public sealed class ComprehensiveAssessmentLiveRefreshWorker( outbox.ProcessingToken = null; outbox.LeaseExpiresAt = null; outbox.LastError = null; + outbox.ProcessingAttempts = 0; } await db.SaveChangesAsync(token); logger.LogInformation("Queued {Count} comprehensive assessment live refresh jobs.", batches.Count); diff --git a/src/Jiaowu.Api/Infrastructure/BackgroundJobs/BackgroundJobRunner.cs b/src/Jiaowu.Api/Infrastructure/BackgroundJobs/BackgroundJobRunner.cs index 5465a71..fdf9ffe 100644 --- a/src/Jiaowu.Api/Infrastructure/BackgroundJobs/BackgroundJobRunner.cs +++ b/src/Jiaowu.Api/Infrastructure/BackgroundJobs/BackgroundJobRunner.cs @@ -56,7 +56,7 @@ public sealed class BackgroundJobRunner( { await MarkJobRetryLimitExceededAsync(message, cancellationToken); await MarkCompletedAsync(message.OutboxMessageId, token, - cancellationToken); + cancellationToken, clearError: false); telemetry.RecordProcessing( message.JobKind, "retry-limit", @@ -275,8 +275,7 @@ public sealed class BackgroundJobRunner( .SetProperty(x => x.LeaseExpiresAt, leaseExpiresAt) .SetProperty( x => x.ProcessingAttempts, - x => x.ProcessingAttempts + 1) - .SetProperty(x => x.LastError, (string?)null), + x => x.ProcessingAttempts + 1), cancellationToken); if (claimed == 1) { @@ -471,7 +470,7 @@ public sealed class BackgroundJobRunner( x.LiveCalculationStatus != ComprehensiveAssessmentCalculationStatus.Succeeded) .ExecuteUpdateAsync(setters => setters .SetProperty(x => x.LiveCalculationStatus, ComprehensiveAssessmentCalculationStatus.Failed) - .SetProperty(x => x.LiveCalculationError, error), cancellationToken); + .SetProperty(x => x.LiveCalculationError, x => x.LiveCalculationError ?? error), cancellationToken); } else { @@ -479,7 +478,7 @@ public sealed class BackgroundJobRunner( x.CalculationStatus != ComprehensiveAssessmentCalculationStatus.Succeeded) .ExecuteUpdateAsync(setters => setters .SetProperty(x => x.CalculationStatus, ComprehensiveAssessmentCalculationStatus.Failed) - .SetProperty(x => x.CalculationError, error), cancellationToken); + .SetProperty(x => x.CalculationError, x => x.CalculationError ?? error), cancellationToken); } break; default: @@ -523,7 +522,8 @@ public sealed class BackgroundJobRunner( private async Task MarkCompletedAsync( Guid outboxMessageId, Guid token, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool clearError = true) { await using var scope = scopeFactory.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); @@ -537,7 +537,8 @@ public sealed class BackgroundJobRunner( .SetProperty(x => x.State, BackgroundJobOutboxState.Completed) .SetProperty(x => x.CompletedAt, DateTime.UtcNow) .SetProperty(x => x.ProcessingToken, (Guid?)null) - .SetProperty(x => x.LeaseExpiresAt, (DateTime?)null), + .SetProperty(x => x.LeaseExpiresAt, (DateTime?)null) + .SetProperty(x => x.LastError, x => clearError ? null : x.LastError), cancellationToken); } diff --git a/tests/Jiaowu.Api.Tests/BackgroundJobOutboxTests.cs b/tests/Jiaowu.Api.Tests/BackgroundJobOutboxTests.cs index 18f6ce4..d32ab5b 100644 --- a/tests/Jiaowu.Api.Tests/BackgroundJobOutboxTests.cs +++ b/tests/Jiaowu.Api.Tests/BackgroundJobOutboxTests.cs @@ -233,6 +233,7 @@ public sealed class BackgroundJobOutboxTests job.Id); outbox.State = BackgroundJobOutboxState.Published; outbox.ProcessingAttempts = 5; + outbox.LastError = "原始处理错误"; db.AddRange(term, plan, job, outbox); await db.SaveChangesAsync(); envelope = new BackgroundJobEnvelope( @@ -256,6 +257,7 @@ public sealed class BackgroundJobOutboxTests Assert.Null(persistedOutbox.ProcessingToken); Assert.Null(persistedOutbox.LeaseExpiresAt); Assert.Equal(6, persistedOutbox.ProcessingAttempts); + Assert.Equal("原始处理错误", persistedOutbox.LastError); var persistedJob = await assertDb.AutomaticScheduleJobs.SingleAsync(); Assert.Equal(AutomaticScheduleJobStatus.Failed, persistedJob.Status); Assert.Null(persistedJob.ActiveSchedulePlanId); diff --git a/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs b/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs index 0ed1f60..ccb723c 100644 --- a/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs +++ b/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs @@ -3,6 +3,7 @@ using Jiaowu.Api.Controllers; using Jiaowu.Api.Contracts; using Jiaowu.Api.Domain.Academic; using Jiaowu.Api.Domain.Identity; +using Jiaowu.Api.Domain.System; using Jiaowu.Api.Infrastructure.Assessments; using Jiaowu.Api.Infrastructure.Auth; using Jiaowu.Api.Infrastructure.Persistence; @@ -20,6 +21,25 @@ namespace Jiaowu.Api.Tests; public sealed class ComprehensiveAssessmentsControllerTests { + [Fact] + public async Task Manual_recalculation_resets_previous_attempts() + { + await using var fixture = await Fixture.CreateAsync(); + fixture.Batch.CalculationStatus = ComprehensiveAssessmentCalculationStatus.Failed; + var outbox = BackgroundJobOutboxMessage.Create(BackgroundJobKind.ComprehensiveAssessmentCalculation, fixture.Batch.Id); + outbox.State = BackgroundJobOutboxState.Completed; + outbox.ProcessingAttempts = 29110; + outbox.LastError = "旧错误"; + fixture.Db.BackgroundJobOutboxMessages.Add(outbox); + await fixture.Db.SaveChangesAsync(); + + var controller = new ComprehensiveAssessmentsController(fixture.Db, fixture.ManagerScope); + Assert.IsType(await controller.QueueCalculation(fixture.Batch.Id, CancellationToken.None)); + Assert.Equal(0, outbox.ProcessingAttempts); + Assert.Null(outbox.LastError); + Assert.Equal(BackgroundJobOutboxState.Pending, outbox.State); + } + [Fact] public async Task Declaration_accepts_attachment_as_evidence_and_obeys_reader_scope() {