我有一个使用Puppeteer在前台运行的Web抓取程序。我需要杀死一个异步函数puppeteer(不是NODE.JS PROCESS)。为什么我要杀死它?因为此Web抓取需要每天执行一次,并且我想杀死它只是为了防止由于某种原因(如果仍在运行)同一功能被执行多次。
这是我的代码:
class Webscraping {
start(){
this.scrappeWithPuppeteer()
this.runPuppeteerAgainDaily()
}
scrappeWithPuppeteer() {
// asynchronous code
}
runPuppeteerAgainDaily() {
// I'm usingthe npm package "node-schedule"
schedule.scheduleJob('5 52 21 * * *', () => {
// KILL CURRENT this.scrappeWithPuppeteer()
// and then
this.scrappeWithPuppeteer()
})
}
}
let webscrapper = new Webscraping()
webscraping.start()
答案 0 :(得分:2)
schedule.scheduleJob是 node-schedule NPM模块的一部分。
如果您查看文档,https://www.npmjs.com/package/node-schedule
您可以看到以下代码取消了预定的作业:
job.cancel();
换句话说,请跟踪您的工作,以便以后可以通过上述功能将其取消。