jd.cs 7.6 KB

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