From d309a82a595315c2a7de250beb5cb8c639e12d12 Mon Sep 17 00:00:00 2001 From: zyao89 Date: Mon, 12 Aug 2019 16:25:44 +0800 Subject: [PATCH] :pencil: add README --- README.MD | 93 +++++++++++++++++++++++++++++++++++++++++++ lib/Client.js | 5 +++ package.json | 51 ++++++++++++++++-------- test/index.js | 11 ++--- test/sub-processes.js | 7 ++-- 5 files changed, 140 insertions(+), 27 deletions(-) create mode 100644 README.MD diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..004fdc5 --- /dev/null +++ b/README.MD @@ -0,0 +1,93 @@ +# @2o3t/process-manager + +Process manager for Nodejs. + +Ideas from https://github.com/Microsoft/vscode. + +## Installation + +```sh +npm install @2o3t/process-manager +``` + +or + +```sh +yarn add @2o3t/process-manager +``` + +## Usage + +**Client** (main process): + +```js +const PM = require('@2o3t/process-manager'); +const client = new PM.Client(__dirname + '/sub-processes.js'); +const channel = client.getChannel('channelName'); +channel.call('info', { + // something +}).then(data => { + console.log('data', data); +}); +channel.listen('ccc')(aa => { + console.log('listen..', aa); +}); +``` + + +**Server** (sub process): + +```js +const PM = require('@2o3t/process-manager'); +const server = new PM.Server(); +const channel = new PM.ServerChannel({ + info(arg) { + console.log('arg: ', arg); + return Promise.resolve(server.info); + }, + ccc(emit) { + emit('100w'); + emit('101w'); + // server.dispose(); + emit('102w'); + emit('103w'); + emit('104w'); + }, +}); +console.log('server ok'); +server.registerChannel('channelName', channel); +``` + +## Options + +**`Client`** options in paramters + +**modulePath**: file path of sub process + +**options**: Object. + +| key | type | desc | +|-----|-----|-----| +| **serverName** | string | A descriptive name for the server this connection is to. Used in logging. | +| **timeout**? | number | Time in millies before killing the ipc process. The next request after killing will start it again. | +| **args**? | string[] | Arguments to the module to execute. | +| **env**? | any | Environment key-value pairs to be passed to the process that gets spawned for the ipc. | +| **debug**? | number | Allows to assign a debug port for debugging the application executed. | +| **debugBrk**? | number | Allows to assign a debug port for debugging the application and breaking it on the first line. | +| **freshExecArgv**? | boolean | See https://github.com/Microsoft/vscode/issues/27665
* Allows to pass in fresh execArgv to the forked process such that it doesn't inherit them from `process.execArgv`.
* e.g. Launching the extension host process with `--inspect-brk=xxx` and then forking a process from the extension host results in the forked process inheriting `--inspect-brk=xxx`. | +| **useQueue**? | boolean | Enables our createQueuedSender helper for this Client. Uses a queue when the internal Node.js queue is full of messages - see notes on that method. | + + + +**`Server`** options in paramters + +**options**: Object. + +| key | type | desc | +|-----|-----|-----| +| **timeoutDelay**? | number | They will timeout after `timeoutDelay`. | + + +## License + +MIT diff --git a/lib/Client.js b/lib/Client.js index 5d3ed03..574dc19 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -53,6 +53,10 @@ const ChannelClient = require('./channel/ChannelClient'); // useQueue?: boolean; // } +const defaultOptions = { + serverName: 'UNKNOW', +}; + class Client { /** *Creates an instance of Client. @@ -67,6 +71,7 @@ class Client { this.channels = new Map(); this.child = null; this._client = null; + this.options = Object.assign({}, defaultOptions, options, { timeout }); } /** diff --git a/package.json b/package.json index ed3aab0..b4ddfb6 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,37 @@ { - "name": "@2o3t/process-manager", - "version": "0.0.1", - "description": "", - "main": "index.js", - "directories": { - "lib": "lib" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "zyao89 ", - "license": "ISC", - "devDependencies": { - "eslint": "^5.16.0", - "eslint-config-2o3t": "^1.1.15" - } + "name": "@2o3t/process-manager", + "version": "0.0.2", + "description": "Process manager for Nodejs.", + "main": "index.js", + "directories": { + "lib": "lib" + }, + "files": [ + "lib", + "index.js" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "process", + "manager", + "pm", + "process-manager", + "ipc" + ], + "author": "zyao89 ", + "license": "MIT", + "homepage": "https://github.com/2o3t/process-manager", + "repository": "github:2o3t/process-manager", + "bugs": { + "url": "https://github.com/2o3t/process-manager/issues" + }, + "engines": { + "node": ">=6" + }, + "devDependencies": { + "eslint": "^5.16.0", + "eslint-config-2o3t": "^1.1.15" + } } diff --git a/test/index.js b/test/index.js index b80207d..0023f56 100644 --- a/test/index.js +++ b/test/index.js @@ -2,15 +2,12 @@ const PM = require('../'); const client = new PM.Client(__dirname + '/sub-processes.js'); -const channel = client.getChannel('abc'); -channel.call('ccd', 789).then(data => { +const channel = client.getChannel('channelName'); +channel.call('info', { + // something +}).then(data => { console.log('data', data); }); -setInterval(() => { - channel.call('ccd', 'info').then(data => { - console.log('info: ', data); - }); -}, 2000); channel.listen('ccc')(aa => { console.log('listen..', aa); }); diff --git a/test/sub-processes.js b/test/sub-processes.js index 9c8d2ba..a49c78f 100644 --- a/test/sub-processes.js +++ b/test/sub-processes.js @@ -14,10 +14,9 @@ const PM = require('../'); const server = new PM.Server(); const channel = new PM.ServerChannel({ - ccd(arg) { + info(arg) { console.log('arg: ', arg); - if (arg === 'info') return Promise.resolve(server.info); - return Promise.resolve(arg); + return Promise.resolve(server.info); }, ccc(emit) { emit('100w'); @@ -29,4 +28,4 @@ const channel = new PM.ServerChannel({ }, }); console.log('server ok'); -server.registerChannel('abc', channel); +server.registerChannel('channelName', channel);