我已经在数字海洋中部署了一个节点项目。偶尔,特别是在周末,API会停止给出响应,并给我504错误网关超时。当我检查服务器上pm2列表的状态时,它显示状态为联机。我尝试pm2 restart 0或pm2 restart server.js,它开始正常工作,直到这种情况再次发生。 我尝试使用cron解决此问题:
const CronJob = require("cron").CronJob;
const restartCommand = "pm2 restart 0";
const listCommand = "pm2 list";
const { exec } = require('child_process');
const restartApp = function () {
exec(restartCommand, (err, stdout, stderr) => {
if (!err && !stderr) {
console.log(new Date(), `App restarted!!!`);
listApps();
}
else if (err || stderr) {
console.log(new Date(), `Error in executing ${restartCommand}`, err || stderr);
}
});
}
function listApps() {
exec(listCommand, (err, stdout, stderr) => {
// handle err if you like!
console.log(`pm2 list`);
console.log(`${stdout}`);
});
}
new CronJob('0 0 17 * * *', function() {
console.log('5 pm Los_Angeles time, restarting the gmd-server');
restartApp();
}, null, true, 'America/Los_Angeles');