mirror of
https://github.com/Cleanuparr/Cleanuparr.git
synced 2025-12-23 22:18:39 -05:00
118 lines
3.9 KiB
C#
118 lines
3.9 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
|
|
using Cleanuparr.Api.Features.QueueCleaner.Contracts.Requests;
|
|
using Cleanuparr.Infrastructure.Models;
|
|
using Cleanuparr.Infrastructure.Services.Interfaces;
|
|
using Cleanuparr.Infrastructure.Utilities;
|
|
using Cleanuparr.Persistence;
|
|
using Cleanuparr.Persistence.Models.Configuration;
|
|
using Cleanuparr.Persistence.Models.Configuration.QueueCleaner;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Cleanuparr.Api.Features.QueueCleaner.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/configuration")]
|
|
public sealed class QueueCleanerConfigController : ControllerBase
|
|
{
|
|
private readonly ILogger<QueueCleanerConfigController> _logger;
|
|
private readonly DataContext _dataContext;
|
|
private readonly IJobManagementService _jobManagementService;
|
|
|
|
public QueueCleanerConfigController(
|
|
ILogger<QueueCleanerConfigController> logger,
|
|
DataContext dataContext,
|
|
IJobManagementService jobManagementService)
|
|
{
|
|
_logger = logger;
|
|
_dataContext = dataContext;
|
|
_jobManagementService = jobManagementService;
|
|
}
|
|
|
|
[HttpGet("queue_cleaner")]
|
|
public async Task<IActionResult> GetQueueCleanerConfig()
|
|
{
|
|
await DataContext.Lock.WaitAsync();
|
|
try
|
|
{
|
|
var config = await _dataContext.QueueCleanerConfigs
|
|
.AsNoTracking()
|
|
.FirstAsync();
|
|
return Ok(config);
|
|
}
|
|
finally
|
|
{
|
|
DataContext.Lock.Release();
|
|
}
|
|
}
|
|
|
|
[HttpPut("queue_cleaner")]
|
|
public async Task<IActionResult> UpdateQueueCleanerConfig([FromBody] UpdateQueueCleanerConfigRequest newConfigDto)
|
|
{
|
|
await DataContext.Lock.WaitAsync();
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(newConfigDto.CronExpression))
|
|
{
|
|
CronValidationHelper.ValidateCronExpression(newConfigDto.CronExpression);
|
|
}
|
|
|
|
newConfigDto.FailedImport.Validate();
|
|
|
|
var oldConfig = await _dataContext.QueueCleanerConfigs
|
|
.FirstAsync();
|
|
|
|
oldConfig.Enabled = newConfigDto.Enabled;
|
|
oldConfig.CronExpression = newConfigDto.CronExpression;
|
|
oldConfig.UseAdvancedScheduling = newConfigDto.UseAdvancedScheduling;
|
|
oldConfig.FailedImport = newConfigDto.FailedImport;
|
|
oldConfig.DownloadingMetadataMaxStrikes = newConfigDto.DownloadingMetadataMaxStrikes;
|
|
oldConfig.IgnoredDownloads = newConfigDto.IgnoredDownloads;
|
|
|
|
await _dataContext.SaveChangesAsync();
|
|
|
|
await UpdateJobSchedule(oldConfig, JobType.QueueCleaner);
|
|
|
|
return Ok(new { Message = "QueueCleaner configuration updated successfully" });
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to save QueueCleaner configuration");
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
DataContext.Lock.Release();
|
|
}
|
|
}
|
|
|
|
private async Task UpdateJobSchedule(IJobConfig config, JobType jobType)
|
|
{
|
|
if (config.Enabled)
|
|
{
|
|
if (!string.IsNullOrEmpty(config.CronExpression))
|
|
{
|
|
_logger.LogInformation("{name} is enabled, updating job schedule with cron expression: {CronExpression}",
|
|
jobType.ToString(), config.CronExpression);
|
|
|
|
await _jobManagementService.StartJob(jobType, null, config.CronExpression);
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("{name} is enabled, but no cron expression was found in the configuration", jobType.ToString());
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("{name} is disabled, stopping the job", jobType.ToString());
|
|
await _jobManagementService.StopJob(jobType);
|
|
}
|
|
}
|