From f6b0014ec6cd14851137d1f070ee923b5e06d801 Mon Sep 17 00:00:00 2001 From: Flaminel Date: Mon, 2 Jun 2025 12:58:45 +0300 Subject: [PATCH] fixed some configuration stuff --- code/Common/Configuration/IJobConfig.cs | 2 + .../Controllers/ConfigurationController.cs | 66 ++++++++----------- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/code/Common/Configuration/IJobConfig.cs b/code/Common/Configuration/IJobConfig.cs index 1ef7a8f1..bc7612a1 100644 --- a/code/Common/Configuration/IJobConfig.cs +++ b/code/Common/Configuration/IJobConfig.cs @@ -3,4 +3,6 @@ public interface IJobConfig : IConfig { bool Enabled { get; init; } + + string CronExpression { get; init; } } \ No newline at end of file diff --git a/code/Executable/Controllers/ConfigurationController.cs b/code/Executable/Controllers/ConfigurationController.cs index a686e10b..79a6068e 100644 --- a/code/Executable/Controllers/ConfigurationController.cs +++ b/code/Executable/Controllers/ConfigurationController.cs @@ -1,3 +1,4 @@ +using Common.Configuration; using Common.Configuration.Arr; using Common.Configuration.ContentBlocker; using Common.Configuration.DownloadCleaner; @@ -16,7 +17,6 @@ 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; @@ -141,57 +141,43 @@ public class ConfigurationController : ControllerBase } // Update the scheduler based on configuration changes - await UpdateQueueCleanerJobSchedule(config); + await UpdateJobSchedule(config, JobType.QueueCleaner); return Ok(new { Message = "QueueCleaner configuration updated successfully" }); } /// - /// Updates the QueueCleaner job schedule based on configuration changes + /// Updates a job schedule based on configuration changes /// - /// The QueueCleaner configuration - private async Task UpdateQueueCleanerJobSchedule(QueueCleanerConfig config) + /// The job configuration + /// The type of job to update + private async Task UpdateJobSchedule(IJobConfig config, JobType jobType) { 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); + // Get the cron expression based on the specific config type + if (!string.IsNullOrEmpty(config.CronExpression)) + { + // If the job is enabled, update its schedule with the configured cron expression + _logger.LogInformation("{JobName} is enabled, updating job schedule with cron expression: {CronExpression}", + jobType.ToString(), config.CronExpression); + + // Create a Quartz job schedule with the cron expression + await _jobManagementService.StartJob(jobType, null, config.CronExpression); + } + else + { + _logger.LogWarning("{JobName} is enabled, but no cron expression was found in the configuration", jobType.ToString()); + } - // 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); + return; } + + // If the job is disabled, stop it + _logger.LogInformation("{JobName} is disabled, stopping the job", jobType.ToString()); + await _jobManagementService.StopJob(jobType); } - /// - /// Updates the DownloadCleaner job schedule based on configuration changes - /// - /// The DownloadCleaner configuration - private async Task UpdateDownloadCleanerJobSchedule(DownloadCleanerConfig config) - { - if (config.Enabled) - { - // If the job is enabled, update its schedule with the configured cron expression - _logger.LogInformation("DownloadCleaner is enabled, updating job schedule with cron expression: {CronExpression}", config.CronExpression); - - // Create a Quartz job schedule with the cron expression - await _jobManagementService.StartJob(JobType.DownloadCleaner, null, config.CronExpression); - } - else - { - // If the job is disabled, stop it - _logger.LogInformation("DownloadCleaner is disabled, stopping the job"); - await _jobManagementService.StopJob(JobType.DownloadCleaner); - } - } - [HttpPut("content_blocker")] public async Task UpdateContentBlockerConfig([FromBody] ContentBlockerConfigUpdateDto dto) { @@ -234,7 +220,7 @@ public class ConfigurationController : ControllerBase } // Update the scheduler based on configuration changes - await UpdateDownloadCleanerJobSchedule(config); + await UpdateJobSchedule(config, JobType.DownloadCleaner); return Ok(new { Message = "DownloadCleaner configuration updated successfully" }); }