|
| 1 | +using Quartz; |
| 2 | + |
| 3 | +namespace Consensus.Api.Jobs; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Background job that cleans up output files (markdown, HTML, logs). |
| 7 | +/// </summary> |
| 8 | +public class OutputCleanupJob : IJob |
| 9 | +{ |
| 10 | + private readonly ILogger<OutputCleanupJob> _logger; |
| 11 | + private readonly string _outputDirectory; |
| 12 | + |
| 13 | + public OutputCleanupJob( |
| 14 | + ILogger<OutputCleanupJob> logger, |
| 15 | + IConfiguration configuration) |
| 16 | + { |
| 17 | + _logger = logger; |
| 18 | + _outputDirectory = configuration["OutputDirectory"] |
| 19 | + ?? Path.Combine(Directory.GetCurrentDirectory(), "output"); |
| 20 | + } |
| 21 | + |
| 22 | + public async Task Execute(IJobExecutionContext context) |
| 23 | + { |
| 24 | + _logger.LogInformation("OutputCleanupJob started at {Time}", DateTime.UtcNow); |
| 25 | + |
| 26 | + try |
| 27 | + { |
| 28 | + var responsesDir = Path.Combine(_outputDirectory, "responses"); |
| 29 | + var logsDir = Path.Combine(_outputDirectory, "logs"); |
| 30 | + |
| 31 | + int totalDeleted = 0; |
| 32 | + |
| 33 | + // Clean up responses directory (markdown and HTML files) |
| 34 | + if (Directory.Exists(responsesDir)) |
| 35 | + { |
| 36 | + totalDeleted += await DeleteFilesAsync(responsesDir, "*.md", "markdown"); |
| 37 | + totalDeleted += await DeleteFilesAsync(responsesDir, "*.html", "HTML"); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + _logger.LogWarning("Responses directory does not exist: {Directory}", responsesDir); |
| 42 | + } |
| 43 | + |
| 44 | + // Clean up logs directory |
| 45 | + if (Directory.Exists(logsDir)) |
| 46 | + { |
| 47 | + totalDeleted += await DeleteFilesAsync(logsDir, "*.log", "log"); |
| 48 | + totalDeleted += await DeleteFilesAsync(logsDir, "*.txt", "text log"); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + _logger.LogWarning("Logs directory does not exist: {Directory}", logsDir); |
| 53 | + } |
| 54 | + |
| 55 | + _logger.LogInformation("OutputCleanupJob completed. Total files deleted: {Count}", totalDeleted); |
| 56 | + } |
| 57 | + catch (Exception ex) |
| 58 | + { |
| 59 | + _logger.LogError(ex, "OutputCleanupJob failed"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private async Task<int> DeleteFilesAsync(string directory, string searchPattern, string fileType) |
| 64 | + { |
| 65 | + int deletedCount = 0; |
| 66 | + |
| 67 | + try |
| 68 | + { |
| 69 | + var files = Directory.GetFiles(directory, searchPattern); |
| 70 | + |
| 71 | + foreach (var file in files) |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | + await Task.Run(() => File.Delete(file)); |
| 76 | + deletedCount++; |
| 77 | + _logger.LogDebug("Deleted {FileType} file: {FileName}", fileType, Path.GetFileName(file)); |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + _logger.LogWarning(ex, "Failed to delete file: {FileName}", Path.GetFileName(file)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (deletedCount > 0) |
| 86 | + { |
| 87 | + _logger.LogInformation("Deleted {Count} {FileType} files from {Directory}", deletedCount, fileType, directory); |
| 88 | + } |
| 89 | + } |
| 90 | + catch (Exception ex) |
| 91 | + { |
| 92 | + _logger.LogError(ex, "Failed to access directory for cleanup: {Directory}", directory); |
| 93 | + } |
| 94 | + |
| 95 | + return deletedCount; |
| 96 | + } |
| 97 | +} |
0 commit comments