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的更多相关文章

  1. [转]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 ...

  2. 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 ...

  3. [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 ...

  4. Node.js(day5)

    一.NOSQL NOSQL是Not Only SQL的简称,与关系型数据库对应,一般称为非关系型数据库.关系型数据库遵循ACID规则,而NOSQL存储数据时不需要严格遵循固定的模式,因此在大数据的今天 ...

  5. 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 ...

  6. Node.js 操作Mongodb

    Node.js 操作Mongodb1.简介官网英文文档  https://docs.mongodb.com/manual/  这里几乎什么都有了MongoDB is open-source docum ...

  7. node.js使用cluster实现多进程

    首先郑重声明: nodeJS 是一门单线程!异步!非阻塞语言! nodeJS 是一门单线程!异步!非阻塞语言! nodeJS 是一门单线程!异步!非阻塞语言! 重要的事情说3遍. 因为nodeJS天生 ...

  8. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  9. 利用Node.js的Net模块实现一个命令行多人聊天室

    1.net模块基本API 要使用Node.js的net模块实现一个命令行聊天室,就必须先了解NET模块的API使用.NET模块API分为两大类:Server和Socket类.工厂方法. Server类 ...

随机推荐

  1. BZOJ.2339.[HNOI2011]卡农(思路 DP 组合 容斥)

    题目链接 \(Description\) 有\(n\)个数,用其中的某些数构成集合,求构造出\(m\)个互不相同且非空的集合(\(m\)个集合无序),并满足每个数总共出现的次数为偶数的方案数. \(S ...

  2. [POI2000]Repetitions

    题目大意: 求多个字符串的LCS. 思路: 同SPOJ-LCS2,不过因为SPOJ上数据比较水,当时用错误的写法过掉了,这次用正确的写法重新过了一遍. 拓扑排序按照每个状态的len值,用计数排序实现. ...

  3. tsinsen A1333

    可以用二维树状数组套值域线段树来做,复杂度:O( (n*n+q) * logn logn log10^9 ) 但作为作为整体二分的例题,还是用整体二分来写了一下.对整体二分有一点感觉了. 整体二分,顾 ...

  4. UVALive 6257 Chemist's vows

    #include<iostream> #include<string.h> #include<stdio.h> #include<ctype.h> #i ...

  5. hdoj 5182 PM2.5 排序

    PM2.5 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Descr ...

  6. jQuery和$、jQuery(function(){})和(function(){})(jQuery)

    1.$是JQuery的别名 ,在使用上是一样的,两者可以互换位置. $==jQuery; //true $===jQuery; //true 2.jQuery(function(){ ....... ...

  7. 总结下git中一些常用命令

    一.目录操作 1.cd 即change directory,改变目录,如 cd d:/www,切换到d盘的www目录. 2.cd .. cd+空格+两个点,回退到上一目录. 3.pwd 即 print ...

  8. 图之Dijkstra算法

    Dijkstra算法是一种求单源最短路的算法,即从一个点开始到所有其他点的最短路.其步骤如下: c语言实现如下:(使用邻接矩阵存储) #include <stdio.h> #include ...

  9. VB.NET章鱼哥出品—怎样解决MDI子窗口被父窗口中的控件覆盖的问题

    近期有个网友问我这个问题,我就上网搜了下,结果非常失望.有几个在CSDN上发的求助帖.看到最后都没有找到明白的答案. 这里笔者在网上找到了API函数SetParent(),并对网上的错误进行了改动,并 ...

  10. [Asp.net MVC]Bundle合并,压缩js、css文件

    摘要 在web优化中有一种手段,压缩js,css文件,减少文件大小,合并js,css文件减少请求次数.asp.net mvc中为我们提供一种使用c#代码压缩合并js和css这类静态文件的方法. 一个例 ...