node.js - 为什么我在 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');
解决方案
推荐阅读
- c - 在 c 中理解这种形式的 for 循环
- java - Springboot 2.1.9.RELEASE - 使用 JPA 持久化具有父子关系的实体
- jquery - 具有特殊日子的完整日历定期事件
- javascript - 如何将@ref 传递给Javascript 中的元素?
- tensorflow - 为什么在 Kubernetes 中运行 Tesorflow 时会出现 Crashloopback?
- linux - Autosar 的 SOME/IP 和 Unix-like 的有什么区别?
- node.js - 如何使用 MERN 堆栈配置玩笑测试?
- python - 考虑顺序如何检查列表(字符串)是否包含另一个列表(字符串)
- javascript - Stripe.createTokenWithCardAsync(params) 不能正常工作
- c++ - 在 XCode 项目 (Objective-C) 中使用静态 C++ 库 (*.a)