1.0.5 • Published 6 years ago

@reinjs/rein-cluster v1.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

rein-cluster

The cluster module of reinjs

Install

npm i @reinjs/rein-cluster

Options

propertytypedescriptiondefault
cwdstringproject root pathprocess.cwd()
agentsarray<string> or undefinedagents listundefined
maxWorkersnumberworker countsos.cpus().length
timeoutnumbertimeoutInfinity
frameworkstringframework name or dir pathundefined

Usage

const Cluster = require('@reinjs/rein-cluster');

const cluster = new Cluster({
  cwd: __dirname,
  agents: ['a', 'b', 'c', 'e'],
  timeout: 10000,
  framework: '@reinjs/rein'
});

cluster.listen();
// Use Promise like this:
// cluster.listen().then(() => console.log('cluster ok')).catch(e => console.error(e));

Framework

We provide three invoking lifecycle to build framework, also it must return a class.

  • @life message(msg): 消息通知生命周期
  • @life create(): 启动服务生命周期
  • @life destroy(signal): 销毁服务生命周期

It is easily to build framework by three lifecycle.

const Koa = require('koa');
module.exports = class Worker extends Koa {
  constructor(obj) {
    super();
    this.app = obj;
    this.logger = console;
  }

  async create() {
    await new Promise((resolve, reject) => {
      this.use(async ctx => ctx.body = 'Hello world');
      this.listen(8080, err => {
        if (err) return reject(err);
        this.logger.log('server on `http://127.0.0.1:8080`');
        resolve();
      });
    });
  }

  async message(msg) {
    console.log(this.app.pid, 'in message lifecycle')
  }

  async destroy(signal) {
    console.log(this.app.pid, 'in destroy lifecycle', signal)
  }
};

License

It is MIT licensed.

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago