jd.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 async Task<int> InsertParseJDLogAsync(int limit, YunhuiKit.RedisClient 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 = await redis.LPopAsync<JdDataDTO>(queue_parse_jd_key);
  21. if (data == null) break;
  22. if (TestParseCore.InWhitelist(data.ip, data.oaid))
  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 int task_insert_parse_jd_logs(int limit, CSRedisClient redis)
  62. {
  63. int total = 0;
  64. using var connection = DBContext.GetOpenConnection();
  65. connection.Open();
  66. using var transaction = connection.BeginTransaction();
  67. try
  68. {
  69. for (int i = 0; i < limit; i++)
  70. {
  71. var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
  72. if (data == null) break;
  73. if (TestParseCore.InWhitelist(data.ip, data.oaid))
  74. {
  75. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  76. }
  77. else
  78. {
  79. //save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
  80. //if (save_dailys_log)
  81. {
  82. string daily_table = $"jd_parse_logs_{data.create_time:yyyyMMdd}";
  83. save_jd_parse_logs(data, daily_table, connection, transaction);
  84. }
  85. if (data.elapsedTime > 1000)
  86. {
  87. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  88. }
  89. if (data.success)
  90. {
  91. save_jd_parse_logs(data, "jd_parse_logs_success", connection, transaction);
  92. }
  93. }
  94. total++;
  95. }
  96. transaction.Commit();
  97. }
  98. catch (Exception ex)
  99. {
  100. transaction.Rollback();
  101. new LoggerLibrary("TkLogCore_error", "task_insert_parse_jd_logs")
  102. .Info(ex.Message, ex.StackTrace)
  103. .SaveAsync();
  104. throw;
  105. }
  106. finally
  107. {
  108. connection.Close();
  109. }
  110. return total;
  111. }
  112. public static async Task ParseLogAsync(JdDataDTO response)
  113. {
  114. try
  115. {
  116. var ts = DateTime.Now - response.create_time;
  117. response.elapsedTime = (int)ts.TotalMilliseconds;
  118. _ = RedisKit.RPushAsync(queue_parse_jd_key, response);
  119. if (!TestParseCore.InWhitelist(response.ip, response.oaid))
  120. {
  121. await SaveParseCacheAsync(response.channel.ToString(), response.accountId,
  122. response.accountName, response.success, response.message, response.reason,
  123. response.deeplink_url);
  124. }
  125. if (response.success) await saveClientRequestTotalAsync(response.channel, response.ip, response.oaid);
  126. if (response.success || response.message.Equals("转链失败"))
  127. {
  128. await RiskControlCore.CallsIncrByAsync(response.channel, response.accountId);
  129. //saveClientRequestTotal(response.channel, response.ip, response.oaid);
  130. }
  131. if (!response.success && ("nologin".Equals(response.reason) ||
  132. "方法不存在".Equals(response.reason) ||
  133. "未登录".Equals(response.reason)))
  134. {
  135. await JdPoolCore.DisabledAsync(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  136. }
  137. if ("没有匹配账号".Equals(response.reason))
  138. {
  139. await JdPoolCore.AccountExhaustedAsync();
  140. }
  141. string failure_key = ":total:continuous_failure";
  142. string failure_lock_key = ":lock:continuous_failure";
  143. if (response.success) await RedisKit.DelAsync(failure_key);
  144. //连续错误发送报警
  145. if ("签名失败".Equals(response.reason) || "网络异常".Equals(response.reason))
  146. {
  147. var total = await RedisKit.IncrByAsync(failure_key);
  148. await RedisKit.ExpireAsync(failure_key, 3600);
  149. if (total > 30)
  150. {
  151. //没有锁定记录才发送,
  152. string lockfFlag = await RedisKit.GetAsync<string>(failure_lock_key);
  153. if (string.IsNullOrEmpty(lockfFlag))
  154. {
  155. //发送锁,保存半小时(半小时内只发送1次)
  156. await RedisKit.SetAsync(failure_lock_key, 1, 1800);
  157. string message = $"【京东{response.accountName}】{EndPointCore.CurrentEndPoint},连续的{response.reason}";
  158. await RedisKit.ExpireAsync(failure_key, 3600);
  159. NotifyCore.Notify(new NifyMessage
  160. {
  161. message = message,
  162. priority = NifyMessagePriority.high,
  163. tags = ["red_circle"]
  164. });
  165. NotifyCore.AnPushNotify(response.reason, message);
  166. }
  167. }
  168. }
  169. }
  170. catch (Exception ex) { }
  171. }
  172. private static int save_jd_parse_logs(JdDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  173. {
  174. return new DBContext.Table(connection, tablename)
  175. .Add("end_point", data.end_point)
  176. .Add("channel", (int)data.channel)
  177. .Add("accountId", data.accountId)
  178. .Add("accountName", data.accountName)
  179. .Add("proxy_node", data.proxy_node)
  180. .Add("rawContent", data.rawContent)
  181. .Add("success", data.success)
  182. .Add("message", data.message)
  183. .Add("reason", data.reason)
  184. .Add("content", data.content)
  185. .Add("itemId", data.itemId)
  186. .Add("itemName", data.itemName)
  187. .Add("pic", data.pic)
  188. .Add("couponAmount", data.couponAmount)
  189. .Add("promotionPrice", data.promotionPrice)
  190. .Add("taoToken", data.taoToken)
  191. .Add("shortLinkurl", data.shortLinkurl)
  192. .Add("deeplink_url", data.deeplink_url)
  193. .Add("elapsedTime", data.elapsedTime)
  194. .Add("subCode", data.subCode)
  195. .Add("ip", data.ip)
  196. .Add("oaid", data.oaid)
  197. .Add("create_time", data.create_time)
  198. .Create(DBContext.InsertType.NORMAL, transaction);
  199. }
  200. }
  201. }