0

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 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.