dz_word_async.js 1.9 KB

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