[Node.js] Availability and Zero-downtime Restarts
It might be possible for our node server has some downtime, no matter it is because server update or simply some crashs in the code. We want to minizie the downtime as much as possible.
1. In case of cluster worker crash, we want master worker fork a new worker:
const http = require('http');
const cluster = require('cluster');
const os = require('os');
if (cluster.isMaster) {
const cpus = os.cpus().length;
console.log(`Forking for ${cpus} CPUs`);
for (let i = ; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
if (code !== 0 && !worker.exitedAfterDisconnect) {
console.log(`Worker ${worker.id} crashed. Starting a new wroker`);
cluster.fork();
}
})
} else {
require('./server');
}
It is important to check 'worker.exitedAfterDisconnect' to see whether is is because crash or because we want to exit one worker.
2. In case of upgrade, we want to restart each worker one by one, to make zero downtime:
// kill -SIGUSR2 <MASTER_PID>
// In case to upgrade, we want to restart each worker one by one
process.on('SIGUSR2', () => {
const workers = Object.values(cluster.workers);
const restartWorker = (workerIndex) => {
const worker = cluster.workers[workerIndex];
if (!worker) return; // On worker exit, we want to restart it, then continue
// with next worker
worker.on('exit', () => {
// If it is because crash, we don't continue
if (!worker.exitedAfterDisconnect) return;
console.log(`Exited process ${worker.process.pid}`);
cluster.fork().on('listening', () => {
restartWorker(workerIndex + );
}); worker.disconnect();
});
}
// Calling restartWorker recursively
restartWorker();
});
In really production, we don't actually need to code cluster by ourselve, we can use PM2 package. but it is important to understand what's happening under hood.
---
const cluster = require('cluster');
const http = require('http');
const os = require('os');
// For runing for the first time,
// Master worker will get started
// Then we can fork our new workers
if (cluster.isMaster) {
const cpus = os.cpus().length;
console.log(`Forking for ${cpus} CPUs`);
for (let i = ; i < cpus; i++) {
cluster.fork();
}
// In case of crash, we want to strat a new worker
cluster.on('exit', (worker, code, signal) => {
if (code !== && !worker.exitedAfterDisconnect) {
console.log(`Worker ${worker.id} crashed. Starting a new wroker`);
cluster.fork();
}
})
// kill -SIGUSR2 <MASTER_PID>
// In case to upgrade, we want to restart each worker one by one
process.on('SIGUSR2', () => {
const workers = Object.values(cluster.workers);
const restartWorker = (workerIndex) => {
const worker = cluster.workers[workerIndex];
if (!worker) return;
// On worker exit, we want to restart it, then continue
// with next worker
worker.on('exit', () => {
// If it is because crash, we don't continue
if (!worker.exitedAfterDisconnect) return;
console.log(`Exited process ${worker.process.pid}`);
cluster.fork().on('listening', () => {
restartWorker(workerIndex + );
});
worker.disconnect();
});
}
// Calling restartWorker recursively
restartWorker();
});
} else {
require('./server');
}
[Node.js] Availability and Zero-downtime Restarts的更多相关文章
- [转]Getting Start With Node.JS Tools For Visual Studio
本文转自:http://www.c-sharpcorner.com/UploadFile/g_arora/getting-started-with-node-js-tools-for-visual-s ...
- Understanding Asynchronous IO With Python 3.4's Asyncio And Node.js
[转自]http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html Introduction I spent this su ...
- [Server Running] [Node.js, PM2] Using PM2 To Keep Your Node Apps Alive
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allow ...
- Node.js(day5)
一.NOSQL NOSQL是Not Only SQL的简称,与关系型数据库对应,一般称为非关系型数据库.关系型数据库遵循ACID规则,而NOSQL存储数据时不需要严格遵循固定的模式,因此在大数据的今天 ...
- Four Node.js Gotchas that Operations Teams Should Know about
There is no doubt that Node.js is one of the fastest growing platforms today. It can be found at sta ...
- Node.js 操作Mongodb
Node.js 操作Mongodb1.简介官网英文文档 https://docs.mongodb.com/manual/ 这里几乎什么都有了MongoDB is open-source docum ...
- node.js使用cluster实现多进程
首先郑重声明: nodeJS 是一门单线程!异步!非阻塞语言! nodeJS 是一门单线程!异步!非阻塞语言! nodeJS 是一门单线程!异步!非阻塞语言! 重要的事情说3遍. 因为nodeJS天生 ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- 利用Node.js的Net模块实现一个命令行多人聊天室
1.net模块基本API 要使用Node.js的net模块实现一个命令行聊天室,就必须先了解NET模块的API使用.NET模块API分为两大类:Server和Socket类.工厂方法. Server类 ...
随机推荐
- bzoj 3611
和BZOJ消耗站一样,先将那个询问的简图构建出来,然后就是简单的树形DP. (倍增数组开小了,然后就狂WA,自己生成的极限数据深度又没有那么高,链又奇迹般正确) #include <cstdio ...
- Hihocoder #1081 最短路径一 dijkstra
#1081 : 最短路径·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 万圣节的早上,小Hi和小Ho在经历了一个小时的争论后,终于决定了如何度过这样有意义的一天—— ...
- servlet注入service业务bean
项目中用到spring容器来管理业务bean,在servlet中就收到前台传递来的请求参数后,调用业务bean,老是出错 部门代码如下 <span style="font-size:1 ...
- MyBatis接口的简单实现原理
MyBatis接口的简单实现原理 用过MyBatis3的人可能会觉得为什么MyBatis的Mapper接口没有实现类,但是可以直接用? 那是因为MyBatis使用Java动态代理实现的接口. 这里仅仅 ...
- Shell获取文件的文件名和扩展名的例子
这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下 basename example.tar.gz .tar.gz # => examp ...
- .NET:CLR via C#:Runtime Serialization
Making a Type Serializable The SerializableAttribute custom attribute may be applied to reference ty ...
- __super
__super::member_function(); The __super keyword allows you to explicitly state that you are calling ...
- copy and paste ,做到这样也很牛逼了
db笔记本 mysql资源 mysql5.1中文参考手册 mysql管理 基于linux使用mysql二进制包安装mysql5.5 mysql client命令行选项 mysqld服务器系统变量和状态 ...
- Spring-4.0 + Quartz-2.2.1 集群实例(Tomcat+Memcached+Quartz集群session共享)还是没有解决Serializable序列化
- Quartz调用大全
Quartz调用大全 1.Quartz应用范围广泛,可单独执行也可在spring中嵌入执行. 类似的定时任务在linux下可以用crontab执行 2.实现代码: QuartzTest :主要执行类 ...