| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using MongoDB.Driver; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | |
| | | 6 | | namespace AsyncResponse.Transports.MongoDB; |
| | | 7 | | |
| | | 8 | | /// <summary>Publishes <see cref="WorkerJobEnvelope"/> messages to the MongoDB worker queue.</summary> |
| | | 9 | | public sealed class MongoDbWorkerTransport : IWorkerTransport |
| | | 10 | | { |
| | | 11 | | private readonly MongoDbAsyncResponseTransportOptions _options; |
| | | 12 | | private readonly MongoDbTransportStore _store; |
| | | 13 | | |
| | | 14 | | /// <summary>Creates a MongoDB worker transport over the host's shared database.</summary> |
| | | 15 | | public MongoDbWorkerTransport( |
| | | 16 | | IOptions<MongoDbAsyncResponseTransportOptions> options, |
| | | 17 | | IMongoDatabase database) |
| | 3 | 18 | | : this(options, new MongoDbTransportStore(database, options)) |
| | | 19 | | { |
| | 3 | 20 | | } |
| | | 21 | | |
| | 3 | 22 | | internal MongoDbWorkerTransport( |
| | 3 | 23 | | IOptions<MongoDbAsyncResponseTransportOptions> options, |
| | 3 | 24 | | MongoDbTransportStore store) |
| | | 25 | | { |
| | 3 | 26 | | _options = options.Value; |
| | 3 | 27 | | MongoDbTransportOptionsValidator.ValidateCommon(_options); |
| | 3 | 28 | | _store = store; |
| | 3 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public async Task PublishAsync(WorkerJobEnvelope job, CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 3 | 34 | | ArgumentNullException.ThrowIfNull(job); |
| | | 35 | | |
| | 3 | 36 | | using var activity = AsyncResponseDiagnostics.StartActivity( |
| | 3 | 37 | | "asyncresponse.worker.publish", |
| | 3 | 38 | | ActivityKind.Producer, |
| | 3 | 39 | | job.CorrelationId); |
| | 3 | 40 | | activity?.SetTag("asyncresponse.transport", "mongodb"); |
| | 3 | 41 | | activity?.SetTag("messaging.system", "mongodb"); |
| | 3 | 42 | | activity?.SetTag("messaging.destination.name", _options.WorkerQueue); |
| | 3 | 43 | | AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget); |
| | 3 | 44 | | AsyncResponseDiagnostics.SetWorker(activity, job.Call); |
| | | 45 | | |
| | | 46 | | try |
| | | 47 | | { |
| | 3 | 48 | | var headers = string.IsNullOrWhiteSpace(job.CorrelationId) |
| | 3 | 49 | | ? null |
| | 3 | 50 | | : new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| | 3 | 51 | | { |
| | 3 | 52 | | [_options.CorrelationIdHeader] = job.CorrelationId! |
| | 3 | 53 | | }; |
| | | 54 | | |
| | 3 | 55 | | var payload = AsyncResponseJson.Serialize(job); |
| | | 56 | | // Stable id outside the retry loop so a retried publish is idempotent rather than enqueuing |
| | | 57 | | // the same worker job twice. |
| | 3 | 58 | | var messageId = Guid.NewGuid(); |
| | 3 | 59 | | await MongoDbTransportRetry.ExecuteAsync( |
| | 3 | 60 | | async token => |
| | 3 | 61 | | { |
| | 3 | 62 | | await _store.PublishAsync(messageId, _options.WorkerQueue, payload, headers, token).ConfigureAwait(f |
| | 3 | 63 | | return true; |
| | 3 | 64 | | }, |
| | 3 | 65 | | _options.PublishMaxAttempts, |
| | 3 | 66 | | _options.PublishRetryBaseDelay, |
| | 3 | 67 | | _options.PublishRetryMaxDelay, |
| | 3 | 68 | | cancellationToken).ConfigureAwait(false); |
| | 3 | 69 | | } |
| | 2 | 70 | | catch (Exception ex) |
| | | 71 | | { |
| | 2 | 72 | | AsyncResponseDiagnostics.SetError(activity, ex); |
| | 3 | 73 | | throw; |
| | | 74 | | } |
| | 3 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | internal static class MongoDbTransportRetry |
| | | 79 | | { |
| | | 80 | | public static Task<T> ExecuteAsync<T>( |
| | | 81 | | Func<CancellationToken, Task<T>> action, |
| | | 82 | | int maxAttempts, |
| | | 83 | | TimeSpan baseDelay, |
| | | 84 | | TimeSpan maxDelay, |
| | | 85 | | CancellationToken cancellationToken) |
| | | 86 | | => AsyncResponseRetry.ExecuteAsync(action, IsTransient, maxAttempts, baseDelay, maxDelay, cancellationToken); |
| | | 87 | | |
| | | 88 | | public static bool IsTransient(Exception exception) |
| | | 89 | | => exception is not OperationCanceledException |
| | | 90 | | && (exception is MongoConnectionException |
| | | 91 | | or MongoNotPrimaryException |
| | | 92 | | or MongoNodeIsRecoveringException |
| | | 93 | | or MongoExecutionTimeoutException |
| | | 94 | | or TimeoutException |
| | | 95 | | || (exception is MongoException mongoException && mongoException.HasErrorLabel("RetryableWriteError"))); |
| | | 96 | | } |