const config = require("../etc/config.json"); const helper = require("../src/helper"); const redis_help = require('../src/use_redis'); const mysql = require('mysql2/promise'); const PullDataService = require('../src/PullDataService'); var pullDataService = new PullDataService(redis_help) const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql const CMD = {} async function processTask(){ let connection = null try{ const today = helper.getLocalDate(); let date = today.replace(/-/g, ''); let table_name = `pull_data_${date}`; // 创建数据库连接 connection = await mysql.createConnection({ ...dbConfig, multipleStatements: true }); // 检查表是否存在 const [tableCheck] = await connection.execute( `SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`, [dbConfig.database, table_name] ); if (tableCheck.length === 0) { // 表不存在 console.error(`表 ${table_name} 不存在`); throw new Error(`表 ${table_name} 不存在`); } else { // 表存在,执行查询 const [rows] = await connection.execute( `SELECT * FROM ${table_name} WHERE status = 0 LIMIT 500` ); if(rows.length<=0){ throw "没有数据处理!" } let materialId_list = [] rows.forEach((row, index) => { materialId_list.push(row.materialId) }); let response = await pullDataService.get_detail(materialId_list) console.log("response:", response.data.list.length) if(response.success){ for (let index = 0; index < parseInt(response.data.list.length); index++) { const element = response.data.list[index]; console.log("element:",element) if(element.videoId){ let landingUrl = element.landingUrl == undefined ?"":decodeURIComponent(element.landingUrl) await connection.execute( `UPDATE ${table_name} SET videoId = ?, status = 1 , landingUrl = ? , publishTime = ? WHERE uniqueId = ?`, [element.videoId,landingUrl,new Date(element.publishTime),element.uniqueId] ); } } } } }catch(e){ console.error("processTask error:",e) } finally{ global.setTimeout(processTask, 1000); if(connection!=null){ await connection.end(); } } } CMD.init = async function(){ redis_help.connect((results)=>{ if(results){ processTask(); } }) } CMD.init()