Renamed namespaces, classes, and commands from `DEXJob` to `ProfileJob` to align with the new "Profiles" context. Updated pipeline behaviors (`CheckQueryExecutionBehavior`, `MainQueryExecutionBehavior`, `ReCRequestExecutionBehavior`) to handle `TriggeringProfileJobCommand`. Refactored unit tests to reflect the new naming convention, including mock setups and assertions. Updated `DtoExtensions` to return `TriggeringProfileJobBatchCommand`. Adjusted queries and dependency injection to use the new `Profiles` namespace. Performed general refactoring to replace all references to "DEXJob" with "ProfileJob" in method names, variables, and documentation for consistency and clarity.
117 lines
4.9 KiB
C#
117 lines
4.9 KiB
C#
using ECMJobRunner.Application.Common.Constants;
|
|
using ECMJobRunner.Application.Common.Dtos;
|
|
using ECMJobRunner.Application.Common.Exceptions;
|
|
using ECMJobRunner.Application.Common.Interfaces;
|
|
using ECMJobRunner.Application.Common.Options;
|
|
using ECMJobRunner.Application.Profiles.Commands;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Options;
|
|
using System;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ECMJobRunner.Application.Profiles.Commands.Behaviors
|
|
{
|
|
/// <summary>
|
|
/// Pipeline behavior that executes the check SQL query for DEX jobs
|
|
/// Runs after MainQueryExecutionBehavior and validates query results
|
|
/// </summary>
|
|
/// <typeparam name="TRequest">The request type</typeparam>
|
|
/// <typeparam name="TResponse">The response type</typeparam>
|
|
public class CheckQueryExecutionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : notnull
|
|
{
|
|
private readonly ISQLExecutor _executor;
|
|
private readonly DexJobOptions _options;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of CheckQueryExecutionBehavior
|
|
/// </summary>
|
|
/// <param name="executor">SQL executor for query execution</param>
|
|
/// <param name="options">DEX job configuration options</param>
|
|
public CheckQueryExecutionBehavior(ISQLExecutor executor, IOptions<DexJobOptions> options)
|
|
{
|
|
_executor = executor;
|
|
_options = options.Value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the pipeline behavior
|
|
/// Executes check 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 TriggeringProfileJobCommand command)
|
|
{
|
|
await ExecuteCheckQueryAsync(command, cancellationToken);
|
|
}
|
|
|
|
return await next();
|
|
}
|
|
|
|
private async Task ExecuteCheckQueryAsync(TriggeringProfileJobCommand command, CancellationToken cancel)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(command.Job.SqlCheckQuery))
|
|
{
|
|
var sqlCheckQuery = Regex.Replace(
|
|
command.Job.SqlCheckQuery!,
|
|
_options.Placeholders.BatchId.Pattern,
|
|
command.BatchId,
|
|
_options.Placeholders.BatchId.RegexOptions);
|
|
|
|
try
|
|
{
|
|
var result = await _executor.ExecuteQueryAsync<CheckQueryResult>(sqlCheckQuery, cancel);
|
|
if (_options.Error.CheckQuery.OnUnexpectedResult == ErrorAction.Stop)
|
|
{
|
|
if (result is null)
|
|
throw new JobSqlException(
|
|
jobName: "Triggering DEX",
|
|
processName: "Check Query",
|
|
batchId: command.BatchId,
|
|
reason: "Check Query returned nothing.",
|
|
query: sqlCheckQuery,
|
|
innerException: null);
|
|
else if (result.ReturnValue <= 0)
|
|
throw new JobSqlException(
|
|
jobName: "Triggering DEX",
|
|
processName: "Check Query",
|
|
batchId: command.BatchId,
|
|
reason: $"The query unexpectedly returned the value {result.ReturnValue}. The expected value was any value greater than 0.",
|
|
query: sqlCheckQuery,
|
|
innerException: null);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_options.Error.CheckQuery.OnExecution == ErrorAction.Stop)
|
|
throw new JobSqlException(
|
|
jobName: "Triggering DEX",
|
|
processName: "Check Query",
|
|
batchId: command.BatchId,
|
|
reason: null,
|
|
query: sqlCheckQuery,
|
|
innerException: ex
|
|
);
|
|
}
|
|
}
|
|
else if (_options.Error.CheckQuery.IfNullOrWhiteSpace == ErrorAction.Stop)
|
|
{
|
|
throw new JobSqlException(
|
|
jobName:"Triggering DEX",
|
|
processName:"Check Query",
|
|
batchId:command.BatchId,
|
|
reason: "SQL Check Query is null or empty",
|
|
query: null,
|
|
innerException: null
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|