Puppeteer works encapsulated with PM2 - is a javascript library that helps on watching node.js scripts - restarting the script when throws errors, etc. The encapsulation could be helpful for your case since there's a configuration for the memory threshold that restarts the entire script when reaches the limit specified, having a minimum downtime. It's super easy to use pm2, you only need to install the library, globally or locally, and then indicates the js file to load.
The library has good documentation for your personal use.
After installed, you need to create an ecosystemfile.js in your project, like these, I have 2 different scripts, but you can put only one
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",
}
}
],
};
After you need to configure your package scripts like these:
"scripts": {
"start": "yarn pm2 start ecosystem.config.js --env production",
"start-dev": "yarn pm2 start ecosystem.config.js"
},
In my case, I have installed it locally.
After that just run yarn start, and the library will load your script. Then you can see the logs, and memory usage using commands: pm2 logs or pm2 monit