Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node的cluster子进程退出问题 #1

Open
Foveluy opened this issue Feb 12, 2018 · 0 comments
Open

node的cluster子进程退出问题 #1

Foveluy opened this issue Feb 12, 2018 · 0 comments

Comments

@Foveluy
Copy link
Member

Foveluy commented Feb 12, 2018

cluster中

在nodejs中,cluster子进程因为某种原因disconnect之后,会处理完所有的数据之后,自动被master重启。

import { Burn } from './core';
import * as cluster from 'cluster';
import * as os from 'os';
import { EventEmitter } from 'events';


export default class BurnCluster extends EventEmitter {
    constructor() {
        super();
    }

    forkWorkers() {
        const numCPUs = os.cpus().length;

        if (cluster.isMaster) {
            // Fork workers.

            console.log(`master进程#${process.pid}`)
            for (let i = 0; i < numCPUs; i++) {
                cluster.fork();
            }
            cluster.on('fork', function (worker) {
                console.log('worker ' + worker.process.pid + ' start');
                worker.on('message', (msg) => {
                    console.log(msg + 'from ' + worker.process.pid)
                })
            })

            cluster.on('online', function (worker) {
                worker.disconnect();
            })

            cluster.on('exit', function (worker, code, signal) {
                console.log('worker ' + worker.process.pid + ' died');
                // cluster.fork();
            });

            cluster.on('disconnect', function (worker) {
                if (worker.isDead) {
                    console.log('工作进程 #' + worker.id + ' 已经死亡');
                }
                console.log('工作进程 #' + worker.id + ' 断开了连接');
            })

            cluster.on('listening', (worker, address) => {

            });


        } else {
            const app = new Burn;

            app.run();

            // process.on('message', function (msg) {
            //     console.log('3:', msg);
            // });

            // (<any>process).send('你好');
        }
    }
    startCluster() {
        this.forkWorkers();
    }
}

worker进程的master被杀死之后,worker都会立即自杀。

childprocess中

在cluster master中,childprocess收到disconnect后,不会自动被集群重启。

需要认为的手动退出和重启。

在master死后,childprocess也不会自动杀死自己,而是变成孤儿进程。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant