如果我运行多个puppeteer实例,heroku会崩溃吗?



所以我创建了一个简单的node.js应用程序来自动生成pdf并使用puppeteer发送电子邮件。在我的本地服务器上,一切都工作得很好,但是一旦我部署到heroku,如果我试图创建超过2个pdf,服务器就会超时。因此,如果我只在我的heroku应用程序上创建2个pdf,它就可以正常工作,但一旦我试图生成更多的pdf,应用程序就会超时。

下面是我用来生成PDF的循环:

for (let x = 1; x <= numTickets; x++) {
console.log(x, " / ", numTickets);
try {
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});
const page = await browser.newPage();
//compile html
const content = await compile(template, {
billing,
id: id + "-" + `${x}`,
campaign,
});

options.attachments.push({
filename: `${billing.first_name}-housedoubleup-${id}-${x}.pdf`,
path: `./${billing.first_name}-housedoubleup-${id}-${x}.pdf`,
contentType: "application/pdf",
});
await page.setContent(content);
await page.emulateMediaType("screen");
await page.pdf({
path: `${billing.first_name}-housedoubleup-${id}-${x}.pdf`,
format: "a5",
printBackground: true,
landscape: true,
});
console.log("done");
} catch (e) {
console.log("Error -> ", e);
}
if (x === numTickets) {
sendEmail(options);
}
}

我想知道我的heroku免费层上的512MB RAM是否可能限制了生成的pdf的其余部分。

如果有人有任何想法如何帮助或什么可能会出错,我真的很感激:)

每次迭代,您的循环都会创建一个带有新页面的新浏览器实例。试着用,

  1. 单个浏览器实例
  2. 整个循环中的单个页面实例。

最新更新