How to interrupt puppeteer-cluster execution inside an infinite loop?

268 Views Asked by At

I'm learning how to use Puppeteer cluster and I have a question.

How can I interrupt a puppeteer cluster execution running in an infinite loop, by using a key press?

The code would be something like this:

const { Cluster } = require('puppeteer-cluster');

const fs = require('fs').promises;
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function run() {
    const cluster = await Cluster.launch({
        concurrency: Cluster.CONCURRENCY_CONTEXT,
        maxConcurrency: 2,
        monitor: true,
    });

    await cluster.task(async ({ page, data: acc }) => {
        // Do task ~2 minutes
    });

    // In case of problems, log them
    cluster.on('taskerror', (err, data) => {
        console.log(`  Error crawling ${data}: ${err.message}`);
    });

    // Read the accs.csv file from the current directory
    const csvFile = await fs.readFile(__dirname + '/accs.csv', 'utf8');
    const lines = csvFile.split('\n');
    while(true){
        //for each account in the file
        for (let i = 0; i < lines.length; i++) {
            const line = lines[i];
            cluster.queue(line);
        }
        
        // sleep for a moment...
        await sleep(60000);
    }
    

    await cluster.idle();
    await cluster.close();
};

try{
    run();
} catch(e) {
    console.log(e.message());
}
1

There are 1 best solutions below

0
On

I manage to do it as I usually do, using readline. I thought it didn't work because of the monitor shown on the terminal.

If anyone need an example on how it's done, check this: Break infinite loop user input nodejs