|
|
@@ -19,6 +19,7 @@ using TencentCloud.Omics.V20221128.Models;
|
|
|
using TencentCloud.Csip.V20221121.Models;
|
|
|
using COSXML.Network;
|
|
|
using System.Security.Policy;
|
|
|
+using System.Diagnostics;
|
|
|
|
|
|
|
|
|
namespace molilian.core
|
|
|
@@ -29,35 +30,50 @@ namespace molilian.core
|
|
|
public static bool save_dailys_log = false;
|
|
|
|
|
|
public static string _test_oaid = "3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2";
|
|
|
+
|
|
|
static TkLogCore()
|
|
|
{
|
|
|
int flag = RedisHelper.Get<int>("turn:save_dailys_log");
|
|
|
if (flag == 1) save_dailys_log = true;
|
|
|
}
|
|
|
- public static int BatchInsertLogDB(int limit)
|
|
|
+ private static readonly SemaphoreSlim semaphore = new SemaphoreSlim(10, 10);
|
|
|
+
|
|
|
+ public static async Task<int> BatchInsertLogDBAsync(int limit)
|
|
|
{
|
|
|
- var result = EndPointCore.ProcessEndPointNodes<int>(node =>
|
|
|
+ await semaphore.WaitAsync(); // 等待获取锁
|
|
|
+ try
|
|
|
{
|
|
|
- if (!node.is_public_api) return 0;
|
|
|
-
|
|
|
- if (CenterHub.IsCenter)
|
|
|
- {
|
|
|
- if (node.is_coupon_api) return 0;
|
|
|
- }
|
|
|
- else
|
|
|
+ var result = await Task.Run(() =>
|
|
|
{
|
|
|
- if (!node.is_coupon_api) return 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (string.IsNullOrEmpty(node.redis_server)) return 0;
|
|
|
-
|
|
|
- var redis = RedisClientManager.GetRedisClient(node.redis_server);
|
|
|
- return BatchInsertLogDB(limit, redis);
|
|
|
- });
|
|
|
- return result.Sum();
|
|
|
+ return EndPointCore.ProcessEndPointNodes<int>(node =>
|
|
|
+ {
|
|
|
+ if (!node.is_public_api) return 0;
|
|
|
+
|
|
|
+ if (CenterHub.IsCenter)
|
|
|
+ {
|
|
|
+ if (node.is_coupon_api) return 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (!node.is_coupon_api) return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(node.redis_server)) return 0;
|
|
|
+
|
|
|
+ var redis = RedisClientManager.GetRedisClient(node.redis_server);
|
|
|
+ return BatchInsertLogDB(limit, redis);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ return result.Sum();
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ semaphore.Release(); // 释放锁,允许下一个任务执行
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static int BatchInsertLogDB(int limit, CSRedisClient redis)
|
|
|
+
|
|
|
+ public static int BatchInsertLogDB2(int limit, CSRedisClient redis)
|
|
|
{
|
|
|
int total = 0;
|
|
|
using var connection = DBContext.GetOpenConnection();
|
|
|
@@ -66,35 +82,164 @@ namespace molilian.core
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- //第三方接口写入日志
|
|
|
- total += task_insert_tk_logs(connection, transaction, limit, redis);
|
|
|
-
|
|
|
- total += task_insert_parse_tb_logs(connection, transaction, limit, redis);
|
|
|
-
|
|
|
- total += task_insert_parse_jd_logs(connection, transaction, limit, redis);
|
|
|
+ Stopwatch stopwatch = new Stopwatch(); // 创建一个计时器
|
|
|
+ LoggerLibrary log = new LoggerLibrary("debug", "BatchInsertLogDB"); // 创建日志对象
|
|
|
|
|
|
- total += task_insert_parse_pdd_logs(connection, transaction, limit, redis);
|
|
|
|
|
|
- total += task_insert_parse_dy_logs(connection, transaction, limit, redis);
|
|
|
-
|
|
|
- total += task_insert_parse_tool_logs(connection, transaction, limit, redis);
|
|
|
-
|
|
|
- total += task_insert_parse_deeplink_logs(connection, transaction, limit, redis);
|
|
|
+ //第三方接口写入日志
|
|
|
+ stopwatch.Start();
|
|
|
+ int taskTotal = task_insert_tk_logs(limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_tk_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_tb_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_tb_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_jd_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_jd_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_pdd_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_pdd_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_dy_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_dy_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_tool_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_tool_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_deeplink_logs(limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_deeplink_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_coupon_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_coupon_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_cps_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_cps_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_promotion_img_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_promotion_img_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
+
|
|
|
+ stopwatch.Start();
|
|
|
+ taskTotal = task_insert_parse_ks_logs(connection, transaction, limit, redis);
|
|
|
+ total += taskTotal;
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"task_insert_parse_ks_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ stopwatch.Reset();
|
|
|
|
|
|
- total += task_insert_parse_coupon_logs(connection, transaction, limit, redis);
|
|
|
+ transaction.Commit();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ transaction.Rollback();
|
|
|
|
|
|
- total += task_insert_parse_cps_logs(connection, transaction, limit, redis);
|
|
|
+ _ = new LoggerLibrary("database_error", "parse_log")
|
|
|
+ .Info(ex.Message, ex.StackTrace)
|
|
|
+ .SaveAsync();
|
|
|
|
|
|
- total += task_insert_promotion_img_logs(connection, transaction, limit, redis);
|
|
|
+ NotifyCore.Notify(new NifyMessage
|
|
|
+ {
|
|
|
+ message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
|
|
|
+ priority = NifyMessagePriority.high,
|
|
|
+ tags = ["red_circle"]
|
|
|
+ });
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ connection.Close();
|
|
|
+ }
|
|
|
+ return total;
|
|
|
+ }
|
|
|
|
|
|
+ public static int BatchInsertLogDB(int limit, CSRedisClient redis)
|
|
|
+ {
|
|
|
+ int total = 0;
|
|
|
+ //using var connection = DBContext.GetOpenConnection();
|
|
|
+ //connection.Open();
|
|
|
+ //using var transaction = connection.BeginTransaction();
|
|
|
+ using IDbTransaction transaction = null;
|
|
|
+ using IDbConnection connection = null;
|
|
|
|
|
|
- total += task_insert_parse_ks_logs(connection, transaction, limit, redis);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Stopwatch stopwatch = new Stopwatch(); // 创建一个计时器
|
|
|
+ LoggerLibrary log = new LoggerLibrary("debug", "BatchInsertLogDB"); // 创建日志对象
|
|
|
|
|
|
- transaction.Commit();
|
|
|
+ var tasks = new List<Task<int>>
|
|
|
+ {
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_tk_logs(limit, redis), "task_insert_tk_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_tb_logs(connection, transaction, limit, redis), "task_insert_parse_tb_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_jd_logs(connection, transaction, limit, redis), "task_insert_parse_jd_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_pdd_logs(connection, transaction, limit, redis), "task_insert_parse_pdd_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_dy_logs(connection, transaction, limit, redis), "task_insert_parse_dy_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_tool_logs(connection, transaction, limit, redis), "task_insert_parse_tool_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_deeplink_logs(limit, redis), "task_insert_parse_deeplink_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_coupon_logs(connection, transaction, limit, redis), "task_insert_parse_coupon_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_cps_logs(connection, transaction, limit, redis), "task_insert_parse_cps_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_promotion_img_logs(connection, transaction, limit, redis), "task_insert_promotion_img_logs", log)),
|
|
|
+ Task.Run(() => RunTaskWithLogging(() => task_insert_parse_ks_logs(connection, transaction, limit, redis), "task_insert_parse_ks_logs", log))
|
|
|
+ };
|
|
|
+
|
|
|
+ // 等待所有任务完成
|
|
|
+ Task.WhenAll(tasks).Wait();
|
|
|
+
|
|
|
+ // 计算所有任务的结果总和
|
|
|
+ total = tasks.Select(t => t.Result).Sum();
|
|
|
+
|
|
|
+ //transaction.Commit();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- transaction.Rollback();
|
|
|
+ //transaction.Rollback();
|
|
|
|
|
|
_ = new LoggerLibrary("database_error", "parse_log")
|
|
|
.Info(ex.Message, ex.StackTrace)
|
|
|
@@ -109,11 +254,47 @@ namespace molilian.core
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
- connection.Close();
|
|
|
+ //connection.Close();
|
|
|
}
|
|
|
return total;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ // 新增一个辅助方法来处理日志记录和任务执行
|
|
|
+ private static int RunTaskWithLogging(Func<int> taskFunc, string taskName, LoggerLibrary log)
|
|
|
+ {
|
|
|
+ Stopwatch stopwatch = new Stopwatch();
|
|
|
+ stopwatch.Start();
|
|
|
+ int taskTotal = taskFunc();
|
|
|
+ stopwatch.Stop();
|
|
|
+ log.Info($"{taskName} 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
|
|
|
+ log.SaveAsync();
|
|
|
+ return taskTotal;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static int BatchInsertLogDB(int limit)
|
|
|
+ {
|
|
|
+ var result = EndPointCore.ProcessEndPointNodes<int>(node =>
|
|
|
+ {
|
|
|
+ if (!node.is_public_api) return 0;
|
|
|
+
|
|
|
+ if (CenterHub.IsCenter)
|
|
|
+ {
|
|
|
+ if (node.is_coupon_api) return 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (!node.is_coupon_api) return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(node.redis_server)) return 0;
|
|
|
+
|
|
|
+ var redis = RedisClientManager.GetRedisClient(node.redis_server);
|
|
|
+ return BatchInsertLogDB(limit, redis);
|
|
|
+ });
|
|
|
+ return result.Sum();
|
|
|
+ }
|
|
|
private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
|
|
|
{
|
|
|
saveAccountCache("all", success, message, reason);
|