1

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.

  1. 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);

  1. 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..)

htop image

Tieson T.
  • 20,774
  • 6
  • 77
  • 92

1 Answers1

0

Maybe this will help you design a more sophisticated cron jobs

const cron = require('node-cron')
const execute = require('child_process').exec
const puppeteer = require('puppeteer')
const processIDs = []

const createBrowser = async () => {
    const browser = await puppeteer.launch()
    processIDs.push(browser.process().pid)
    return browser
}

const closeBrowser = async (browser) => {
    await browser.close()
    for (let i = 0; i < processIDs.length; i++) {
        execute(`echo ${process.env.PASSWD} | sudo -S kill -9 ${processIDs[i]}`)
    }
}

const runHourly = async () => {
    const browser = await createBrowser()
    const [page] = await browser.pages()
    const response = await page.goto(url)
    const screenshot = await page.screenshot({
        path: 'screenshot_' + Date.now() + '.png'
    })
    const closeTab = await page.close()
    const exit = await closeBrowser(browser)
}

cron.schedule('0 * * * *', () => {
    console.log('running every minute 0 / hourly')
    await runHourly()
})
Edi Imanto
  • 2,119
  • 1
  • 11
  • 17