Files
ECMJobRunner/ECMJobRunner.Application/DEXJob/TriggeringDEXJobBatchCommand.cs
TekH 2af17ec870 feat: implement DEX job triggering commands with CQRS pattern
TriggeringDEXJobBatchCommand:
- Orchestrates batch job execution for a profile
- Creates unique 20-character timestamp-based batch ID
- Executes all SQL jobs sequentially for given profile ID
- Uses MediatR ISender to trigger individual job commands

TriggeringDEXJobCommand:
- Executes single DEX job with three-stage pipeline:
  1. Main Query: executes primary SQL with batch ID placeholder replacement
  2. Check Query: validates execution with return value check (> 0)
  3. ReC Request: invokes ReC API with batch ID reference
- Configurable error handling per stage via DexJobOptions
- Regex-based placeholder replacement for dynamic batch ID injection
- Returns Unit for void-like MediatR command pattern

Both commands include:
- Comprehensive XML documentation
- Primary constructor injection (modern C# syntax)
- Proper async/await with CancellationToken support
- Integration with ISQLExecutor and ReCClient abstractions
2026-07-11 12:38:42 +02:00

74 lines
2.7 KiB
C#

using ECMJobRunner.Domain.Interfaces;
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ECMJobRunner.Application.DEXJob
{
/// <summary>
/// Command to trigger DEX job batch for a profile
/// Executes all SQL jobs associated with a profile ID
/// </summary>
public class TriggeringDEXJobBatchCommand : IRequest<Unit>
{
/// <summary>
/// Profile ID to trigger all associated SQL jobs
/// </summary>
public int ProfileId { get; set; }
}
/// <summary>
/// Handler for TriggeringDEXJobBatchCommand
/// Retrieves all SQL jobs for a profile and executes them sequentially
/// </summary>
public class TriggeringDEXJobBatchCommandHandler(IProfileSqlJobRepository jobRepo, ISender sender)
: IRequestHandler<TriggeringDEXJobBatchCommand, Unit>
{
/// <summary>
/// Handles the TriggeringDEXJobBatchCommand
/// Creates a unique batch ID and triggers individual job commands
/// </summary>
/// <param name="request">The command containing the profile ID</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Unit value indicating completion</returns>
public async Task<Unit> Handle(TriggeringDEXJobBatchCommand request, CancellationToken cancellationToken)
{
var jobs = await jobRepo.FindAsync(j => j.ProfileId == request.ProfileId, cancellationToken);
var batchId = CreateBatchId();
foreach (var job in jobs)
{
await sender.Send(new TriggeringDEXJobCommand
{
Job = job,
BatchId = batchId
}, cancellationToken);
}
return Unit.Value;
}
/// <summary>
/// Creates a unique batch ID based on current timestamp
/// </summary>
/// <returns>20-character timestamp string (yyyyMMddHHmmssfffffff truncated to 20 chars)</returns>
public static string CreateBatchId()
{
// Format: "yyyy" : Year (4 digits)
// "MM" : Month (2 digits)
// "dd" : Day (2 digits)
// "HH" : Hour (24-hour format, 2 digits)
// "mm" : Minute (2 digits)
// "ss" : Second (2 digits)
// "fffffff": Fractions of a second / 100-nanoseconds (7 digits)
string fullBatchId = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
// Limit to 20 characters as per specification
return fullBatchId.Substring(0, 20);
}
}
}