diff --git a/code/Executable/Controllers/ConfigurationController.cs b/code/Executable/Controllers/ConfigurationController.cs index ee9e9e4f..f7519797 100644 --- a/code/Executable/Controllers/ConfigurationController.cs +++ b/code/Executable/Controllers/ConfigurationController.cs @@ -15,7 +15,9 @@ using Common.Configuration.IgnoredDownloads; using Common.Configuration.Notification; using Common.Configuration.QueueCleaner; using Infrastructure.Configuration; +using Infrastructure.Models; using Infrastructure.Services; +using Infrastructure.Services.Interfaces; using Mapster; using Microsoft.AspNetCore.Mvc; @@ -27,13 +29,16 @@ public class ConfigurationController : ControllerBase { private readonly ILogger _logger; private readonly IConfigManager _configManager; + private readonly IJobManagementService _jobManagementService; public ConfigurationController( ILogger logger, - IConfigManager configManager) + IConfigManager configManager, + IJobManagementService jobManagementService) { _logger = logger; _configManager = configManager; + _jobManagementService = jobManagementService; } [HttpGet("queue_cleaner")] @@ -135,8 +140,35 @@ public class ConfigurationController : ControllerBase return StatusCode(500, "Failed to save QueueCleaner configuration"); } + // Update the scheduler based on configuration changes + await UpdateQueueCleanerJobSchedule(config); + return Ok(new { Message = "QueueCleaner configuration updated successfully" }); } + + /// + /// Updates the QueueCleaner job schedule based on configuration changes + /// + /// The QueueCleaner configuration + private async Task UpdateQueueCleanerJobSchedule(QueueCleanerConfig config) + { + if (config.Enabled) + { + // If the job is enabled, update its schedule with the configured cron expression + _logger.LogInformation("QueueCleaner is enabled, updating job schedule with cron expression: {CronExpression}", config.CronExpression); + + // Create a Quartz job schedule with the cron expression + // Note: This is using the raw cron expression, not creating a JobSchedule object + // since QueueCleanerConfig already contains a cron expression + await _jobManagementService.StartJob(JobType.QueueCleaner, null, config.CronExpression); + } + else + { + // If the job is disabled, stop it + _logger.LogInformation("QueueCleaner is disabled, stopping the job"); + await _jobManagementService.StopJob(JobType.QueueCleaner); + } + } [HttpPut("content_blocker")] public async Task UpdateContentBlockerConfig([FromBody] ContentBlockerConfigUpdateDto dto) diff --git a/code/Infrastructure/Services/Interfaces/IJobManagementService.cs b/code/Infrastructure/Services/Interfaces/IJobManagementService.cs index a8e1cc83..095bd346 100644 --- a/code/Infrastructure/Services/Interfaces/IJobManagementService.cs +++ b/code/Infrastructure/Services/Interfaces/IJobManagementService.cs @@ -4,7 +4,7 @@ namespace Infrastructure.Services.Interfaces; public interface IJobManagementService { - Task StartJob(JobType jobType, JobSchedule? schedule = null); + Task StartJob(JobType jobType, JobSchedule? schedule = null, string? directCronExpression = null); Task StopJob(JobType jobType); Task PauseJob(JobType jobType); Task ResumeJob(JobType jobType); diff --git a/code/Infrastructure/Services/JobManagementService.cs b/code/Infrastructure/Services/JobManagementService.cs index 70df20f8..757f3d8b 100644 --- a/code/Infrastructure/Services/JobManagementService.cs +++ b/code/Infrastructure/Services/JobManagementService.cs @@ -21,10 +21,10 @@ public class JobManagementService : IJobManagementService _schedulerFactory = schedulerFactory; } - public async Task StartJob(JobType jobType, JobSchedule? schedule = null) + public async Task StartJob(JobType jobType, JobSchedule? schedule = null, string? directCronExpression = null) { string jobName = jobType.ToJobName(); - string? cronExpression = schedule?.ToCronExpression(); + string? cronExpression = directCronExpression ?? schedule?.ToCronExpression(); try {