簡體   English   中英

Puppeteer(在 Heroku 上):如何防止掛起 Chromium 進程?

[英]Puppeteer (on Heroku): how to prevent hanging Chromium processes?

我們在一個簡單的 Node.js 應用程序中使用 Puppeteer 來“作為服務”創建屏幕截圖。 我們在 Heroku 上運行它,很快我們就看到 Chromium 進程一直掛着。 結果是服務器內存不足,應用程序開始失敗。 這是最近的ps命令的結果:

  PID                    STARTED       TIME   STAT CMD
    1   Sun Jan 19 20:02:19 2020   00:00:00   S    ps-run
    4   Sun Jan 19 20:02:20 2020   00:00:11   Sl   node app.js
  35   Sun Jan 19 20:02:21 2020   00:00:00   Ss   /usr/sbin/sshd -f /app/.ssh/sshd_co
  36   Sun Jan 19 20:02:21 2020   00:00:00   S    bash --login -c node app.js
  73   Sun Jan 19 20:02:22 2020   00:00:24   Sl   /app/.heroku/node/bin/node /app/app
  78   Sun Jan 19 20:02:22 2020   00:00:24   Sl   /app/.heroku/node/bin/node /app/app
  997   Sun Jan 19 20:33:30 2020   00:00:22   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
  999   Sun Jan 19 20:33:30 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
1012   Sun Jan 19 20:33:30 2020   00:02:02   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
1014   Sun Jan 19 20:33:30 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
1021   Sun Jan 19 20:33:30 2020   00:06:44   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
5030   Mon Jan 20 00:29:34 2020   00:00:15   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
5032   Mon Jan 20 00:29:34 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
5045   Mon Jan 20 00:29:34 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
5051   Mon Jan 20 00:29:34 2020   00:01:26   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
5054   Mon Jan 20 00:29:34 2020   00:04:49   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
7088   Mon Jan 20 03:26:01 2020   00:00:10   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
7090   Mon Jan 20 03:26:01 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
7103   Mon Jan 20 03:26:01 2020   00:00:56   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
7105   Mon Jan 20 03:26:01 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
7111   Mon Jan 20 03:26:01 2020   00:03:06   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
10972   Mon Jan 20 07:02:55 2020   00:00:04   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
10974   Mon Jan 20 07:02:55 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
10988   Mon Jan 20 07:02:55 2020   00:00:22   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
10990   Mon Jan 20 07:02:55 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
10996   Mon Jan 20 07:02:55 2020   00:01:13   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
11660   Mon Jan 20 07:23:37 2020   00:00:03   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
11662   Mon Jan 20 07:23:37 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
11677   Mon Jan 20 07:23:38 2020   00:00:19   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
11679   Mon Jan 20 07:23:38 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
11687   Mon Jan 20 07:23:38 2020   00:01:03   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
12132   Mon Jan 20 07:29:31 2020   00:00:03   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
12134   Mon Jan 20 07:29:31 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
12147   Mon Jan 20 07:29:31 2020   00:00:17   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
12148   Mon Jan 20 07:29:31 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
12156   Mon Jan 20 07:29:31 2020   00:00:59   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
12790   Mon Jan 20 07:56:29 2020   00:00:02   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
12792   Mon Jan 20 07:56:29 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
12805   Mon Jan 20 07:56:29 2020   00:00:13   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
12808   Mon Jan 20 07:56:29 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
12814   Mon Jan 20 07:56:29 2020   00:00:44   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
13268   Mon Jan 20 08:02:03 2020   00:00:02   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
13270   Mon Jan 20 08:02:03 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
13282   Mon Jan 20 08:02:03 2020   00:00:12   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
13285   Mon Jan 20 08:02:03 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
13293   Mon Jan 20 08:02:03 2020   00:00:43   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
13862   Mon Jan 20 08:23:30 2020   00:00:00   Ssl  /app/node_modules/puppeteer/.local-chromium/linux-686
13864   Mon Jan 20 08:23:30 2020   00:00:00   S    /app/node_modules/puppeteer/.local-chromium/linux-686
13885   Mon Jan 20 08:23:35 2020   00:00:05   Sl   /app/.heroku/node/bin/node /app/app
13891   Mon Jan 20 08:23:36 2020   00:00:00   Sl   /app/node_modules/puppeteer/.local-chromium/linux-686
15045   Mon Jan 20 08:39:23 2020   00:00:00   S    ssh -o ServerAliveInterval=30 -o Se
17156   Mon Jan 20 09:09:33 2020   00:00:00   Ss   sshd: u55786 [priv]
17158   Mon Jan 20 09:09:33 2020   00:00:00   R    sshd: u55786@pts/0
17159   Mon Jan 20 09:09:34 2020   00:00:00   Ss   -bash
17769   Mon Jan 20 09:20:49 2020   00:00:00   R+   ps -eo pid,lstart,time,stat,cmd

應用程序代碼非常簡單:

(async () => {
  try {
    const browser = await puppeteer.launch({
      defaultViewport: { width: windowSize.width, height: windowSize.height },
      ignoreHTTPSErrors: true,
      args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--disable-infobars', '--hide-scrollbars', '--no-default-browser-check', '--bwsi']
    });

    const page = await browser.newPage();
    await page.setDefaultTimeout(20000);
    await page.goto(urlToScreenshot);
    await page.waitForFunction(jsReadyTest, { polling: 500 });
    await page.waitFor(500);
    await page.screenshot({ type: "jpeg", quality: 70 }).then(function (buffer) {
      res.setHeader('Content-Disposition', 'attachment;filename="' + urlToScreenshot + '.jpg"');
      res.setHeader('Content-Type', 'image/jpg');
      res.send(buffer);
    });

    await browser.close();
  } catch (exception) {
    res.send('Runtime exception!');
  }
})();

請注意,我們添加了 20 秒超時以確保應用程序(以及瀏覽器)在 Heroku 在 30 秒后終止請求之前結束。

我們可以做些什么來確保所有 Chromium 進程始終正確終止?

您可以使用--no-zygote命令參數。 根據提交

--no-zygote可用於完全禁用 zygote 進程的使用。 在不需要和/或不切實際維護分叉服務器(例如 zygote)的環境中運行 Headless Chrome 時需要這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM