ServerContext.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows.Documents;
  8. using XdCxRhDW.App.Model;
  9. using XdCxRhDW.Dto;
  10. using XdCxRhDW.Repostory;
  11. namespace XdCxRhDW.App
  12. {
  13. class ServerContext
  14. {
  15. public static ServerContext Instance { get; private set; } = new ServerContext();
  16. readonly List<ModelSvr> list = new List<ModelSvr>();
  17. public void Init()
  18. {
  19. Messenger.Defalut.Sub<SvrStateReportDto>("服务状态改变", dto =>
  20. {
  21. try
  22. {
  23. lock (this)
  24. {
  25. if (dto.ReportType == 0)
  26. {
  27. var find = list.Find(p => p.BaseHttpAddr == dto.BaseHttpAddr);
  28. if (find == null)
  29. {
  30. list.Add(new ModelSvr()
  31. {
  32. SvrType = dto.SvrType,
  33. SvrID = dto.SvrID,
  34. BaseHttpAddr = dto.BaseHttpAddr,
  35. SwaggerAddr = dto.SwaggerAddr,
  36. ReportTime = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(SysConfig.Config.ZoneHours)),
  37. });
  38. }
  39. else
  40. {
  41. find.BaseHttpAddr = dto.BaseHttpAddr;
  42. find.SwaggerAddr = dto.SwaggerAddr;
  43. find.SvrType = dto.SvrType;
  44. find.SvrID = dto.SvrID;
  45. find.ReportTime = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(SysConfig.Config.ZoneHours));
  46. }
  47. }
  48. else
  49. {
  50. var find = list.Find(p => p.BaseHttpAddr == dto.BaseHttpAddr);
  51. if (find == null) return;
  52. list.Remove(find);
  53. }
  54. Messenger.Defalut.Pub("服务集合改变", list);
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. XdCxRhDW.Framework.LogHelper.Error("处理服务状态上报异常", ex);
  60. }
  61. });
  62. CheckSvrState();
  63. }
  64. public List<ModelSvr> GetAll()
  65. {
  66. return list.Skip(0).ToList();
  67. }
  68. public List<ModelSvr> GetAll(EnumSvrType type)
  69. {
  70. lock (this)
  71. {
  72. return list.FindAll(p => p.SvrType == type);
  73. }
  74. }
  75. public ModelSvr GetOne(EnumSvrType type, string svrID)
  76. {
  77. lock (this)
  78. {
  79. return list.FirstOrDefault(p => p.SvrType == type && p.SvrID == svrID);
  80. }
  81. }
  82. public ModelSvr GetRandomOne(EnumSvrType type)
  83. {
  84. ModelSvr svr = null;
  85. lock (this)
  86. {
  87. svr = list.Where(p => p.SvrType == type).OrderBy(p => Guid.NewGuid()).FirstOrDefault();
  88. }
  89. if (svr == null)
  90. throw new Exception($"找不到【{type.GetEnumDisplayName()}】");
  91. return svr;
  92. }
  93. private void CheckSvrState()
  94. {
  95. Task.Run(() =>
  96. {
  97. while (true)
  98. {
  99. try
  100. {
  101. lock (this)
  102. {
  103. List<string> tmp = new List<string>();
  104. list.ForEach(t =>
  105. {
  106. if ((DateTime.Now - t.ReportTime).TotalSeconds > 20)
  107. {
  108. tmp.Add(t.BaseHttpAddr);
  109. }
  110. });
  111. list.RemoveAll(p => tmp.Contains(p.BaseHttpAddr));
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. XdCxRhDW.Framework.LogHelper.Error("服务状态检测异常", ex);
  117. }
  118. Thread.Sleep(10000);
  119. }
  120. });
  121. }
  122. }
  123. }