Ver código fonte

尝试修复jd的账号选择bug

dodo hold 1 ano atrás
pai
commit
203d28d1af

+ 1 - 1
molilian.api/Program.cs

@@ -7,7 +7,7 @@ using Microsoft.Extensions.Diagnostics.HealthChecks;
 using Microsoft.AspNetCore.Diagnostics.HealthChecks;
 
 var builder = WebApplication.CreateBuilder(args);
-await builder.Services.InitAsync();
+builder.Services.Init();
 
 builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
 builder.Services.AddControllers(option =>

Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
molilian.api/Properties/PublishProfiles/tester.pubxml.user


+ 1 - 1
molilian.api/molilian.api.csproj

@@ -15,7 +15,7 @@
     <PackageReference Include="dodohold.core" Version="1.0.1.17" />
     <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.0-Preview.1" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
-    <PackageReference Include="YunhuiKit" Version="0.0.14" />
+    <PackageReference Include="YunhuiKit" Version="0.0.16" />
   </ItemGroup>
 
   <ItemGroup>

+ 1 - 1
molilian.api/molilian.api.csproj.user

@@ -2,7 +2,7 @@
 <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <ActiveDebugProfile>http</ActiveDebugProfile>
-    <NameOfLastUsedPublishProfile>E:\project\2024\molilian\server\molilian.api\Properties\PublishProfiles\latest.pubxml</NameOfLastUsedPublishProfile>
+    <NameOfLastUsedPublishProfile>E:\project\2024\molilian\server\molilian.api\Properties\PublishProfiles\tester.pubxml</NameOfLastUsedPublishProfile>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
     <DebuggerFlavor>ProjectDebugger</DebuggerFlavor>

+ 42 - 9
molilian.core/Core/API/ApiAccountCore.cs

@@ -27,7 +27,7 @@ namespace molilian.core
 
     public partial class ApiAccountCore
     {
-        private static readonly object _lockObj = new();
+        private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
         private static IEnumerable<ApiAccountDTO> _cached;
         public static async Task<ApiAccountDTO> GetOneAsync(string api_key)
         {
@@ -37,26 +37,59 @@ namespace molilian.core
         }
 
 
+        //public static async Task<IEnumerable<ApiAccountDTO>> ListAsync(bool force = false)
+        //{
+        //    if (!force && _cached != null) return _cached;
+
+        //    string cache_key = $"cache:api_key";
+        //    var list = await RedisHelper.GetAsync<IEnumerable<ApiAccountDTO>>(cache_key);
+        //    if (force || list == null)
+        //    {
+        //        lock (_lockObj)
+        //        {
+        //            list = new DBContext.Table("api_account").Select<ApiAccountDTO>();
+        //            if (list == null) return default;
+        //            _ = RedisHelper.SetAsync(cache_key, list, 30 * 86400);
+        //        }
+        //    }
+        //    _cached = list;
+        //    return list;
+        //}
+
         public static async Task<IEnumerable<ApiAccountDTO>> ListAsync(bool force = false)
         {
             if (!force && _cached != null) return _cached;
 
-            string cache_key = $"cache:api_key";
-            var list = await RedisHelper.GetAsync<IEnumerable<ApiAccountDTO>>(cache_key);
-            if (force || list == null)
+            try
             {
-                lock (_lockObj)
+                await _semaphore.WaitAsync();
+
+                string cache_key = $"cache:api_key";
+                var list = await RedisHelper.GetAsync<IEnumerable<ApiAccountDTO>>(cache_key);
+
+                if (force || list == null)
                 {
                     list = new DBContext.Table("api_account").Select<ApiAccountDTO>();
                     if (list == null) return default;
-                    _ = RedisHelper.SetAsync(cache_key, list, 30 * 86400);
+
+                    // 等待 Redis 写入完成
+                    await RedisHelper.SetAsync(cache_key, list, 30 * 86400);
                 }
+
+                _cached = list;
+                return list;
+            }
+            catch (Exception)
+            {
+                // 发生异常时返回上一次的缓存,如果没有则返回默认值
+                return _cached ?? default;
+            }
+            finally
+            {
+                _semaphore.Release();
             }
-            _cached = list;
-            return list;
         }
 
-
         public static void Refresh()
         {
             _ = ListAsync(true);

+ 49 - 19
molilian.core/Core/jd/JdPoolCore.cs

@@ -34,7 +34,8 @@ namespace molilian.core
             _end_point = Environment.GetEnvironmentVariable("EndPoint");
         }
 
-        private static readonly object _lockObj = new();
+        private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
+
         private static IEnumerable<JdPoolDTO> _cached;
         private static IEnumerable<JdPoolDTO> _all_cached;
         public static async Task<JdPoolDTO?> GetOneAsync()
@@ -122,49 +123,78 @@ namespace molilian.core
             return true;
         }
 
+
         public static async Task<IEnumerable<JdPoolDTO>> AllListAsync(bool force = false)
         {
             if (!force && _all_cached != default) return _all_cached;
-            string cache_key = $"cache:all_jd_pool";
-            var list = await RedisKit.GetAsync<IEnumerable<JdPoolDTO>>(cache_key);
-            if (force || list == default)
+
+            try
             {
-                lock (_lockObj)
+                await _semaphore.WaitAsync();
+
+                string cache_key = $"cache:all_jd_pool";
+                var list = await RedisKit.GetAsync<IEnumerable<JdPoolDTO>>(cache_key);
+
+                if (force || list == default)
                 {
                     list = new DBContext.Table("jd_pool").Select<JdPoolDTO>();
                     if (list == null) return default;
 
-                    _ = RedisKit.SetAsync(cache_key, list, 30 * 86400);
+                    // 等待 Redis 写入完成
+                    await RedisKit.SetAsync(cache_key, list, 30 * 86400);
                 }
+
+                _all_cached = list;
+                return list;
+            }
+            catch (Exception)
+            {
+                // 发生异常时返回上一次的缓存,如果没有则返回默认值
+                return _all_cached ?? default;
+            }
+            finally
+            {
+                _semaphore.Release();
             }
-            _all_cached = list;
-            return list;
         }
 
         public static async Task<IEnumerable<JdPoolDTO>> ListAsync(bool force = false)
         {
             if (!force && _cached != null) return _cached;
-            string cache_key = $"cache:jd_pool";
-            var list = await RedisKit.GetAsync<IEnumerable<JdPoolDTO>>(cache_key);
-            if (force || list == null)
+
+            try
             {
-                lock (_lockObj)
+                await _semaphore.WaitAsync(); // 异步等待锁
+
+                string cache_key = $"cache:jd_pool";
+                var list = await RedisKit.GetAsync<IEnumerable<JdPoolDTO>>(cache_key);
+
+                if (force || list == null)
                 {
                     list = new DBContext.Table("jd_pool")
                         .Where("status=@status", new { status = 1 })
                         .Select<JdPoolDTO>();
-                    if (list == null) return default;
 
-                    foreach (var item in list)
+                    if (list != null)
                     {
-                        _ = RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
-                        _ = RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
+                        foreach (var item in list)
+                        {
+                            _ = RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id,
+                                DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
+                            _ = RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id,
+                                DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
+                        }
+                        await RedisKit.SetAsync(cache_key, list, 30 * 86400);
                     }
-                    _ = RedisKit.SetAsync(cache_key, list, 30 * 86400);
                 }
+
+                _cached = list;
+                return list;
+            }
+            finally
+            {
+                _semaphore.Release(); // 确保释放锁
             }
-            _cached = list;
-            return list;
         }
 
         public static void Refresh()

+ 16 - 10
molilian.core/Core/log/base.cs

@@ -3,6 +3,7 @@ using CSRedis;
 using System.Data;
 using System.Diagnostics;
 using YunhuiKit;
+using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData;
 
 
 namespace molilian.core
@@ -31,10 +32,10 @@ namespace molilian.core
                     .Where(node => CenterHub.IsCenter ? !node.is_coupon_api : node.is_coupon_api)
                     .Select(async node =>
                     {
-                        var redisServer = EndPointCore.GetRedisServer(node);
-                        if (string.IsNullOrEmpty(redisServer)) return 0;
                         try
                         {
+                            var redisServer = EndPointCore.GetRedisServer(node);
+                            if (string.IsNullOrEmpty(redisServer)) return 0;
                             await using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
                             return BatchInsertLogDB(limit, scope.Client);
                         }
@@ -521,12 +522,12 @@ namespace molilian.core
                     .Where(node => all_node || (CenterHub.IsCenter ? !node.is_coupon_api : node.is_coupon_api))
                     .Select(async node =>
                     {
-                        var redisServer = EndPointCore.GetRedisServer(node);
-                        if (string.IsNullOrEmpty(redisServer))
-                            return 0;
-
                         try
                         {
+                            var redisServer = EndPointCore.GetRedisServer(node);
+                            if (string.IsNullOrEmpty(redisServer))
+                                return 0;
+
                             await using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
                             return await scope.Client.GetAsync<int>(keyname);
                         }
@@ -555,11 +556,16 @@ namespace molilian.core
             {
                 try
                 {
-                    var redisServer = EndPointCore.GetRedisServer(node);
-                    if (string.IsNullOrEmpty(redisServer)) return [];
+                    try
+                    {
+                        var redisServer = EndPointCore.GetRedisServer(node);
+                        if (string.IsNullOrEmpty(redisServer)) return [];
 
-                    using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
-                    return await scope.Client.SMembersAsync<string>(keyname);
+                        using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
+                        return await scope.Client.SMembersAsync<string>(keyname);
+                    }
+                    catch (Exception ex) { }
+                    return [];
                 }
                 catch (Exception ex)
                 {

+ 1 - 0
molilian.core/Core/log/taobao.cs

@@ -237,6 +237,7 @@ namespace molilian.core
                 .Add("elapsedTime", data.elapsedTime)
                 .Add("elapsedTime2", data.elapsedTime2)
                 .Add("elapsedTime3", data.elapsedTime3)
+                .Add("retry_count", data.retry_count)
                 .Add("subCode", data.subCode)
                 .Add("ip", data.ip)
                 .Add("oaid", data.oaid)

+ 41 - 31
molilian.core/Core/taoke/OrderTrackingCore.cs

@@ -71,34 +71,39 @@ namespace molilian.core
                     .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
                     .Select(async node =>
                     {
-                        var redisServer = EndPointCore.GetRedisServer(node);
-                        if (string.IsNullOrEmpty(redisServer)) return null;
+                        try
+                        {
+                            var redisServer = EndPointCore.GetRedisServer(node);
+                            if (string.IsNullOrEmpty(redisServer)) return null;
 
 
-                        using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
-                        var redis = scope.Client;
+                            using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
+                            var redis = scope.Client;
 
-                        // 按优先级依次查询不同的缓存key
-                        TkDataDTO result = null;
+                            // 按优先级依次查询不同的缓存key
+                            TkDataDTO result = null;
 
-                        if (!string.IsNullOrEmpty(mktId))
-                        {
-                            result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:mktId:{mktId}");
-                            if (result != null) return result;
-                        }
-
-                        if (!string.IsNullOrEmpty(itemId))
-                        {
-                            result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemId}");
-                            if (result != null) return result;
+                            if (!string.IsNullOrEmpty(mktId))
+                            {
+                                result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:mktId:{mktId}");
+                                if (result != null) return result;
+                            }
 
-                            if (!string.IsNullOrEmpty(itemTitle))
+                            if (!string.IsNullOrEmpty(itemId))
                             {
-                                result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemTitle}");
+                                result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemId}");
                                 if (result != null) return result;
+
+                                if (!string.IsNullOrEmpty(itemTitle))
+                                {
+                                    result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemTitle}");
+                                    if (result != null) return result;
+                                }
                             }
                         }
-
+                        catch (Exception ex)
+                        {
+                        }
                         return null;
                     });
 
@@ -120,21 +125,26 @@ namespace molilian.core
                     .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
                     .Select(async node =>
                     {
-                        var redisServer = EndPointCore.GetRedisServer(node);
-                        if (string.IsNullOrEmpty(redisServer)) return;
+                        try
+                        {
 
-                        using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
-                        var redis = scope.Client;
+                            var redisServer = EndPointCore.GetRedisServer(node);
+                            if (string.IsNullOrEmpty(redisServer)) return;
 
-                        var keys = new[]
-                        {
-                            $":cache:order_summary:{accountId}:{itemId}",
-                            $":cache:order_summary:{accountId}:mktId:{mktId}",
-                            $":cache:order_summary:{accountId}:{itemTitle}"
-                        }.Where(k => !string.IsNullOrEmpty(k));
+                            using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
+                            var redis = scope.Client;
+
+                            var keys = new[]
+                            {
+                                $":cache:order_summary:{accountId}:{itemId}",
+                                $":cache:order_summary:{accountId}:mktId:{mktId}",
+                                $":cache:order_summary:{accountId}:{itemTitle}"
+                            }.Where(k => !string.IsNullOrEmpty(k));
 
-                        // 批量删除所有相关缓存
-                        await redis.DelAsync(keys.ToArray());
+                            // 批量删除所有相关缓存
+                            await redis.DelAsync(keys.ToArray());
+                        }
+                        catch (Exception ex) { }
                     });
                 await Task.WhenAll(tasks);
             }

+ 23 - 11
molilian.core/Core/taoke/RiskControlCore.cs

@@ -127,12 +127,17 @@ namespace molilian.core
                     .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
                     .Select(async node =>
                     {
-                        var redisServer = EndPointCore.GetRedisServer(node);
-                        if (string.IsNullOrEmpty(redisServer)) return 0;
-
-                        var cacheKey = $"RiskControl:{key}:calls:{flag}";
-                        using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
-                        return await scope.Client.GetAsync<int>(cacheKey);
+                        try
+                        {
+                            var redisServer = EndPointCore.GetRedisServer(node);
+                            if (string.IsNullOrEmpty(redisServer)) return 0;
+
+                            var cacheKey = $"RiskControl:{key}:calls:{flag}";
+                            using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
+                            return await scope.Client.GetAsync<int>(cacheKey);
+                        }
+                        catch (Exception ex) { }
+                        return 0;
                     });
 
                 var results = await Task.WhenAll(tasks);
@@ -179,11 +184,18 @@ namespace molilian.core
                     .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
                     .Select(async node =>
                     {
-                        var redisServer = EndPointCore.GetRedisServer(node);
-                        if (string.IsNullOrEmpty(redisServer)) return false;
-
-                        using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
-                        return await scope.Client.SetAsync(cache_key, income_amt, 3 * 86400);
+                        try
+                        {
+                            var redisServer = EndPointCore.GetRedisServer(node);
+                            if (string.IsNullOrEmpty(redisServer)) return false;
+
+                            using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
+                            return await scope.Client.SetAsync(cache_key, income_amt, 3 * 86400);
+                        }
+                        catch (Exception ex)
+                        {
+                            return false;
+                        }
                     });
 
                 await Task.WhenAll(tasks);

+ 1 - 0
molilian.core/DTO/alimama/TkConfigDTO.cs

@@ -39,6 +39,7 @@ namespace molilian.core
 
         public int tk_limit_per_ip_24h { get; set; } = 0;
         public int tk_limit_per_oaid_24h { get; set; } = 0;
+        public int tk_parse_retry_count { get; set; } = 0;
 
         public string cpsIgnorePercentageCity { get; set; } = string.Empty;
         public int cps_limit_per_ip_24h { get; set; } = 0;

+ 1 - 0
molilian.core/DTO/alimama/TkDataDTO.cs

@@ -91,6 +91,7 @@ namespace molilian.core
         public int elapsedTime { get; set; } = 0;
         public int elapsedTime2 { get; set; } = 0;
         public int elapsedTime3 { get; set; } = 0;
+        public int retry_count { get; set; } = 0;
         public string ip { get; set; } = string.Empty;
         public string oaid { get; set; } = string.Empty;
 

+ 14 - 1
molilian.core/Plus/Alimama/parse.cs

@@ -179,7 +179,20 @@ namespace molilian.core
             }
 
 
-            result = await alimamaParseAsync(result.content, result, cancellationToken);
+            //2025-02-06 增加重试机制
+            int try_count = 0;
+#if DEBUG
+            _config.tk_parse_retry_count = 3;
+#endif
+            while (try_count == 0 || try_count <= _config.tk_parse_retry_count)
+            {
+                if (try_count > 3) break;
+                result = await alimamaParseAsync(result.content, result, cancellationToken);
+                if (!"该链接不支持转化,请更换链接尝试".Equals(result.message)) break;
+                try_count++;
+            }
+            //result = await alimamaParseAsync(result.content, result, cancellationToken);
+            result.retry_count = try_count;
             result.commercial = result.success;
 
             if (!result.success)

+ 2 - 2
molilian.core/middleware/CustomToken.cs

@@ -9,7 +9,7 @@ namespace molilian.core
 {
     public static class CustomToken
     {
-        public static async Task<IServiceCollection> InitAsync(this IServiceCollection services)
+        public static IServiceCollection Init(this IServiceCollection services)
         {
             //int minWorkerThreads = 128;  // 工作线程数的建议设置
             //int minIOCompletionThreads = 512;  // I/O 完成端口线程数的建议设置
@@ -43,7 +43,7 @@ namespace molilian.core
             SystemMonitor.StartMonitoring(30);
 
             var connstr = Environment.GetEnvironmentVariable("RedisConfig") ?? throw new ArgumentNullException("找不到对应的RedisConfig配置!");
-            await RedisKit.InitializeAsync(connstr);
+            RedisKit.Initialize(connstr);
 
 
 

+ 1 - 1
molilian.core/molilian.core.csproj

@@ -25,7 +25,7 @@
     <PackageReference Include="IP2Region.Net" Version="2.0.2" />
     <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
     <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
-    <PackageReference Include="YunhuiKit" Version="0.0.14" />
+    <PackageReference Include="YunhuiKit" Version="0.0.16" />
   </ItemGroup>
 
   <ItemGroup>

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff