重构: 将 LogCleanupService 转为 Quartz Job 服务
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 22s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 22s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
- 创建 LogCleanupJob 替代 LogCleanupService (BackgroundService) - 在 Expand.cs 中注册 LogCleanupJob (每天凌晨2点执行, 保留30天日志) - 从 Program.cs 移除 LogCleanupService 的 HostedService 注册 - 删除 Service/LogCleanupService.cs - 删除 Service/PeriodicBillBackgroundService.cs (已无用的重复服务) 所有后台任务现在统一通过 Quartz.NET 管理, 支持运行时控制
This commit is contained in:
78
Service/Jobs/LogCleanupJob.cs
Normal file
78
Service/Jobs/LogCleanupJob.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Quartz;
|
||||
|
||||
namespace Service.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// 日志清理定时任务
|
||||
/// </summary>
|
||||
[DisallowConcurrentExecution]
|
||||
public class LogCleanupJob(ILogger<LogCleanupJob> logger) : IJob
|
||||
{
|
||||
private const int RetentionDays = 30; // 保留30天的日志
|
||||
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.LogInformation("开始执行日志清理任务");
|
||||
|
||||
var logDirectory = Path.Combine(Directory.GetCurrentDirectory(), "logs");
|
||||
if (!Directory.Exists(logDirectory))
|
||||
{
|
||||
logger.LogWarning("日志目录不存在: {LogDirectory}", logDirectory);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var cutoffDate = DateTime.Now.AddDays(-RetentionDays);
|
||||
var logFiles = Directory.GetFiles(logDirectory, "log-*.txt");
|
||||
var deletedCount = 0;
|
||||
|
||||
foreach (var logFile in logFiles)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileName = Path.GetFileNameWithoutExtension(logFile);
|
||||
var dateStr = fileName.Replace("log-", "");
|
||||
|
||||
// 尝试解析日期 (格式: yyyyMMdd)
|
||||
if (DateTime.TryParseExact(dateStr, "yyyyMMdd",
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None,
|
||||
out var logDate))
|
||||
{
|
||||
if (logDate < cutoffDate)
|
||||
{
|
||||
File.Delete(logFile);
|
||||
deletedCount++;
|
||||
logger.LogInformation("已删除过期日志文件: {LogFile} (日期: {LogDate})",
|
||||
Path.GetFileName(logFile), logDate.ToString("yyyy-MM-dd"));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "删除日志文件失败: {LogFile}", logFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (deletedCount > 0)
|
||||
{
|
||||
logger.LogInformation("日志清理完成,共删除 {DeletedCount} 个过期日志文件(保留 {RetentionDays} 天)",
|
||||
deletedCount, RetentionDays);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogDebug("没有需要清理的过期日志文件");
|
||||
}
|
||||
|
||||
logger.LogInformation("日志清理任务执行完成");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "日志清理任务执行出错");
|
||||
throw; // 让 Quartz 知道任务失败
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user