首页 > 解决方案 > 为什么我在 Node Js 中的后端项目停止在数字海洋中工作?

问题描述

我在数字海洋中部署了一个节点项目。偶尔,尤其是在周末,api 停止给出响应并给我 504 错误网关超时。当我在服务器上检查 pm2 list 的情况时,它显示状态为在线。我尝试了 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');

标签: node.jsdigital-oceanpm2

解决方案


推荐阅读