dz_word_async.js 1.8 KB

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