diff --git a/src/Sundial.Dashboard/Sundial.Dashboard.csproj b/src/Sundial.Dashboard/Sundial.Dashboard.csproj index 07f196a..14f0fd9 100644 --- a/src/Sundial.Dashboard/Sundial.Dashboard.csproj +++ b/src/Sundial.Dashboard/Sundial.Dashboard.csproj @@ -1,7 +1,7 @@ - 2.10.0 + 2.10.1 Sundial Dashboard 看板 @@ -18,7 +18,7 @@ - + diff --git a/src/Sundial/Factories/SchedulerFactory.Internal.cs b/src/Sundial/Factories/SchedulerFactory.Internal.cs index 92e89d2..dc46804 100644 --- a/src/Sundial/Factories/SchedulerFactory.Internal.cs +++ b/src/Sundial/Factories/SchedulerFactory.Internal.cs @@ -44,6 +44,12 @@ internal sealed partial class SchedulerFactory : ISchedulerFactory /// 用于取消休眠状态(唤醒) private CancellationTokenSource _sleepCancellationTokenSource; + /// + /// GC 垃圾回收间隔 + /// + /// 单位毫秒 + private const int GC_COLLECT_INTERVAL_MILLISECONDS = 3000; + /// /// 作业计划集合 /// @@ -111,6 +117,11 @@ public SchedulerFactory(IServiceProvider serviceProvider /// private bool PreloadCompleted { get; set; } = false; + /// + /// GC 最近一次回收时间 + /// + private DateTime? LastGCCollectTime { get; set; } + /// /// 作业调度器初始化 /// @@ -150,7 +161,7 @@ public void Preload() // 释放引用内存并立即回收GC _schedulerBuilders.Clear(); - GC.Collect(); + GCCollect(); // 输出作业调度器初始化日志 if (preloadSucceed) _logger.LogWarning("Schedule hosted service preload completed, and a total of <{Count}> schedulers are appended.", _schedulers.Count); @@ -339,7 +350,7 @@ public void Dispose() if (!_schedulers.Any()) { // 输出作业调度器休眠总时长和唤醒时间日志 - _logger.LogInformation("Schedule hosted service will sleep until it wakes up."); + _logger.LogWarning("Schedule hosted service will sleep until it wakes up."); return null; } @@ -360,7 +371,7 @@ public void Dispose() var sleepMilliseconds = (earliestTriggerTime - startAt).TotalMilliseconds; // 输出作业调度器休眠总时长和唤醒时间日志 - _logger.LogInformation("Schedule hosted service will sleep <{sleepMilliseconds}> milliseconds and be waked up at <{earliestTriggerTime}>.", sleepMilliseconds, earliestTriggerTime.ToUnspecifiedString()); + _logger.LogDebug("Schedule hosted service will sleep <{sleepMilliseconds}> milliseconds and be waked up at <{earliestTriggerTime}>.", sleepMilliseconds, earliestTriggerTime.ToUnspecifiedString()); return sleepMilliseconds; } @@ -400,13 +411,13 @@ private void CreateCancellationTokenSource() // 初始化作业调度器休眠 Token _sleepCancellationTokenSource = new CancellationTokenSource(); - // 监听休眠被取消 + // 监听休眠被取消,并通知 GC 垃圾回收器回收 _sleepCancellationTokenSource.Token.Register(() => { - _logger.LogWarning("Schedule hosted service cancels hibernation and GC.Collect()."); + _logger.LogWarning("Schedule hosted service cancels hibernation."); // 通知 GC 垃圾回收器立即回收 - GC.Collect(); + GCCollect(); }); } @@ -447,4 +458,20 @@ private ScheduleResult InternalTryGetJob(string jobId, out Scheduler scheduler, scheduler = originScheduler; return ScheduleResult.Succeed; } + + /// + /// GC 垃圾回收器回收处理 + /// + /// 避免频繁 GC 回收 + private void GCCollect() + { + var nowTime = DateTime.UtcNow; + if ((LastGCCollectTime == null || (nowTime - LastGCCollectTime.Value).TotalMilliseconds > GC_COLLECT_INTERVAL_MILLISECONDS)) + { + LastGCCollectTime = nowTime; + + // 通知 GC 垃圾回收器立即回收 + GC.Collect(); + } + } } \ No newline at end of file diff --git a/src/Sundial/HostedServices/ScheduleHostedService.cs b/src/Sundial/HostedServices/ScheduleHostedService.cs index eafad73..c577ed4 100644 --- a/src/Sundial/HostedServices/ScheduleHostedService.cs +++ b/src/Sundial/HostedServices/ScheduleHostedService.cs @@ -113,7 +113,7 @@ public override Task StartAsync(CancellationToken cancellationToken) /// /// 后台主机服务停止时取消任务 Token /// 实例 - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + protected async override Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Schedule hosted service is running."); diff --git a/src/Sundial/Sundial.csproj b/src/Sundial/Sundial.csproj index 206979a..44ec3d5 100644 --- a/src/Sundial/Sundial.csproj +++ b/src/Sundial/Sundial.csproj @@ -1,7 +1,7 @@ - 2.10.0 + 2.10.1 .NET 功能齐全的开源分布式作业调度系统,可从最小的应用程序到大型企业系统使用。