DigitalOcean 液滴上的 Puppeteer 脚本 运行 24/7 内存耗尽并崩溃

Puppeteer script on DigitalOcean droplet running 24/7 maxes out memory and crashes

我有一个简单的人偶脚本,我将 运行ning 留在每月 5 美元的 digitalocean droplet 上。脚本如下。该脚本登录到电子商务网站,并不断刷新页面以查找特定交易(如果找到则添加到购物车和结帐)。从监控图中可以看出,每次循环内存都会增加,直到达到最大值,然后使脚本崩溃。我认为这里的内存是RAM,而不是存储。

任何人都可以建议一个更好的方法来配置 puppeteer 脚本来解决这个问题吗?循环大约需要 5 秒到 运行,运行s 大约需要 12 小时,直到内存使用量增加到循环 运行 时间从每 [=23] 5 秒逐渐增加的程度=] 每 运行 30 秒。然后最终内存达到最大值并使脚本崩溃。

每次 运行 关闭并重新打开浏览器是否可以解决内存问题? 是因为我将脚本 运行ning 作为一个永无止境的 .js 文件吗?

Droplet内存截图

const puppeteer = require('puppeteer-extra')
puppeteer.use(require('puppeteer-extra-plugin-stealth')())
const fs = require('fs');
const performance = require('perf_hooks').performance;


(async () => {
  const browser = await puppeteer.launch({
    args: ['--no-sandbox'],
    headless: true
  })
  const page = await browser.newPage()
 
  //Login to page
  await page.goto('https://example.com/login', {waitUntil: 'load', timeout: 0});
  await page.type('[name=email]','xxx@xxx.com');
  await page.click('button[type="submit"]');
  await page.waitForTimeout('input[name=password]');
  await page.type('[name=password]','xxx');
  await page.click('button[type="submit"]');

  var runcount = 0
  var foundcount = 0

  //loops always running after logging in
  while(true){
    var t0 = performance.now();  
    
    await page.goto('https://www.example.com/xxx', {waitUntil: 'load', timeout: 0});
    
    //do some await page.$$eval here
    
  
    runcount++;
    var t1 = performance.now();
    console.log("Run: " + runcount + " | Runtime: " + Math.floor((t1 - t0)/1000);
  
  }
 
  await browser.close()
})()

Puppeteer 使用 PM2 封装 - 是一个 javascript 库,有助于观看 node.js 脚本 - 在抛出错误等时重新启动脚本。封装可能对您有所帮助这种情况是因为有内存阈值的配置,当达到指定的限制时重新启动整个脚本,停机时间最短。 pm2超级好用,只需要在全局或本地安装库,然后指定加载js文件即可。

图书馆有很好的 documentation 供您个人使用。

安装后,你需要在你的项目中创建一个ecosystemfile.js,像这样,我有2个不同的脚本,但你可以只放一个

module.exports = {
  apps : [{
    name: "server-centrodonto",
    script: "./dist/server.js",
    exp_backoff_restart_delay: 100,
    max_memory_restart: '200M',
    watch: false, 
    exec_mode  : "cluster",
    instances: 1,
    env: {
      "NODE_ENV": "development",
      "APP_NAME": "server-centrodonto",
      "PORT": 3030,
      "HOST": "localhost",
      "DATABASE": "centrodonto-database-development"
    },
    env_production : {
      "NODE_ENV": "production",
      "APP_NAME": "server-centrodonto",
      "PORT": 3030,
      "HOST": "localhost",
      "DATABASE": "centrodonto-database"
   }
  },
  {
    name: "wpp-bot-atendente_virtual-centrodonto", 
    // here you put your js file location
    script: "./dist/whatsappBot/AtendenteVirtual_bot.js",
    exp_backoff_restart_delay: 100,
    max_memory_restart: '200M',
    watch: false, 
    exec_mode  : "cluster",
    instances: 1,
    env: {
      "NODE_ENV": "development",
      "APP_NAME": "WPPbot_atendente virtual",
    },
    env_production : {
       "NODE_ENV": "production",
       "APP_NAME": "WPPbot_atendente virtual",
    }
  }
],

};

在您需要像这样配置您的包脚本之后:

 "scripts": {
    "start": "yarn pm2 start ecosystem.config.js --env production",
    "start-dev": "yarn pm2 start ecosystem.config.js"
  },

就我而言,我已经在本地安装了它。

之后 运行 yarn 启动,库将加载您的脚本。然后你可以使用命令查看日志和内存使用情况:pm2 logs 或 pm2 monit