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

Error starting Chrome with puppeteer when deploying headless-chrome functions example #1395

Closed
mattwelke opened this issue Jul 6, 2019 · 10 comments
Assignees

Comments

@mattwelke
Copy link

I was looking at the documentation for Cloud Functions (https://cloud.google.com/functions/docs/concepts/nodejs-10-runtime) and wanted to deploy one of the example functions in this repo, the headless-chrome example. My deploy command was based on what I was reading in the docs site:

gcloud functions deploy screenshot --runtime nodejs10 --trigger-http --project <redacted>

The deploy succeeds, but then in the web browser when I visit the URL, the function displays the expected message if I don't provide the URL query string parameter:

?url=https://example.com
Please provide URL as GET parameter, for example:

But it fails if I do provide a URL query string parameter, displaying the following message in the browser:

Error: could not handle the request

And the following message in the Stackdriver logs for the function:

TimeoutError: Timed out after 30000 ms while trying to connect to Chrome! The only Chrome revision guaranteed to work is r672088 at Timeout.onTimeout (/srv/functions/node_modules/puppeteer/lib/Launcher.js:351:14) at ontimeout (timers.js:436:11) at tryOnTimeout (timers.js:300:5) at listOnTimeout (timers.js:263:5) at Timer.processTimers (timers.js:223:10)

Despite the error message mentioning "30000 ms", this happens within a few seconds.

@mattwelke
Copy link
Author

I was able to get the function to work properly (where I see an image in the response when I give it a URL), I had to change my code to the following:

'use strict';

// [START functions_headless_chrome]
const puppeteer = require('puppeteer');

exports.screenshot = async (req, res) => {
  console.log('Beginning function')

  const browser = await puppeteer.launch({
    headless: true,
    args: ['--no-sandbox'],
  });
  console.log('Puppeteer launched');

  const page = await browser.newPage();
  console.log('Puppeteer page created');

  const {url} = req.query;

  if (!url) {
    return res.send(
      'Please provide URL as GET parameter, for example: <a href="?url=https://example.com">?url=https://example.com</a>'
    );
  }

  await page.goto(url);
  const imageBuffer = await page.screenshot();
  console.log('Screenshot created');

  res.set('Content-Type', 'image/png');
  res.send(imageBuffer);
};
// [END functions_headless_chrome]

I found that I had to add the headless: true option as I launch Puppeteer (which is recommended in the Puppeteer docs), and I had to get rid of the main function since it wasn't running (confirmed by adding a console.log in it and seeing nothing in the logs). Instead, as you can see in the code above, I had to have the Puppeteer commands run each time a request comes in. I had to make both of these changes to get it to work.

@fhinkel
Copy link
Contributor

fhinkel commented Jul 8, 2019

@Welkie Thanks for reporting the issue. @ace-n , this is a problem with one of the functions examples, can you have a look? cc @steren

@fhinkel fhinkel assigned fhinkel and ace-n and unassigned fhinkel Jul 8, 2019
@ace-n
Copy link
Contributor

ace-n commented Jul 24, 2019

This sample isn't used in the docs. @labtopia @stew-r should we fix it + add it there, or remove it from this repo?

@ace-n
Copy link
Contributor

ace-n commented Jul 24, 2019

(Followed up internally - @labtopia and I decided to add this to the docs, but we aren't really sure where to put it.)

@stew-r
Copy link
Contributor

stew-r commented Jul 24, 2019 via email

@mattwelke
Copy link
Author

I actually found this example really interesting. The performance isn't great, but that's exactly why I wanted to use Cloud Functions for this. I have a use case where I need to interact with a website that doesn't have an API, so I could use Puppeteer. And I could use a FaaS service so that I don't have to worry about running out of memory on a VM or App Engine etc from more than one user performing the request that runs Puppeteer at a time.

@fhinkel fhinkel closed this as completed Sep 1, 2019
@danoc
Copy link

danoc commented Sep 3, 2019

HI! I noticed that the examples were removed a few days ago in #1461.

Is Puppeteer still supported in Cloud Functions?

@clement128
Copy link

Same questions? I am planing to use it !!

@ace-n
Copy link
Contributor

ace-n commented Sep 3, 2019 via email

@steren
Copy link
Contributor

steren commented Sep 3, 2019

I confirm that the OS packages needed to run Headless Chrome (and thus puppeteer) will keep being present in the Cloud Functions environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants