1.1.26 • Published 5 years ago

@nelts/process v1.1.26

Weekly downloads
46
License
MIT
Repository
-
Last release
5 years ago

@nelts/process

nelts安全进程辅助架构。

Usage

在项目中安装工具

npm i @nelts/process

开发模式下启动

ts-node node_modules/@nelts/cli/dist/runtime.ts --script={start file name}

生产环境下启动,建议使用PM2启动。

pm2 start node_modules/@nelts/cli/dist/runtime.js --name={name} -- --script={start file name}

Lifecycle

每个启动的进程对应一个script文件地址,它具有以下的生命周期:

  • componentWillCreate 进程启动前执行函数 此时 messager 不可用
  • componentDidCreated 进程启动完毕执行函数 此时 messager 可用
  • componentWillDestroy 进程结束前执行函数 此时 messager 可用
  • componentDidDestroyed 进程结束后执行函数 此时 messager 不可用
  • componentCatchError 统一错误捕获 参数为 error: Error
  • componentReceiveMessage 统一消息接受函数 (message, socket) => {}

script文件内容必须是一个返回的class对象

eg:

import { Component, Processer } from '@nelts/process';

export default class DemoComponent extends Component {
  constructor(processer: Processer, args: { [name:string]: any }) {
    super(processer, args);
  }
  async componentWillCreate() {}
  async componentDidCreated() {}
  async componentWillDestroy() {}
  async componentDidDestroyed() {}
  componentCatchError(err: Error) {}
  componentReceiveMessage(message:any, socket?:any) {}
}

它具有以下隐式方法

  • this.send(message:any, socket?:any) 发送消息
  • this.kill(pid?: number) 杀掉进程
  • this.createAgent(name: string, file: string, _args?: any) 创建子agent进程
  • this.createWorkerForker(file: string, _args?: any) 创建子worker进程

this.send(message:any, socket?:any)

  • message {object} 消息体 { to, event, data }
  • socket socket对象

发送消息

this.send({
  to: 'test',
  event: 'abc',
  data: {
    a: 1
  }
});

this.kill(pid?: number)

杀掉自身进程或者指定进程,或者全局结束所有进程

  • pid 进程ID。如果省略就是杀掉所有相关进程。
this.kill(process.pid);
this.kill(0); // 0 表示杀死全部

this.createAgent(name: string, file: string, _args?: any)

创建一个agent进程

  • name 进程名
  • file 进程执行文件地址
  • _args 额外参数 为json数据
import { Node } from '@nelts/process';
const agent: Node = await this.createAgent('test', 'test/file.js');
// agent 就是对应的Node进程节点

this.createWorkerForker(file: string, _args?: any)

创建一个worker进程forker。

  • file 进程执行文件地址
  • _args 额外参数 为json数据
const fork = this.createWorkerForker('test/worker.js');
await fork();
await fork();
await fork();
await fork();

License

MIT

Copyright (c) 2018-present, yunjie (Evio) shen

1.1.26

5 years ago

1.1.25

5 years ago

1.1.24

5 years ago

1.1.23

5 years ago

1.1.22

5 years ago

1.1.21

5 years ago

1.1.20

5 years ago

1.1.19

5 years ago

1.1.18

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago