• [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 = 0; 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 + 1);
                    });
    
                    worker.disconnect();
                });
            }
            // Calling restartWorker recursively
            restartWorker(0);
        });

    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 = 0; i < cpus; i++) {
            cluster.fork();
        }
    
        // In case of crash, we want to strat a new worker
        cluster.on('exit', (worker, code, signal) => {
            if (code !== 0 && !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 + 1);
                    });
    
                    worker.disconnect();
                });
            }
            // Calling restartWorker recursively
            restartWorker(0);
        });
    } else {
        require('./server');
    }
  • 相关阅读:
    int **指针问题
    用 _tcscmp 替代 strcmp 或 wcscmp
    C++经典面试算法题
    目标世界上最小的Linux系统—ttylinux体验
    im-switch -s ibus错误:Error: no configuration file "ibus" exists.
    Ruby环境搭建
    emulator: ERROR: x86 emulation currently requires hardware acceleration!Please ensure Intel HAXM is properly installed and usable.CPU acceleration status: HAX kernel module is not installed!
    POJ 1007 DNA Sorting
    POJ 1002 487-3279
    Ubuntu Android adb调试无法识别设备 -- List of devices attached ???????????? no permissions
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10502056.html
Copyright © 2020-2023  润新知