Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse Puppeteer browser and page for text parser #388

Closed
humphd opened this issue Nov 30, 2019 · 10 comments
Closed

Reuse Puppeteer browser and page for text parser #388

humphd opened this issue Nov 30, 2019 · 10 comments
Assignees

Comments

@humphd
Copy link
Contributor

humphd commented Nov 30, 2019

In #383 we are moving to use Puppeteer. This uses a real Chromium browser to render content in a full DOM. We can reuse the browser and page objects, so we don't have more than one instance. We just have to use a Promise chain pattern, see puppeteer/puppeteer#486 (comment):

const browser = await puppeteer.launch();
const page = await browser.newPage();
let chain = Promise.resolve();
// Request handler 
app.get('/pdf', (req, res) => {
  // Handle request generation in the end of the chain
  chain = chain.then(async () => { 
    await page.goto(req.query.url);
    const pdf = await page.pdf();
    res.send(pdf);
  });
});

Depends on #383.

@sukhbeersingh sukhbeersingh self-assigned this Dec 1, 2019
@sukhbeersingh
Copy link
Contributor

I'll give this a go, once #383 is landed.

@humphd
Copy link
Contributor Author

humphd commented Dec 2, 2019

Turns out we're going to need this sooner rather than later. I'm using it in #408, and without it, it's going to use too many resources to run the server. Let's do this in class together tomorrow morning.

@humphd
Copy link
Contributor Author

humphd commented Dec 2, 2019

This might be overkill, but we could maybe use the puppeteer-cluster module, which does a bunch of this for free.

This is sort of what we're doing: https://github.com/thomasdondorf/puppeteer-cluster/blob/HEAD/examples/function-queuing-complex.js.

cc @giatuongtran9

@ragnarokatz
Copy link
Contributor

ragnarokatz commented Dec 3, 2019

@sukhbeersingh This is the updated code:

Current state:

module.exports = async function(htmlFragment) {
  let browser;
  try {
    browser = await puppeteer.launch();
    const page = await browser.newPage();
  }
...

Target state:

  1. instantiate a browser instance to be used
  2. use the same browser instance for all function calls
  3. close the browser instance after all text parsing services have completed

@giatuongtran9
Copy link
Contributor

@humphd sorry for my late reply. Do you still want to use puppeteer-cluster? I can have a look at it

@sukhbeersingh
Copy link
Contributor

Hey @giatuongtran9 if you want to try this please go ahead. I would love to work on this but I have some assignments backlog for other courses so I won’t be able to touch this until next week.

@sukhbeersingh sukhbeersingh removed their assignment Dec 5, 2019
@ragnarokatz
Copy link
Contributor

@giatuongtran9 I can work with you on this issue.

@ragnarokatz
Copy link
Contributor

@humphd can I take on this issue and assign it to myself? @giatuongtran9 seems to be busy and hasn't responded yet.

Don't worry about merging this PR in a hurry if you have other PRs to merge for Friday night/over the weekend.

@humphd
Copy link
Contributor Author

humphd commented Dec 7, 2019

@ragnarokatz go for it. I think others are slammed with other work. I'd love a PR for this.

@humphd
Copy link
Contributor Author

humphd commented Jan 20, 2020

This was fixed by #540.

@humphd humphd closed this as completed Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants