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.JobDefine { /// /// Fires a job immediately via its existing Quartz trigger. Rejects (400, via the /// thrown InvalidOperationException) any JobId that isn't already registered in /// Quartz — deliberately does not fall back to an anonymous one-shot trigger, /// which would lose TenantId scoping (a known pitfall in the reference design). /// public sealed class TriggerJobDefine : BaseEndpoint> { private readonly IJobDefineOperationsBLL _ops; private readonly ILoginContextProvider _loginProvider; public TriggerJobDefine(IJobDefineOperationsBLL ops, ILoginContextProvider loginProvider) { _ops = ops; _loginProvider = loginProvider; } public override void Configure() { Post("/JobDefine/Trigger"); AllowAnonymous(); // Internal scheduler admin call — matches Scheduler/* endpoint convention } public sealed record TriggerJobDefineParameters( int JobId, [property: FromHeader] string Login ); protected override async Task> ExecuteAsync( TriggerJobDefineParameters req, LoginDTO loginDTO, CancellationToken ct) { try { if (string.IsNullOrWhiteSpace(loginDTO?.DatabaseName)) loginDTO = _loginProvider.GetLogin(); await _ops.TriggerAsync(req.JobId, loginDTO, ct); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( $"Job {req.JobId} triggered.", CacheKeyLevel.NOT_REQUIRED, loginDTO); } catch (InvalidOperationException ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.NOT_REQUIRED, loginDTO, ex.Message, 400); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.NOT_REQUIRED, loginDTO, ex.Message, 500); } } } }