Skip to content

Commit

Permalink
设置全局定时调度器的时间提供者,借助服务器时间差,以获得更准确的时间。避免本地时间偏差导致定时任务执行时间不准确
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 18, 2024
1 parent 50779b9 commit 18b0ab3
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Samples/TestA/TestA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.10.2024.713-beta0409" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.718-beta0517" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Samples/TestB/TestB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.10.2024.713-beta0409" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.718-beta0517" />
</ItemGroup>

</Project>
7 changes: 2 additions & 5 deletions Stardust.Server/Controllers/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ public Int32 PostEvents(EventModel[] events)
[HttpPost(nameof(Report))]
public async Task<Object> Report(Int32 id)
{
var node = _node;
if (node == null) throw new ApiException(401, "节点未登录");
var node = _node ?? throw new ApiException(401, "节点未登录");

var cmd = NodeCommand.FindById(id);
if (cmd != null && cmd.NodeID == node.ID)
Expand Down Expand Up @@ -283,9 +282,7 @@ public async Task Notify()
private async Task Handle(WebSocket socket, String token, String ip)
{
var (node, error) = _nodeService.DecodeToken(token, _setting.TokenSecret);
if (node == null) throw new ApiException(401, $"未登录![ip={ip}]");

_node = node;
_node = node ?? throw new ApiException(401, $"未登录![ip={ip}]");
if (error != null) throw error;

var sid = Rand.Next();
Expand Down
4 changes: 2 additions & 2 deletions Stardust.Server/Services/NodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ private static Node CheckNode(Node node, NodeInfo di, String productCode, String
// 网卡地址
if (di.Macs != node.MACs)
{
var dims = di.Macs?.Split(",") ?? new String[0];
var nodems = node.MACs?.Split(",") ?? new String[0];
var dims = di.Macs?.Split(",") ?? [];
var nodems = node.MACs?.Split(",") ?? [];
// 任意网卡匹配则通过
if (!nodems.Any(e => dims.Contains(e)))
{
Expand Down
2 changes: 1 addition & 1 deletion Stardust.Web/Areas/Registry/Controllers/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected override IEnumerable<App> Search(Pager p)
if (id > 0)
{
var node = App.FindById(id);
if (node != null) return new[] { node };
if (node != null) return [node];
}

var projectId = p["projectId"].ToInt(-1);
Expand Down
4 changes: 1 addition & 3 deletions Stardust/AppClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public class AppClient : ClientBase, IRegistry
private readonly ConcurrentDictionary<String, ConsumeServiceInfo> _consumeServices = new();
private readonly ConcurrentDictionary<String, ServiceModel[]> _consumes = new();
private readonly ConcurrentDictionary<String, IList<Delegate>> _consumeEvents = new();
private readonly ConcurrentQueue<AppInfo> _fails = new();
private readonly ICache _cache = new MemoryCache();
#endregion

#region 构造
Expand Down Expand Up @@ -439,7 +437,7 @@ public PublishServiceInfo Register(String serviceName, Func<String?> addressCall
/// <param name="callback">回调方法</param>
public void Bind(String serviceName, ServiceChangedCallback callback)
{
var list = _consumeEvents.GetOrAdd(serviceName, k => new List<Delegate>());
var list = _consumeEvents.GetOrAdd(serviceName, k => []);
list.Add(callback);

StartTimer();
Expand Down
12 changes: 12 additions & 0 deletions Stardust/Services/StarTimeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using NewLife.Remoting.Clients;

namespace Stardust.Services;

#if !NET40
internal class StarTimeProvider : TimeProvider
{
public ClientBase Client { get; set; } = null!;

public override DateTimeOffset GetUtcNow() => Client != null ? Client.GetNow().ToUniversalTime() : base.GetUtcNow();
}
#endif
8 changes: 8 additions & 0 deletions Stardust/StarFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
using NewLife.Remoting.Clients;
using NewLife.Remoting.Models;
using NewLife.Security;
using NewLife.Threading;
using Stardust.Configs;
using Stardust.Models;
using Stardust.Monitors;
using Stardust.Registry;
using Stardust.Services;

#if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD
using TaskEx = System.Threading.Tasks.Task;
#endif
Expand Down Expand Up @@ -290,6 +293,11 @@ private Boolean Valid()
Log = Log,
};

#if !NET40
// 设置全局定时调度器的时间提供者,借助服务器时间差,以获得更准确的时间。避免本地时间偏差导致定时任务执行时间不准确
TimerScheduler.GlobalTimeProvider = new StarTimeProvider { Client = client };
#endif

//var set = StarSetting.Current;
//if (set.Debug) client.Log = XTrace.Log;
client.WriteInfoEvent("应用启动", $"pid={Process.GetCurrentProcess().Id}");
Expand Down
4 changes: 2 additions & 2 deletions Stardust/Stardust.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.Remoting" Version="3.0.2024.710-beta1226" />
<PackageReference Include="NewLife.Remoting" Version="3.0.2024.717-beta0605" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'!='net40'">
<PackageReference Include="NewLife.Core" Version="10.10.2024.0713-beta0409" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.0718-beta0517" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net40'">
<PackageReference Include="NewLife.Core" Version="10.10.2024.0701-net40" />
Expand Down
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Win32.SystemEvents" Version="8.0.0" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.713-beta0409" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.718-beta0517" />
<PackageReference Include="SSH.NET" Version="2024.1.0" />
</ItemGroup>

Expand Down

0 comments on commit 18b0ab3

Please sign in to comment.