加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
node_media_cluster.js 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
illuspas 提交于 2018-06-20 16:59 . u
//
// Created by Mingliang Chen on 18/6/20.
// illuspas[a]gmail.com
// Copyright (c) 2018 Nodemedia. All rights reserved.
//
const NodeMediaServer = require('./node_media_server');
const cluster = require('cluster');
const context = require('./node_core_ctx');
const Logger = require('./node_core_logger');
class NodeMediaCluster {
constructor(config) {
this.config = config;
}
run() {
if (cluster.isMaster) {
Logger.log(`Master ${process.pid} is running`);
const messageHandler = (msg) => {
for (let id in cluster.workers) {
cluster.workers[id].send(msg);
}
}
const newWorker = () => {
let worker = cluster.fork();
worker.on('message', messageHandler);
}
for (let i = 0; i < this.config.cluster.num; i++) {
newWorker();
}
cluster.on('exit', (worker, code, signal) => {
Logger.log(`worker ${worker.process.pid} died`);
newWorker();
});
} else {
this.nms = new NodeMediaServer(this.config);
this.nms.run();
Logger.log(`worker ${process.pid} started`);
}
}
on(eventName, listener) {
if (cluster.isWorker) {
context.nodeEvent.on(eventName, listener);
}
}
stop() {
if (cluster.isWorker) {
this.nms.stop();
}
}
}
module.exports = NodeMediaCluster
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化