如何在Google Cloud Functions上使用Apify [英] How to use Apify on Google Cloud Functions
问题描述
我正在使用Apify作为Google Cloud Functions部署一些代码.触发后,云功能将以静默方式终止.我在做什么错了?
I'm deploying some code using Apify as Google Cloud Functions. When triggered, the Cloud Function terminates silently. What am I doing wrong?
我有一些使用Apify 0.15.1的工作代码.它在本地运行良好.部署为Google Cloud Function后,它会静默失败,没有任何明显的错误.使用Puppeteer 1.18.1的等效代码可以正常工作.
I have some working code using Apify 0.15.1. It runs fine locally. Once deployed as a Google Cloud Function, it fails silently without any clear error. The equivalent code using Puppeteer 1.18.1 works fine.
我已在下面使用更简单的代码重现了该问题.尽管此示例并非严格要求Apify,但我希望能够使用Apify提供的额外功能.
I've reproduced the issue using more simple code below. While this example doesn't strictly require Apify, I would like to be able to use the extra functionality provided by Apify.
使用Apify的代码:
Code using Apify:
const Apify = require("apify");
exports.screenshotApify = async (req, res) => {
let imageBuffer;
Apify.main(async () => {
const browser = await Apify.launchPuppeteer({ headless: true });
const page = await browser.newPage();
await page.goto("https://xenaccounting.com");
imageBuffer = await page.screenshot({ fullPage: true });
await browser.close();
});
if (res) {
res.set("Content-Type", "image/png");
res.send(imageBuffer);
}
return imageBuffer;
};
使用操纵up的代码:
const puppeteer = require("puppeteer");
exports.screenshotPup = async (req, res) => {
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
await page.goto("https://xenaccounting.com");
const imageBuffer = await page.screenshot({ fullpage: true });
await browser.close();
if (res) {
res.set("Content-Type", "image/png");
res.send(imageBuffer);
}
return imageBuffer;
};
一旦部署为Google Cloud Function(带有--trigger-http和--memory = 2048),Puppeteer变体就可以正常工作,而Apify变体会无提示地无提示终止(除了'ok'/HTTP 200返回)值).
Once deployed as a Google Cloud Function (with --trigger-http and --memory=2048), the Puppeteer variant works fine, while the Apify variant terminates silently without result (apart from an 'ok' / HTTP 200 return value).
推荐答案
摆脱 Apify.main()
函数,它会将调用安排在以后的时间,直到您的函数已经返回了结果.
Get rid of the Apify.main()
function, it schedules the call to a later time, after your function already returned the result.
这篇关于如何在Google Cloud Functions上使用Apify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!