dz_word_async.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const config = require("../etc/config.json");
  2. const mysql = require('mysql2/promise');
  3. const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
  4. const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
  5. const redis_help = require('../src/use_redis');
  6. const CMD = {}
  7. async function processTask(){
  8. let right_status = true
  9. let connection = null
  10. try{
  11. connection = await mysql.createConnection({
  12. ...taskdbConfig,
  13. multipleStatements: true
  14. });
  15. //先获取所有没有父id的七猫免费书籍
  16. const [rows] = await connection.execute(
  17. `SELECT * FROM video_product WHERE book_platform = 4 AND totalChapterNum =0 LIMIT 500`
  18. );
  19. if(rows.length<=0){
  20. throw 0
  21. }
  22. for (let index = 0; index < rows.length; index++) {
  23. let video_product_info = rows[index]
  24. let data = await require('../src/api/dz/dz_search_book').search_id(video_product_info.product_id)
  25. let totalWordSize = data.words
  26. let totalChapterNum = data.totalChapterNum
  27. await connection.execute(
  28. `UPDATE video_product SET totalChapterNum = "${totalChapterNum}" , words = "${totalWordSize}" WHERE id = ${video_product_info.id} `
  29. );
  30. }
  31. }catch(e){
  32. if(e==0){
  33. right_status = false
  34. }
  35. console.error("processTask error:",e)
  36. } finally{
  37. if(right_status){
  38. global.setTimeout(processTask, 1000);
  39. }
  40. }
  41. }
  42. CMD.init = async function(){
  43. redis_help.connect((results)=>{
  44. if(results){
  45. processTask();
  46. }
  47. })
  48. }
  49. CMD.init()