[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类 ...
随机推荐
- luoguP4279 [SHOI2008]小约翰的游戏 Anti-SG 博弈论
这就是一个Anti SG问题 当整个游戏的\(sg = 0\)时,如果不存在单一游戏局面\(sg > 1\),那么先手必胜 当整个游戏的\(sg \neq 0\)时,如果至少存在一个单一游戏局面 ...
- 【20181031T1】一串数字【分解质因数+贪心】
题面 [错解] 立方就是所有质因子次数都是3的倍数嘛 发现1e5的三次根很小,可以枚举所有和这个数乘起来是完全立方数的(flag*1) 然后--连条边跑最大独立集? 不对啊是NP问题(实际上是个二分图 ...
- [BZOJ3928/4048]Outer space invaders
[BZOJ3928/4048]Outer space invaders 题目大意: 有\(n(n\le300)\)个物品,第\(i\)个物品会在\(a_i\sim b_i\)时刻出现,且离你的距离为\ ...
- [CodeChef-QTREE6]Query on a tree VI
题目大意: 给你一棵黑白树,每个点默认是白色,要求支持以下两种操作: 1.改变一个点的颜色: 2.除去连接不同颜色的点的边,求某个点连通块的大小. 思路: 对原树维护两个树链剖分, 一棵维护当点x为白 ...
- 网站(Web)压测工具Webbench源码分析
一.我与webbench二三事 Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能.Webbench ...
- DOM事件绑定方式
普通事件可以直接绑定 比如document.onmouseover=fn; 或者document.addEventListener("mouseover",fn,flase); a ...
- Hyper-V创建固定大小虚拟机
1.新建硬盘 点击确定,就创建好了一个固定大小的vhd文件,下面我们开始创建虚拟机. 2.创建虚拟机 输入虚拟机名称 选择第一代虚拟机 我这里给虚拟机分配512MB内存 网络配置 在这之前我们已经创建 ...
- linux 定时任务 crontab 详细解释(转)
cron 是linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: 引用: /sbin/service crond start //启动服务 /sbin/service cr ...
- 强烈推荐:Android史上最强大的自定义任务软件Tasker
强烈推荐:Android史上最强大的自定义任务软件Taskerhttp://bbs.mumayi.com/thread-28387-1-1.html(出处: 木蚂蚁手机乐园) Android上的Tas ...
- java基础学习总结——Object类
一.Object类介绍