EndPointCore.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Spire.Pdf.Exporting.XPS.Schema;
  10. using System.Xml.Linq;
  11. using System.Net;
  12. using TencentCloud.Mrs.V20200910.Models;
  13. using static molilian.core.TkPoolCore;
  14. using Org.BouncyCastle.Tls;
  15. namespace molilian.core
  16. {
  17. public partial class EndPointCore
  18. {
  19. private static readonly object _lockObj = new();
  20. private static IEnumerable<EndPointDTO> _cached;
  21. public static EndPointDTO GetOne(string node)
  22. {
  23. var list = List();
  24. if (!list.Any()) return null;
  25. var item = list.Where(s => s.name == node).FirstOrDefault();
  26. return item;
  27. }
  28. public static IEnumerable<EndPointDTO> List(bool force = false)
  29. {
  30. if (!force && _cached != null) return _cached;
  31. string cache_key = $"cache:end_point";
  32. var list = RedisHelper.Get<IEnumerable<EndPointDTO>>(cache_key);
  33. if (force || list == null)
  34. {
  35. lock (_lockObj)
  36. {
  37. list = new DBContext.Table("end_point")
  38. .Where("status=@status", new { status = 1 })
  39. .Select<EndPointDTO>();
  40. if (list == null) return default;
  41. RedisHelper.Set(cache_key, list, 30 * 86400);
  42. }
  43. }
  44. _cached = list;
  45. return list;
  46. }
  47. public static void Refresh()
  48. {
  49. _ = List(true);
  50. }
  51. public static List<T> ProcessEndPointNodes<T>(Func<EndPointDTO, T> nodeAction, bool force = false)
  52. {
  53. var resultList = new List<T>();
  54. var list = List(force);
  55. foreach (var node in list)
  56. {
  57. var result = nodeAction(node);
  58. if (result != null) resultList.Add(result);
  59. }
  60. return resultList;
  61. }
  62. public static async Task ProcessEndPointNodesAsync(Func<EndPointDTO, Task> nodeAction, bool force = false)
  63. {
  64. var list = List(force);
  65. var tasks = new List<Task>();
  66. foreach (var node in list)
  67. {
  68. tasks.Add(nodeAction(node));
  69. }
  70. await Task.WhenAll(tasks);
  71. }
  72. public static async Task NotifyReload(bool onlyAccount = false)
  73. {
  74. await ProcessEndPointNodesAsync(async node =>
  75. {
  76. try
  77. {
  78. string url = $"{node.api_server}Task_70160bd632/reload";
  79. if (onlyAccount) url = $"{node.api_server}Task_70160bd632/reloadAccount";
  80. await new WebClientUtility().RequestAsync(url);
  81. }
  82. catch (Exception ex)
  83. {
  84. _ = new LoggerLibrary("NotifyReload", "error")
  85. .Info(node.Convert2Json())
  86. .Info(ex.Message, ex.StackTrace)
  87. .SaveAsync();
  88. NotifyCore.Notify(new NifyMessage
  89. {
  90. message = $"【NotifyReload异常】{node.description}\n{ex.Message}\n{ex.StackTrace}",
  91. priority = NifyMessagePriority.high,
  92. tags = ["red_circle"]
  93. });
  94. }
  95. });
  96. }
  97. }
  98. }