Skip to content

Commit

Permalink
发布 v2.10.1 版本,改进 GC 回收时机
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Apr 21, 2023
1 parent 57800da commit 435e5d2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Sundial.Dashboard/Sundial.Dashboard.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>2.10.0</Version>
<Version>2.10.1</Version>
<Description>Sundial Dashboard 看板</Description>
</PropertyGroup>

Expand All @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Sundial" Version="2.10.0" />
<PackageReference Include="Sundial" Version="2.10.1" />
</ItemGroup>

</Project>
39 changes: 33 additions & 6 deletions src/Sundial/Factories/SchedulerFactory.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ internal sealed partial class SchedulerFactory : ISchedulerFactory
/// <remarks>用于取消休眠状态(唤醒)</remarks>
private CancellationTokenSource _sleepCancellationTokenSource;

/// <summary>
/// GC 垃圾回收间隔
/// </summary>
/// <remarks>单位毫秒</remarks>
private const int GC_COLLECT_INTERVAL_MILLISECONDS = 3000;

/// <summary>
/// 作业计划集合
/// </summary>
Expand Down Expand Up @@ -111,6 +117,11 @@ public SchedulerFactory(IServiceProvider serviceProvider
/// </summary>
private bool PreloadCompleted { get; set; } = false;

/// <summary>
/// GC 最近一次回收时间
/// </summary>
private DateTime? LastGCCollectTime { get; set; }

/// <summary>
/// 作业调度器初始化
/// </summary>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
});
}

Expand Down Expand Up @@ -447,4 +458,20 @@ private ScheduleResult InternalTryGetJob(string jobId, out Scheduler scheduler,
scheduler = originScheduler;
return ScheduleResult.Succeed;
}

/// <summary>
/// GC 垃圾回收器回收处理
/// </summary>
/// <remarks>避免频繁 GC 回收</remarks>
private void GCCollect()
{
var nowTime = DateTime.UtcNow;
if ((LastGCCollectTime == null || (nowTime - LastGCCollectTime.Value).TotalMilliseconds > GC_COLLECT_INTERVAL_MILLISECONDS))
{
LastGCCollectTime = nowTime;

// 通知 GC 垃圾回收器立即回收
GC.Collect();
}
}
}
2 changes: 1 addition & 1 deletion src/Sundial/HostedServices/ScheduleHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public override Task StartAsync(CancellationToken cancellationToken)
/// </summary>
/// <param name="stoppingToken">后台主机服务停止时取消任务 Token</param>
/// <returns><see cref="Task"/> 实例</returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Schedule hosted service is running.");

Expand Down
2 changes: 1 addition & 1 deletion src/Sundial/Sundial.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>2.10.0</Version>
<Version>2.10.1</Version>
<Description>.NET 功能齐全的开源分布式作业调度系统,可从最小的应用程序到大型企业系统使用。</Description>
</PropertyGroup>

Expand Down

0 comments on commit 435e5d2

Please sign in to comment.