const {MongoManager} = require("./workers/mongo_manager"); const {_android: android} = require("playwright"); const alert = require("alert"); const LinkValidator = require("./workers/LinkValidator"); const mongoManager = new MongoManager(); const schedule = require('node-schedule'); let canContinue = true; function validateLinks() { android.devices().then((devices) => { if (devices.length === 0) { alert("未找到连接的设备"); return } mongoManager.getAllLinkToValidate().then(list => { let segmentNumber = list.length / devices.length; console.log("connected device number:" + devices.length) console.log("segmentNumber:" + segmentNumber) canContinue = false; for (let i = 0; i < devices.length; i++) { startWithList(list.slice(i * segmentNumber, segmentNumber * (i + 1)), devices[i]).then(result => { console.log("stop") canContinue = true; }) } }) }) } async function startWithList(listOfUrls, device) { await listOfUrls.reduce(async (promise, urlToValidate) => { // This line will wait for the last async function to finish. // The first iteration uses an already resolved Promise // so, it will immediately continue. await promise; let commandor = new LinkValidator(device, urlToValidate.url, mongoManager); await commandor.loadPage() console.log("next"); }, Promise.resolve()); } mongoManager.connect().then(r => { const job = schedule.scheduleJob('*/1 * * * *', function () { if (canContinue) { validateLinks() } else { console.log("job is executing, ignored") } }) });