ServerContext.cs 5.3 KB

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