using ECMJobRunner.Domain.Interfaces; using MediatR; using System; using System.Threading; using System.Threading.Tasks; namespace ECMJobRunner.Application.DEXJob { /// /// Command to trigger DEX job batch for a profile /// Executes all SQL jobs associated with a profile ID /// public class TriggeringDEXJobBatchCommand : IRequest { /// /// Profile ID to trigger all associated SQL jobs /// public int ProfileId { get; set; } } /// /// Handler for TriggeringDEXJobBatchCommand /// Retrieves all SQL jobs for a profile and executes them sequentially /// public class TriggeringDEXJobBatchCommandHandler(IProfileSqlJobRepository jobRepo, ISender sender) : IRequestHandler { /// /// Handles the TriggeringDEXJobBatchCommand /// Creates a unique batch ID and triggers individual job commands /// /// The command containing the profile ID /// Cancellation token /// Unit value indicating completion public async Task 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; } /// /// Creates a unique batch ID based on current timestamp /// /// 20-character timestamp string (yyyyMMddHHmmssfffffff truncated to 20 chars) 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); } } }