Home > other >  Firebase Cloud Function memory limit fail
Firebase Cloud Function memory limit fail

Time:10-20

I've created a cloud function that aims to take a screenshot of a website when it's added to my websites collection using puppeteer, however I'm constantly hitting a 256mb limit error. I don't know whether the limit is too small or there's an issue with the function and it shouldn't ever be hitting this limit.

exports.onWebsiteCreateTakeScreenshot = functions.firestore
    .document('websites/{id}')
    .onCreate(async (snap, context) =>{
      const webpageDocRef = admin.firestore().collection("websites").doc(context.params.id);
      webpageDocRef.get().then(async (snapshot) => {
        if (!snapshot.empty) {
          const url = snapshot.data()["url"];

          try {
            console.log("Launching puppeteer");
            const browser = await puppeteer.launch({
              headless: true,
              args: ["--no-sandbox"],
              timeout: 0,
            });
            console.log("puppeteer launch complete");
            const page = await browser.newPage();
            await page.setViewport({width: 640, height: 480});
            await page.goto(url, {waitUntil: 'networkidle2'});
            const bucket = admin.storage().bucket("thumbnails");
            const file = bucket.file(context.params.id   ".png");
            const screenshotBuffer = await page.screenshot();
            await file.save(screenshotBuffer);
            await browser.close();
          } catch (e) {
            console.error("Error: "   e);
          }
        } else {
          console.log("Error: No documents found");
        }
      });
    });

I noticed that this code block take a long time to complete (minutes rather than seconds):

const browser = await puppeteer.launch({
              headless: true,
              args: ["--no-sandbox"],
              timeout: 0,
            });

Then I check the logs and see my console log statement in the cloud logs showing that the puppeteer launch did complete, however after this I just see the following error:

Container worker exceeded memory limit of 256 MiB with 256 MiB used after servicing 2 requests total. Consider setting a larger instance class.

There 2 requests, that doesn't seem right although I have had the exact same error stating only 1 request, is this due to the nature of the async functions?

relevant info from functions package.json:

"engines": { "node": "16" }, "main": "index.js", "dependencies": { "firebase-admin": "^10.0.2", "firebase-functions": "^3.18.0", "puppeteer" : "^18.2.1", "chromium" : "^3.0.3"

CodePudding user response:

You will need to configure more memory if you want to use a piece of software like puppeteer. The documentation describes how to set that. For example:

exports.onWebsiteCreateTakeScreenshot = functions.firestore
    .runWith({
      // Ensure the function has enough memory and time
      timeoutSeconds: 300,
      memory: "1GB",
    })
    .document('websites/{id}')
    .onCreate(async (snap, context) =>{

Be prepared to also spend more money per CPU-second for this configuration.

  • Related