| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | |
| | | 5 | | namespace AsyncResponse; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Executes <see cref="WorkerJobEnvelope"/>s: restores the correlation context and invokes the |
| | | 9 | | /// described service method through the DI container. Shared by the broker ingress |
| | | 10 | | /// (<see cref="IAsyncResponseIngress.HandleWorkerMessageAsync"/>) and the in-process worker |
| | | 11 | | /// transport, so every transport executes jobs identically. |
| | | 12 | | /// </summary> |
| | 3 | 13 | | internal sealed class WorkerJobExecutor(IServiceScopeFactory _scopeFactory, ILogger<WorkerJobExecutor> _logger) |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Executes the job. Exceptions propagate to the caller — transports decide whether to log, |
| | | 17 | | /// retry, or dead-letter. |
| | | 18 | | /// </summary> |
| | | 19 | | public async Task ExecuteAsync(WorkerJobEnvelope job) |
| | | 20 | | { |
| | 3 | 21 | | ArgumentNullException.ThrowIfNull(job); |
| | | 22 | | |
| | | 23 | | // Reject a job stamped with an unsupported schema rather than invoke a possibly-incompatible |
| | | 24 | | // method shape. Throwing routes the job through the transport's normal |
| | | 25 | | // failure/dead-letter handling. This is the single choke point every transport shares. |
| | 3 | 26 | | if (!WorkerJobEnvelopeSchema.IsReadable(job.SchemaVersion)) |
| | | 27 | | { |
| | 2 | 28 | | _logger.LogWarning( |
| | 2 | 29 | | "Worker job for correlationId {CorrelationId} has unsupported schema version {SchemaVersion} (current: { |
| | 2 | 30 | | job.CorrelationId, job.SchemaVersion, WorkerJobEnvelopeSchema.Current); |
| | 2 | 31 | | AsyncResponseDiagnostics.RecordWorkerOutcome("rejected"); |
| | 2 | 32 | | throw new InvalidOperationException( |
| | 2 | 33 | | $"Worker job schema version {job.SchemaVersion} is not supported by this build " + |
| | 2 | 34 | | $"(current: {WorkerJobEnvelopeSchema.Current}) and cannot be executed safely."); |
| | | 35 | | } |
| | | 36 | | |
| | 3 | 37 | | using var activity = AsyncResponseDiagnostics.StartActivity( |
| | 3 | 38 | | "asyncresponse.worker.execute", |
| | 3 | 39 | | ActivityKind.Consumer, |
| | 3 | 40 | | job.CorrelationId); |
| | 3 | 41 | | AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget); |
| | 3 | 42 | | AsyncResponseDiagnostics.SetWorker(activity, job.Call); |
| | | 43 | | |
| | 3 | 44 | | _logger.LogDebug("Executing worker job {Target}.{Method} (correlationId: {CorrelationId}, replyTarget: {ReplyTar |
| | | 45 | | |
| | | 46 | | try |
| | | 47 | | { |
| | | 48 | | // Scope the restored ambient context so one job cannot inherit or leak another job's |
| | | 49 | | // correlation id or reply target. |
| | 3 | 50 | | using var asyncResponseScope = AsyncResponseContext.PushContext(job.CorrelationId, job.ReplyTarget); |
| | | 51 | | |
| | 3 | 52 | | var invocation = ReflectionExtensions.ResolveCallback( |
| | 3 | 53 | | job.Call, |
| | 3 | 54 | | payload: null, |
| | 3 | 55 | | exception: null, |
| | 3 | 56 | | correlationId: job.CorrelationId); |
| | | 57 | | |
| | 3 | 58 | | await using var scope = _scopeFactory.CreateAsyncScope(); |
| | 3 | 59 | | await scope.ServiceProvider.InvokeAsync(invocation).ConfigureAwait(false); |
| | | 60 | | |
| | 3 | 61 | | _logger.LogDebug("Executed worker job {Target}.{Method} successfully.", job.Call.ServiceInterfaceFullName, j |
| | 3 | 62 | | AsyncResponseDiagnostics.RecordWorkerOutcome("executed"); |
| | 3 | 63 | | } |
| | 2 | 64 | | catch (Exception ex) |
| | | 65 | | { |
| | 2 | 66 | | AsyncResponseDiagnostics.SetError(activity, ex); |
| | 2 | 67 | | AsyncResponseDiagnostics.RecordWorkerOutcome("failed"); |
| | 2 | 68 | | throw; |
| | | 69 | | } |
| | 3 | 70 | | } |
| | | 71 | | } |