MdpNightlyFullRebuildJob.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Furion.Schedule;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Logging;
  4. using System.Text.Json;
  5. using Admin.NET.Plugin.AiDOP.DataPlatform.MdpRebuild;
  6. namespace Admin.NET.Plugin.AiDOP.Job;
  7. /// <summary>
  8. /// S1–S7 夜间串行全量兜底。triggerType 用 <see cref="ModuleRebuildTriggerType.AutoNightly"/>,
  9. /// 穿透 AUTO 冷却窗口;串行靠 <c>GlobalMaxParallelScopes = 1</c>,本 Job 一次性入队全部 scope。
  10. /// </summary>
  11. [JobDetail("job_mdp_nightly_full_rebuild",
  12. Description = "S1–S7 夜间串行全量兜底(穿透 AUTO 冷却窗口)",
  13. GroupName = "default",
  14. Concurrent = false)]
  15. [Cron("40 3 * * *",
  16. TriggerId = "trigger_mdp_nightly_full_rebuild",
  17. Description = "每日 03:40 触发(5 字段:分 时 日 月 周)")]
  18. public class MdpNightlyFullRebuildJob : IJob
  19. {
  20. private static readonly string[] Modules = ["S1", "S2", "S3", "S4", "S5", "S6", "S7"];
  21. private readonly IServiceScopeFactory _scopeFactory;
  22. private readonly ILogger _logger;
  23. public MdpNightlyFullRebuildJob(IServiceScopeFactory scopeFactory, ILoggerFactory loggerFactory)
  24. {
  25. _scopeFactory = scopeFactory;
  26. _logger = loggerFactory.CreateLogger(nameof(MdpNightlyFullRebuildJob));
  27. }
  28. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  29. {
  30. if (!AidopJobGate.ShouldRun(nameof(MdpNightlyFullRebuildJob), _logger)) return;
  31. using var scope = _scopeFactory.CreateScope();
  32. try
  33. {
  34. foreach (var module in Modules)
  35. {
  36. stoppingToken.ThrowIfCancellationRequested();
  37. var payload = await ModuleRebuildJobRunner.EnqueueEnabledScopesAsync(
  38. scope.ServiceProvider, module, ModuleRebuildTriggerType.AutoNightly, stoppingToken, _logger);
  39. _logger.LogInformation("MdpNightlyFullRebuildJob {Module} 入队 {Payload}", module, JsonSerializer.Serialize(payload));
  40. }
  41. }
  42. catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
  43. {
  44. _logger.LogInformation("MdpNightlyFullRebuildJob 收到停止信号,本轮结束");
  45. throw;
  46. }
  47. catch (Exception ex)
  48. {
  49. _logger.LogError(ex, "MdpNightlyFullRebuildJob 入队失败");
  50. }
  51. }
  52. }