using ECMJobRunner.Application.Common.Constants; using ECMJobRunner.Application.Common.Exceptions; using ECMJobRunner.Application.Common.Options; using ECMJobRunner.Application.DEXJob.Commands; using MediatR; using Microsoft.Extensions.Options; using ReC.Client; using ReC.Client.Api; using System; using System.Threading; using System.Threading.Tasks; namespace ECMJobRunner.Application.Behaviors { /// /// Pipeline behavior that sends ReC HTTP request for DEX jobs /// Runs after CheckQueryExecutionBehavior and invokes ReC API /// /// The request type /// The response type public class ReCRequestExecutionBehavior : IPipelineBehavior where TRequest : notnull { private readonly ReCClient _reCClient; private readonly DexJobOptions _options; /// /// Initializes a new instance of ReCRequestExecutionBehavior /// /// ReC client for HTTP requests /// DEX job configuration options public ReCRequestExecutionBehavior(ReCClient reCClient, IOptions options) { _reCClient = reCClient; _options = options.Value; } /// /// Handles the pipeline behavior /// Sends ReC request if request is TriggeringDEXJobCommand /// #if NET48 public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) #else public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) #endif { if (request is TriggeringDEXJobCommand command) { await SendReCRequestAsync(command, cancellationToken); } return await next(); } private async Task SendReCRequestAsync(TriggeringDEXJobCommand command, CancellationToken cancel) { try { await _reCClient.RecActions.InvokeAsync(command.Job.ProfileId, new InvokeReferences() { BatchId = command.BatchId, }, cancel); } catch (Exception ex) { if (_options.Error.ReCRequest.OnSending == ErrorAction.Stop) { throw new JobHttpException( jobName: "Triggering DEX", processName: "ReC Http Request", batchId: command.BatchId, reason: null, clientLibrary: "ReC.Client", clientMethod: "RecActions.InvokeAsync", innerException: ex); } } } } }