I have a simple Puppeteer script that I want to run in an hourly interval. I've taken two approaches 1) using setIntervals 2) using a sleep function. I know there maybe other approaches such as cron jobs and would like to ask what would be the best way to solve this.
- Use set intervals
const runPuppeteer = () => {
puppeteer.launch({
headless: isHeadless,
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
}).then(async browser => {
//Do something...
await browser.close() // close browser after
})}
setInterval(runPuppeteer, 60 * 1000 * 60);
- Use sleep
const runPuppeteer = () => {
puppeteer.launch({
headless: isHeadless,
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
}).then(async browser => {
while(true){
//Do something...
sleep.sleep(60 * 60)
}
})}
For some reason when I took 2) sleep approach and looked at htop there were multiple threads and tasks (I think this is mostly due to my code..)