| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using Spire.Pdf.Exporting.XPS.Schema;
- using System.Xml.Linq;
- using System.Net;
- using TencentCloud.Mrs.V20200910.Models;
- using static molilian.core.TkPoolCore;
- using Org.BouncyCastle.Tls;
- namespace molilian.core
- {
- public partial class EndPointCore
- {
- private static readonly object _lockObj = new();
- private static IEnumerable<EndPointDTO> _cached;
- public static EndPointDTO GetOne(string node)
- {
- var list = List();
- if (!list.Any()) return null;
- var item = list.Where(s => s.name == node).FirstOrDefault();
- return item;
- }
- public static IEnumerable<EndPointDTO> List(bool force = false)
- {
- if (!force && _cached != null) return _cached;
- string cache_key = $"cache:end_point";
- var list = RedisHelper.Get<IEnumerable<EndPointDTO>>(cache_key);
- if (force || list == null)
- {
- lock (_lockObj)
- {
- list = new DBContext.Table("end_point")
- .Where("status=@status", new { status = 1 })
- .Select<EndPointDTO>();
- if (list == null) return default;
- RedisHelper.Set(cache_key, list, 30 * 86400);
- }
- }
- _cached = list;
- return list;
- }
- public static void Refresh()
- {
- _ = List(true);
- }
- public static List<T> ProcessEndPointNodes<T>(Func<EndPointDTO, T> nodeAction, bool force = false)
- {
- var resultList = new List<T>();
- var list = List(force);
- foreach (var node in list)
- {
- var result = nodeAction(node);
- if (result != null) resultList.Add(result);
- }
- return resultList;
- }
- public static async Task ProcessEndPointNodesAsync(Func<EndPointDTO, Task> nodeAction, bool force = false)
- {
- var list = List(force);
- var tasks = new List<Task>();
- foreach (var node in list)
- {
- tasks.Add(nodeAction(node));
- }
- await Task.WhenAll(tasks);
- }
- public static async Task NotifyReload(bool onlyAccount = false, CancellationToken cancellationToken = default)
- {
- await ProcessEndPointNodesAsync(async node =>
- {
- try
- {
- string url = $"{node.api_server}Task_70160bd632/reload";
- if (onlyAccount) url = $"{node.api_server}Task_70160bd632/reloadAccount";
- await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);
- }
- catch (Exception ex)
- {
- _ = new LoggerLibrary("NotifyReload", "error")
- .Info(node.Convert2Json())
- .Info(ex.Message, ex.StackTrace)
- .SaveAsync();
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【NotifyReload异常】{node.description}\n{ex.Message}\n{ex.StackTrace}",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- }
- });
- }
- }
- }
|