jd.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using dodohold.core;
  2. using CSRedis;
  3. using System.Data;
  4. using YunhuiKit;
  5. namespace molilian.core
  6. {
  7. public partial class TkLogCore
  8. {
  9. static string queue_parse_jd_key = "queue:parse_logs:jd";
  10. public static int task_insert_parse_jd_logs(int limit, CSRedisClient redis)
  11. {
  12. int total = 0;
  13. using var connection = DBContext.GetOpenConnection();
  14. connection.Open();
  15. using var transaction = connection.BeginTransaction();
  16. try
  17. {
  18. for (int i = 0; i < limit; i++)
  19. {
  20. var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
  21. if (data == null) break;
  22. if (_test_oaid.Equals(data.oaid) ||
  23. data.ip.StartsWith("127.0.0"))
  24. {
  25. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  26. }
  27. else
  28. {
  29. //save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
  30. //if (save_dailys_log)
  31. {
  32. string daily_table = $"jd_parse_logs_{data.create_time:yyyyMMdd}";
  33. save_jd_parse_logs(data, daily_table, connection, transaction);
  34. }
  35. if (data.elapsedTime > 1000)
  36. {
  37. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  38. }
  39. if (data.success)
  40. {
  41. save_jd_parse_logs(data, "jd_parse_logs_success", connection, transaction);
  42. }
  43. }
  44. total++;
  45. }
  46. transaction.Commit();
  47. }
  48. catch (Exception ex)
  49. {
  50. transaction.Rollback();
  51. new LoggerLibrary("TkLogCore_error", "task_insert_parse_jd_logs")
  52. .Info(ex.Message, ex.StackTrace)
  53. .SaveAsync();
  54. throw;
  55. }
  56. finally
  57. {
  58. connection.Close();
  59. }
  60. return total;
  61. }
  62. public static async Task ParseLogAsync(JdDataDTO response)
  63. {
  64. try
  65. {
  66. var ts = DateTime.Now - response.create_time;
  67. response.elapsedTime = (int)ts.TotalMilliseconds;
  68. _ = RedisKit.RPushAsync(queue_parse_jd_key, response);
  69. if (!response.ip.StartsWith("127.0.0"))
  70. {
  71. saveParseCache(response.channel.ToString(), response.accountId,
  72. response.accountName, response.success, response.message, response.reason,
  73. response.deeplink_url);
  74. }
  75. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  76. if (response.success || response.message.Equals("转链失败"))
  77. {
  78. RiskControlCore.CallsIncrBy(response.channel, response.accountId);
  79. //saveClientRequestTotal(response.channel, response.ip, response.oaid);
  80. }
  81. if (!response.success && ("nologin".Equals(response.reason) ||
  82. "方法不存在".Equals(response.reason) ||
  83. "未登录".Equals(response.reason)))
  84. {
  85. await JdPoolCore.DisabledAsync(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  86. }
  87. if ("没有匹配账号".Equals(response.reason))
  88. {
  89. await JdPoolCore.AccountExhaustedAsync();
  90. }
  91. string failure_key = ":total:continuous_failure";
  92. string failure_lock_key = ":lock:continuous_failure";
  93. if (response.success) await RedisKit.DelAsync(failure_key);
  94. //连续错误发送报警
  95. if ("签名失败".Equals(response.reason) || "网络异常".Equals(response.reason))
  96. {
  97. var total = await RedisKit.IncrByAsync(failure_key);
  98. await RedisKit.ExpireAsync(failure_key, 3600);
  99. if (total > 30)
  100. {
  101. //没有锁定记录才发送,
  102. string lockfFlag = await RedisKit.GetAsync<string>(failure_lock_key);
  103. if (string.IsNullOrEmpty(lockfFlag))
  104. {
  105. //发送锁,保存半小时(半小时内只发送1次)
  106. await RedisKit.SetAsync(failure_lock_key, 1, 1800);
  107. string message = $"【京东{response.accountName}】{EndPointCore.CurrentEndPoint},连续的{response.reason}";
  108. await RedisKit.ExpireAsync(failure_key, 3600);
  109. NotifyCore.Notify(new NifyMessage
  110. {
  111. message = message,
  112. priority = NifyMessagePriority.high,
  113. tags = ["red_circle"]
  114. });
  115. NotifyCore.AnPushNotify(response.reason, message);
  116. }
  117. }
  118. }
  119. }
  120. catch (Exception ex) { }
  121. }
  122. private static int save_jd_parse_logs(JdDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  123. {
  124. return new DBContext.Table(connection, tablename)
  125. .Add("end_point", data.end_point)
  126. .Add("channel", (int)data.channel)
  127. .Add("accountId", data.accountId)
  128. .Add("accountName", data.accountName)
  129. .Add("proxy_node", data.proxy_node)
  130. .Add("rawContent", data.rawContent)
  131. .Add("success", data.success)
  132. .Add("message", data.message)
  133. .Add("reason", data.reason)
  134. .Add("content", data.content)
  135. .Add("itemId", data.itemId)
  136. .Add("itemName", data.itemName)
  137. .Add("pic", data.pic)
  138. .Add("couponAmount", data.couponAmount)
  139. .Add("promotionPrice", data.promotionPrice)
  140. .Add("taoToken", data.taoToken)
  141. .Add("shortLinkurl", data.shortLinkurl)
  142. .Add("deeplink_url", data.deeplink_url)
  143. .Add("elapsedTime", data.elapsedTime)
  144. .Add("subCode", data.subCode)
  145. .Add("ip", data.ip)
  146. .Add("oaid", data.oaid)
  147. .Add("create_time", data.create_time)
  148. .Create(DBContext.InsertType.NORMAL, transaction);
  149. }
  150. }
  151. }