Reboot assetImage browser when it disappears

Been seeing this in testing in prod, the first few images worked great, but then eventually they all started saying the browser was disconnected.

Here, we add a check to reconnect if it goes missing. This is actually kinda hard to test in dev, because the dev server creates a new process every time the function runs, so fingers crossed!

I also added explicit logic to close the page when we're done with it, I'm worried we crashed the browser by exceeding the RAM limit by leaving pages open. Not sure quite how their model works and whether things eventually get flushed out on their own!
This commit is contained in:
Emi Matchu 2021-08-19 23:38:25 -07:00
parent f036890aa1
commit fc52439387

View file

@ -37,6 +37,13 @@ const ASSET_IMAGE_PAGE_BASE_URL = process.env.VERCEL_URL
// parallel will be a problem for our API endpoint. // parallel will be a problem for our API endpoint.
let BROWSER; let BROWSER;
async function getBrowser() { async function getBrowser() {
// Sometimes, the browser crashes. Maybe a RAM thing? If that happened, set
// it to null, and then we'll replace it with a new instance below.
if (BROWSER && !BROWSER.isConnected()) {
console.info("Browser is no longer connected; rebooting.");
BROWSER = null;
}
if (!BROWSER) { if (!BROWSER) {
if (process.env["NODE_ENV"] === "production") { if (process.env["NODE_ENV"] === "production") {
// In production, we use a special chrome-aws-lambda Chromium. // In production, we use a special chrome-aws-lambda Chromium.
@ -102,6 +109,7 @@ async function loadAndScreenshotImage(libraryUrl, size) {
const page = await browser.newPage(); const page = await browser.newPage();
console.debug("Page opened, navigating to: " + assetImagePageUrl.toString()); console.debug("Page opened, navigating to: " + assetImagePageUrl.toString());
try {
await page.goto(assetImagePageUrl.toString()); await page.goto(assetImagePageUrl.toString());
console.debug("Page loaded, awaiting image"); console.debug("Page loaded, awaiting image");
@ -125,6 +133,10 @@ async function loadAndScreenshotImage(libraryUrl, size) {
`${JSON.stringify(Object.keys(firstResultFromPage))}` `${JSON.stringify(Object.keys(firstResultFromPage))}`
); );
} }
} finally {
// Close the page, to save on RAM in our shared browser instance.
page.close();
}
} }
async function screenshotImageFromPage(page) { async function screenshotImageFromPage(page) {