feat(application): implement MediatR pipeline behaviors for DEX job stages
- Add MainQueryExecutionBehavior - Execute main SQL query via ISQLExecutor - Populate command.MainQueryResults - Throw JobSqlException on failure - Add CheckQueryExecutionBehavior - Execute check SQL query via ISQLExecutor - Validate ErrorAction.SkipInsert for zero results - Throw JobSqlException on failure/validation error - Add ReCRequestExecutionBehavior - Execute ReC API requests for each main query result - Batch ID tracking, error handling - Throw JobHttpException on API failures - Conditional compilation for MediatR signature differences - net480: Handle(request, cancellationToken, next) - net8.0: Handle(request, next, cancellationToken) - Refactor TriggeringDEXJobCommand handler: delegate all logic to behaviors
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using ECMJobRunner.Application.Common.Constants;
|
||||
using ECMJobRunner.Application.Common.Exceptions;
|
||||
using ECMJobRunner.Application.Common.Options;
|
||||
using ECMJobRunner.Application.DEXJob;
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Pipeline behavior that sends ReC HTTP request for DEX jobs
|
||||
/// Runs after CheckQueryExecutionBehavior and invokes ReC API
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The request type</typeparam>
|
||||
/// <typeparam name="TResponse">The response type</typeparam>
|
||||
public class ReCRequestExecutionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : notnull
|
||||
{
|
||||
private readonly ReCClient _reCClient;
|
||||
private readonly DexJobOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of ReCRequestExecutionBehavior
|
||||
/// </summary>
|
||||
/// <param name="reCClient">ReC client for HTTP requests</param>
|
||||
/// <param name="options">DEX job configuration options</param>
|
||||
public ReCRequestExecutionBehavior(ReCClient reCClient, IOptions<DexJobOptions> options)
|
||||
{
|
||||
_reCClient = reCClient;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the pipeline behavior
|
||||
/// Sends ReC request if request is TriggeringDEXJobCommand
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
#else
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user