我創建了一個云功能,旨在在使用 puppeteer 將網站添加到我的網站集合時截取網站的螢屏截圖,但是我經常遇到 256mb 限制錯誤。我不知道限制是否太小或功能有問題,它不應該達到這個限制。
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");
}
});
});
我注意到這個代碼塊需要很長時間才能完成(幾分鐘而不是幾秒鐘):
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox"],
timeout: 0,
});
然后我檢查日志并在云日志中查看我的控制臺日志陳述句,顯示 puppeteer 啟動確實完成了,但是在此之后我只看到以下錯誤:
容器作業人員超過了 256 MiB 的記憶體限制,在服務 2 個請求后使用了 256 MiB。考慮設定一個更大的實體類。
有 2 個請求,雖然我遇到了完全相同的錯誤,只說明了 1 個請求,但這似乎不正確,這是由于異步函式的性質造成的嗎?
來自函式 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"
uj5u.com熱心網友回復:
如果要使用 puppeteer 之類的軟體,則需要配置更多記憶體。該檔案描述了如何設定它。例如:
exports.onWebsiteCreateTakeScreenshot = functions.firestore
.runWith({
// Ensure the function has enough memory and time
timeoutSeconds: 300,
memory: "1GB",
})
.document('websites/{id}')
.onCreate(async (snap, context) =>{
準備好為此配置在每 CPU 秒上花費更多的錢。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/518677.html
標籤:Google Cloud Collective javascript火力基地谷歌云火库谷歌云功能傀儡师
