ServerContext.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. namespace XdCxRhDW.App
  11. {
  12. class ServerContext
  13. {
  14. public static ServerContext Instance { get; private set; } = new ServerContext();
  15. List<ModelSvr> list = new List<ModelSvr>();
  16. public void Init()
  17. {
  18. Messenger.Defalut.Sub<SvrStateReportDto>("服务状态改变", dto =>
  19. {
  20. try
  21. {
  22. lock (this)
  23. {
  24. if (dto.ReportType == 0)
  25. {
  26. var find = list.Find(p => p.BaseHttpAddr == dto.BaseHttpAddr);
  27. if (find == null)
  28. {
  29. list.Add(new ModelSvr()
  30. {
  31. SvrType = dto.SvrType,
  32. SvrID = dto.SvrID,
  33. BaseHttpAddr = dto.BaseHttpAddr,
  34. SwaggerAddr = dto.SwaggerAddr,
  35. ReportTime = DateTime.Now,
  36. });
  37. }
  38. else
  39. {
  40. find.BaseHttpAddr = dto.BaseHttpAddr;
  41. find.SwaggerAddr = dto.SwaggerAddr;
  42. find.SvrType = dto.SvrType;
  43. find.SvrID = dto.SvrID;
  44. find.ReportTime = DateTime.Now;
  45. }
  46. }
  47. else
  48. {
  49. var find = list.Find(p => p.BaseHttpAddr == dto.BaseHttpAddr);
  50. if (find == null) return;
  51. list.Remove(find);
  52. }
  53. }
  54. Messenger.Defalut.Pub("服务集合改变", list);
  55. }
  56. catch (Exception ex)
  57. {
  58. Serilog.Log.Error(ex, "处理服务状态上报异常");
  59. }
  60. });
  61. CheckSvrState();
  62. }
  63. public List<ModelSvr> GetAll()
  64. {
  65. return list.Skip(0).ToList();
  66. }
  67. public List<ModelSvr> GetAll(EnumSvrType type)
  68. {
  69. lock (this)
  70. {
  71. return list.FindAll(p => p.SvrType == type);
  72. }
  73. }
  74. public ModelSvr GetOne(EnumSvrType type, string svrID)
  75. {
  76. lock (this)
  77. {
  78. return list.FirstOrDefault(p => p.SvrType == type && p.SvrID == svrID);
  79. }
  80. }
  81. public ModelSvr GetOne(EnumSvrType type)
  82. {
  83. lock (this)
  84. {
  85. return list.FirstOrDefault(p => p.SvrType == type);
  86. }
  87. }
  88. private void CheckSvrState()
  89. {
  90. Task.Run(() =>
  91. {
  92. while (true)
  93. {
  94. try
  95. {
  96. lock (this)
  97. {
  98. List<string> tmp = new List<string>();
  99. list.ForEach(t =>
  100. {
  101. if ((DateTime.Now - t.ReportTime).TotalSeconds > 20)
  102. {
  103. tmp.Add(t.BaseHttpAddr);
  104. }
  105. });
  106. list.RemoveAll(p => tmp.Contains(p.BaseHttpAddr));
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. Serilog.Log.Error(ex, "服务状态检测异常");
  112. }
  113. Thread.Sleep(10000);
  114. }
  115. });
  116. }
  117. }
  118. }