using FastEndpoints; using FrameworkBLL.SchedulerTaskGenerator; using FrameworkDAL.CustomCode.SchedulerTaskGenerator; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using static GB5Shared.GB5Constant.Constant; namespace FrameworkSL.Endpoints.SchedulerTaskGenerator { /// /// Marks a scheduler job execution as In-Progress before it is dispatched to the queue. /// Called internally by the scheduler engine prior to publishing. /// public sealed class PublishSchedulerTaskEndpoint : BaseEndpoint< PublishSchedulerTaskEndpoint.PublishSchedulerTaskParameters, ResponseStandardDTO> { private readonly ISchedulerTaskServiceBLL _schedulerService; private readonly ILoginContextProvider _loginProvider; public PublishSchedulerTaskEndpoint( ISchedulerTaskServiceBLL schedulerService, ILoginContextProvider loginProvider) { _schedulerService = schedulerService ?? throw new ArgumentNullException(nameof(schedulerService)); _loginProvider = loginProvider ?? throw new ArgumentNullException(nameof(loginProvider)); } public override void Configure() { Patch("/Scheduler/PublishTask"); AllowAnonymous(); // Internal scheduler call — no user auth required } public sealed record PublishSchedulerTaskParameters( int JobExecutionID, [property: FromHeader] string Login ); protected override async Task> ExecuteAsync( PublishSchedulerTaskParameters req, LoginDTO loginDTO, CancellationToken ct) { try { // Fallback to ambient login context if header is absent or incomplete if (string.IsNullOrWhiteSpace(loginDTO?.DatabaseName)) loginDTO = _loginProvider.GetLogin(); // Mark execution row as In-Progress before dispatching to queue await _schedulerService.MarkExecutionStartedAsync( req.JobExecutionID, loginDTO, ct // ✅ Forward ct so the DB call respects client cancellation ); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( $"Scheduler task {req.JobExecutionID} marked as in-progress.", CacheKeyLevel.USER_LEVEL, loginDTO ); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.USER_LEVEL, loginDTO, ex.Message, 500 ); } } } }