MdpRawRetentionJob.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  2. using Furion.Schedule;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Logging;
  6. namespace Admin.NET.Plugin.AiDOP.Job;
  7. [JobDetail("job_mdp_raw_retention", Description = "数据中台 Raw 保留到期处置", GroupName = "default", Concurrent = false)]
  8. [PeriodSeconds(900, TriggerId = "trigger_mdp_raw_retention", Description = "分批清理已到期的 Raw 原文", RunOnStart = false)]
  9. public sealed class MdpRawRetentionJob(IServiceScopeFactory scopes, IConfiguration config, ILogger<MdpRawRetentionJob> logger) : IJob
  10. {
  11. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  12. {
  13. if (!AidopJobGate.ShouldRun(nameof(MdpRawRetentionJob), logger)
  14. || !config.GetValue<bool>("MdpRawRetention:Enabled")) return;
  15. using var scope = scopes.CreateScope();
  16. var retention = scope.ServiceProvider.GetRequiredService<MdpIngestRawRetentionService>();
  17. var total = 0;
  18. const int maxBatches = 12;
  19. for (var batch = 0; batch < maxBatches && !stoppingToken.IsCancellationRequested; batch++)
  20. {
  21. var count = await retention.ExpireAsync(stoppingToken);
  22. total += count;
  23. if (count < MdpIngestRawRetentionService.BatchLimit)
  24. break;
  25. }
  26. if (total > 0)
  27. logger.LogInformation("数据中台已处置 {Count} 个到期 Raw 原文,保留批次与追溯证据。", total);
  28. if (total == maxBatches * MdpIngestRawRetentionService.BatchLimit)
  29. logger.LogWarning("Raw 到期处置已达到单次上限,剩余批次由后续任务处理。");
  30. }
  31. }