- Add CronSchedule property to CfgProfileDto for scheduling support - Create ProfileSqlJobDto for SQL job execution data transfer - Update TriggeringDEXJobBatchCommand to accept ProfileId and Jobs collection - Make TriggeringDEXJobBatchCommand public for Hangfire job activation - Update Application DependencyInjection with required service registrations
93 lines
3.5 KiB
C#
93 lines
3.5 KiB
C#
using ECMJobRunner.Application.Common.Exceptions;
|
|
using ECMJobRunner.Application.Common.Interfaces;
|
|
using ECMJobRunner.Domain.Interfaces;
|
|
using MediatR;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ECMJobRunner.Application.DEXJob.Commands
|
|
{
|
|
/// <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 long ProfileId { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handler for TriggeringDEXJobBatchCommand
|
|
/// Retrieves all SQL jobs for a profile and executes them sequentially
|
|
/// Validates that the profile is active before execution
|
|
/// </summary>
|
|
public class TriggeringDEXJobBatchCommandHandler(ICfgProfileRepository profileRepo, 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>
|
|
/// <exception cref="InactiveProfileException">Thrown when the profile is not active</exception>
|
|
public async Task<Unit> Handle(TriggeringDEXJobBatchCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var batchId = CreateBatchId();
|
|
|
|
// Retrieve the profile to check if it's active
|
|
var profile = await profileRepo.GetByIdAsync(request.ProfileId, cancellationToken);
|
|
|
|
// Check if profile exists and is active
|
|
if (profile == null)
|
|
{
|
|
throw new InvalidOperationException($"Profile with ID {request.ProfileId} not found.");
|
|
}
|
|
|
|
if (!profile.Active)
|
|
{
|
|
throw new InactiveProfileException(profile.Id, profile.ProfileName, batchId);
|
|
}
|
|
|
|
// Retrieve all jobs for the profile
|
|
var jobs = await jobRepo.FindAsync(j => j.ProfileId == request.ProfileId, cancellationToken);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|