JobExceptionHandlingBehavior: - Add ILogger for diagnostic output - Change ResultText to include full exception details (ToString()) - Log JobException with warning level including ProfileId, JobName, ProcessName, BatchId - Return default instead of re-throwing to allow graceful handling ReCRequestExecutionBehavior: - Convert to primary constructor pattern - Add ILogger for request tracking - Store RecActionResult in command for later use - Log successful ReC requests with detailed metrics (TotalActionCount, ActionExceptionCount) - Improve error handling and logging
86 lines
3.5 KiB
C#
86 lines
3.5 KiB
C#
using ECMJobRunner.Application.Common.Constants;
|
|
using ECMJobRunner.Application.Common.Exceptions;
|
|
using ECMJobRunner.Application.Common.Options;
|
|
using ECMJobRunner.Application.Profiles.Commands;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using ReC.Client;
|
|
using ReC.Client.Api;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ECMJobRunner.Application.Profiles.Commands.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>
|
|
/// <remarks>
|
|
/// Initializes a new instance of ReCRequestExecutionBehavior
|
|
/// </remarks>
|
|
/// <param name="ReCClient">ReC client for HTTP requests</param>
|
|
/// <param name="options">DEX job configuration options</param>
|
|
public class ReCRequestExecutionBehavior<TRequest, TResponse>(ReCClient ReCClient, IOptions<DexJobOptions> options, ILogger<ReCRequestExecutionBehavior<TRequest, TResponse>> Logger) : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : notnull
|
|
{
|
|
private readonly DexJobOptions 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 TriggeringProfileJobCommand command)
|
|
{
|
|
await SendReCRequestAsync(command, cancellationToken);
|
|
}
|
|
|
|
return await next();
|
|
}
|
|
|
|
private async Task SendReCRequestAsync(TriggeringProfileJobCommand command, CancellationToken cancel)
|
|
{
|
|
try
|
|
{
|
|
command.RecActionResult = await ReCClient.RecActions.InvokeAsync(command.Job.ProfileId, new InvokeReferences()
|
|
{
|
|
BatchId = command.BatchId,
|
|
}, cancel);
|
|
|
|
Logger.LogInformation(
|
|
"ReC request completed successfully. Profile ID: {ProfileId} | Job name: {JobName} | Batch ID: {BatchId} | Total action count: {TotalActionCount} | Action exception count: {ActionExceptionCount}",
|
|
command.Job.ProfileId,
|
|
command.Job.Name,
|
|
command.BatchId,
|
|
command.RecActionResult?.TotalActionCount ?? 0,
|
|
command.RecActionResult?.ActionExceptionCount ?? 0);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (Options.Error.ReCRequest.OnSending == ErrorAction.Stop)
|
|
{
|
|
throw new JobHttpException(
|
|
profileId: command.Job.ProfileId,
|
|
jobName: "Triggering DEX",
|
|
processName: "ReC Http Request",
|
|
batchId: command.BatchId,
|
|
reason: null,
|
|
clientLibrary: "ReC.Client",
|
|
clientMethod: "RecActions.InvokeAsync",
|
|
innerException: ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|