dz_word_async.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // if(data!=null){
  26. // }
  27. let totalWordSize = data.words
  28. let totalChapterNum = data.totalChapterNum
  29. await connection.execute(
  30. `UPDATE video_product SET totalChapterNum = "${totalChapterNum}" , words = "${totalWordSize}" WHERE id = ${video_product_info.id} `
  31. );
  32. }
  33. }catch(e){
  34. if(e==0){
  35. right_status = false
  36. }
  37. console.error("processTask error:",e)
  38. } finally{
  39. if(right_status){
  40. global.setTimeout(processTask, 1000);
  41. }
  42. }
  43. }
  44. CMD.init = async function(){
  45. redis_help.connect((results)=>{
  46. if(results){
  47. processTask();
  48. }
  49. })
  50. }
  51. CMD.init()