OnetapInc / chromy

Chromy is a library for operating headless chrome. 🍺🍺🍺
MIT License
605 stars 41 forks source link

Simutaneous screenshots doesn't work beyond 2 instances of chromy #121

Open bhagn opened 4 years ago

bhagn commented 4 years ago

I have seen the existing issues about running chromy in parallel - but my issues seems to be a bit weird. When there are more than 2 instances of chromy created simultaneously to take screenshot of a different URLs - randomly just 2 of them work, and the remaining don't even respond. The chrome processes for the remaining ones just continue running. Here's the sample code - works fine on mac, seeing the issue on ubuntu 18.04

router.get('/export/:id', async function() {
  try {
    await exportPage(req.params.id);
    res.json({result: 'success'})
  } catch(e) {
    res.status(500);
    res.json({result: 'failure'})
  }
});
async function exportPage(id) {
  let url = 'https://localhost:8000/page/id';  // some valid URL
  let port = await getPort();

  let chromy = new Chromy({
     visible: false,
     port: port,
     launchBrowser: true,
     chromeFlags: [
       '--ignore-certificate-errors',
       '--allow-insecure-localhost',
       '--window-size=1200,800'
     ]
  });

  return chromy.chain()
    .goto(url)
    .wait(10000)
    .evaluate(_ => {
         // some browser specific JS evaluation, 
    })  // all instances run till here with no issues. 
    .screenshotDocument()  // only 2 instances succeed in running beyond this.
    .result(screenshot => {
        // write screenshot to file
    })
    .end()
    .then(_ => {
        console.log('Successful');
    })
    .catch(err => {
        console.error(err);
        throw err;
    })
    .finally(_ => {
        chromy.close();
    });
}