Socket
Book a DemoInstallSign in
Socket

@clusic/cluster

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clusic/cluster

The cluster model of clusic

latest
Source
npmnpm
Version
1.0.6
Version published
Maintainers
1
Created
Source

@clusic/cluster

集群启动模块

Install

npm i @clusic/cluster

Usage

const path = require('path');
const master = require('@clusic/cluster');
const app = new master({
  cwd: __dirname,
  env: 'dev',
  agents: ['agent', 'test', 'abc'],
  framework: path.resolve(__dirname, 'framework'),
  // max: 2
});
app.createServer().catch(e => app.kill());

Arguments:

/**
 * IPC Master process
 *
 * OPTIONS:
 * @param socket {boolean} <default: false> 是否开启socket模式
 * @param port {number} <default: 8080> 服务启动端口
 * @param cwd {string} <default: process.cwd()> 服务运行基址
 * @param env {string} <default: 'development'> 环境变量
 * @param debug {boolean|string} <default: false> 是否开启调试以及调试模式 eg: 'inspect-brk'
 * @param framework {string} <default: __dirname> 服务启动框架
 * @param max {Number} <default: os.cpus().length> 最大worker进程个数
 * @type {module.MasterProcess}
 */

Framework

框架原型

module.exports = class Application {
  constructor(app, data) {
    this.data = data;
    this.app = app;
  }
  // 进程创建周期
  async processCreate() {
    await new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('worker processWillCreate', Date.now());
        // if (process.pid % 2 === 0) {
        //   return reject(new Error('test error'))
        // }
        resolve();
      }, 2000);
    });
  }
  // 消息接收函数
  processMessage(msg, socket) {
    if (msg.action === 'cluster:ready') {
      console.log('all ready in worker ' + process.pid);
    }
    // console.log('message', msg);
  }
  //进程销毁周期
  async processDestroy() {
    await new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('worker processDestroy', Date.now());
        resolve();
      }, 2000);
    });
  }
};

Keywords

cluster

FAQs

Package last updated on 10 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts