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:
113
ECMJobRunner.Application/Behaviors/MainQueryExecutionBehavior.cs
Normal file
113
ECMJobRunner.Application/Behaviors/MainQueryExecutionBehavior.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using ECMJobRunner.Application.Common.Constants;
|
||||
using ECMJobRunner.Application.Common.Dtos;
|
||||
using ECMJobRunner.Application.Common.Exceptions;
|
||||
using ECMJobRunner.Domain.Interfaces;
|
||||
using ECMJobRunner.Application.Common.Options;
|
||||
using ECMJobRunner.Application.DEXJob;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ECMJobRunner.Application.Behaviors
|
||||
{
|
||||
/// <summary>
|
||||
/// Pipeline behavior that executes the main SQL query for DEX jobs
|
||||
/// Runs before the command handler and validates query results
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The request type</typeparam>
|
||||
/// <typeparam name="TResponse">The response type</typeparam>
|
||||
public class MainQueryExecutionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : notnull
|
||||
{
|
||||
private readonly ISQLExecutor _executor;
|
||||
private readonly DexJobOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of MainQueryExecutionBehavior
|
||||
/// </summary>
|
||||
/// <param name="executor">SQL executor for query execution</param>
|
||||
/// <param name="options">DEX job configuration options</param>
|
||||
public MainQueryExecutionBehavior(ISQLExecutor executor, IOptions<DexJobOptions> options)
|
||||
{
|
||||
_executor = executor;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the pipeline behavior
|
||||
/// Executes main query 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 ExecuteMainQueryAsync(command, cancellationToken);
|
||||
}
|
||||
|
||||
return await next();
|
||||
}
|
||||
|
||||
private async Task ExecuteMainQueryAsync(TriggeringDEXJobCommand command, CancellationToken cancel)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(command.Job.SqlMainQuery))
|
||||
{
|
||||
var sqlMainQuery = Regex.Replace(
|
||||
command.Job.SqlMainQuery!,
|
||||
_options.Placeholders.BatchId.Pattern,
|
||||
command.BatchId,
|
||||
_options.Placeholders.BatchId.RegexOptions);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await _executor.ExecuteQueryAsync<MainQueryResult>(sqlMainQuery, cancel);
|
||||
if (_options.Error.MainQuery.OnUnexpectedResult == ErrorAction.Stop)
|
||||
{
|
||||
if (result is null)
|
||||
throw new JobSqlException(
|
||||
jobName: "Triggering DEX",
|
||||
processName: "Main Query",
|
||||
batchId: command.BatchId,
|
||||
reason: "Main Query returned nothing.",
|
||||
query: sqlMainQuery,
|
||||
innerException: null);
|
||||
else if (result.ReturnValue is not null)
|
||||
throw new JobSqlException(
|
||||
jobName: "Triggering DEX",
|
||||
processName: "Main Query",
|
||||
batchId: command.BatchId,
|
||||
reason: $"The query unexpectedly returned the value {result.ReturnValue}. The expected value was null.",
|
||||
query: sqlMainQuery, innerException: null);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (_options.Error.MainQuery.OnExecution == ErrorAction.Stop)
|
||||
throw new JobSqlException(
|
||||
jobName: "Triggering DEX",
|
||||
processName: "Main Query",
|
||||
batchId: command.BatchId,
|
||||
reason: null,
|
||||
query: sqlMainQuery,
|
||||
innerException: ex);
|
||||
}
|
||||
}
|
||||
else if (_options.Error.MainQuery.IfNullOrWhiteSpace == ErrorAction.Stop)
|
||||
{
|
||||
throw new JobSqlException(
|
||||
jobName: "Triggering DEX",
|
||||
processName: "Main Query",
|
||||
batchId: command.BatchId,
|
||||
reason: "SQL Check Query is null or empty",
|
||||
query: null,
|
||||
innerException: null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user