JobClusterServer.cs 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core.Service;
  7. /// <summary>
  8. /// 作业集群控制
  9. /// </summary>
  10. public class JobClusterServer : IJobClusterServer
  11. {
  12. private static readonly SqlSugarRepository<SysJobCluster> _sysJobClusterRep = App.GetRequiredService<SqlSugarRepository<SysJobCluster>>();
  13. private readonly Random _random = new(DateTime.Now.Millisecond);
  14. /// <summary>
  15. /// 当前作业调度器启动通知
  16. /// </summary>
  17. /// <param name="context">作业集群服务上下文</param>
  18. public async void Start(JobClusterContext context)
  19. {
  20. // 在作业集群表中,如果 clusterId 不存在,则新增一条(否则更新一条),并设置 status 为 ClusterStatus.Waiting
  21. if (await _sysJobClusterRep.IsAnyAsync(u => u.ClusterId == context.ClusterId))
  22. {
  23. await _sysJobClusterRep.AsUpdateable().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
  24. }
  25. else
  26. {
  27. await _sysJobClusterRep.AsInsertable(new SysJobCluster { ClusterId = context.ClusterId, Status = ClusterStatus.Waiting }).ExecuteCommandAsync();
  28. }
  29. }
  30. /// <summary>
  31. /// 等待被唤醒
  32. /// </summary>
  33. /// <param name="context">作业集群服务上下文</param>
  34. /// <returns><see cref="Task"/></returns>
  35. public async Task WaitingForAsync(JobClusterContext context)
  36. {
  37. var clusterId = context.ClusterId;
  38. while (true)
  39. {
  40. // 控制集群心跳频率(放在头部为了防止 IsAnyAsync continue 没sleep占用大量IO和CPU)
  41. await Task.Delay(3000 + _random.Next(500, 1000)); // 错开集群同时启动
  42. try
  43. {
  44. ICache cache = App.GetRequiredService<ICacheProvider>().Cache;
  45. // 使用分布式锁
  46. using (cache.AcquireLock("lock:JobClusterServer:WaitingForAsync", 1000))
  47. {
  48. // 在这里查询数据库,根据以下两种情况处理
  49. // 1) 如果作业集群表已有 status 为 ClusterStatus.Working 则继续循环
  50. // 2) 如果作业集群表中还没有其他服务或只有自己,则插入一条集群服务或调用 await WorkNowAsync(clusterId); 之后 return;
  51. // 3) 如果作业集群表中没有 status 为 ClusterStatus.Working 的,调用 await WorkNowAsync(clusterId); 之后 return;
  52. if (await _sysJobClusterRep.IsAnyAsync(u => u.Status == ClusterStatus.Working)) continue;
  53. await WorkNowAsync(clusterId);
  54. return;
  55. }
  56. }
  57. catch { }
  58. }
  59. }
  60. /// <summary>
  61. /// 当前作业调度器停止通知
  62. /// </summary>
  63. /// <param name="context">作业集群服务上下文</param>
  64. public async void Stop(JobClusterContext context)
  65. {
  66. // 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
  67. await _sysJobClusterRep.UpdateAsync(u => new SysJobCluster { Status = ClusterStatus.Crashed }, u => u.ClusterId == context.ClusterId);
  68. }
  69. /// <summary>
  70. /// 当前作业调度器宕机
  71. /// </summary>
  72. /// <param name="context">作业集群服务上下文</param>
  73. public async void Crash(JobClusterContext context)
  74. {
  75. // 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
  76. await _sysJobClusterRep.UpdateAsync(u => new SysJobCluster { Status = ClusterStatus.Crashed }, u => u.ClusterId == context.ClusterId);
  77. }
  78. /// <summary>
  79. /// 指示集群可以工作
  80. /// </summary>
  81. /// <param name="clusterId">集群 Id</param>
  82. /// <returns></returns>
  83. private static async Task WorkNowAsync(string clusterId)
  84. {
  85. // 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Working
  86. await _sysJobClusterRep.UpdateAsync(u => new SysJobCluster { Status = ClusterStatus.Working }, u => u.ClusterId == clusterId);
  87. }
  88. }