Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
904309564a | |||
d8c03fd9ca | |||
7e5c0fe678 | |||
93b44ba888 | |||
30249ad254 | |||
07be849d2f | |||
d31f85c6cb | |||
fb2b72a97f | |||
18dfa70d16 | |||
1cb892c357 | |||
275e242ad8 | |||
b295b55a92 | |||
8fbd9c0b51 | |||
1850f7d132 | |||
e0dffad3f2 | |||
241880f2b0 | |||
9d33a5df53 | |||
57ddf4c45d | |||
1329513543 | |||
e1acfd03e6 | |||
d73c877815 | |||
347036cf4e | |||
661e782367 | |||
6da41845f1 | |||
6b95c7e2ca | |||
9d08755705 | |||
00969c1d7f | |||
a16a66f1f6 |
112
.gitlab-ci.yml
112
.gitlab-ci.yml
@ -1,49 +1,125 @@
|
|||||||
image: hosttoday/ht-docker-node:npmts
|
# gitzone standard
|
||||||
|
image: hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
|
cache:
|
||||||
|
paths:
|
||||||
|
- .npmci_cache/
|
||||||
|
key: "$CI_BUILD_STAGE"
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- page
|
- metadata
|
||||||
|
|
||||||
testLEGACY:
|
# ====================
|
||||||
stage: test
|
# security stage
|
||||||
|
# ====================
|
||||||
|
mirror:
|
||||||
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci test legacy
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
snyk:
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install -g snyk
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
|
- npmci command snyk test
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# test stage
|
||||||
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test lts
|
- npmci npm prepare
|
||||||
|
- npmci node install lts
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
testSTABLE:
|
testSTABLE:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test stable
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
environment: npmjs-com_registry
|
|
||||||
script:
|
script:
|
||||||
- npmci publish
|
- npmci node install stable
|
||||||
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# metadata stage
|
||||||
|
# ====================
|
||||||
|
codequality:
|
||||||
|
stage: metadata
|
||||||
|
image: docker:stable
|
||||||
|
allow_failure: true
|
||||||
|
services:
|
||||||
|
- docker:stable-dind
|
||||||
|
script:
|
||||||
|
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||||
|
- docker run
|
||||||
|
--env SOURCE_CODE="$PWD"
|
||||||
|
--volume "$PWD":/code
|
||||||
|
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||||
|
artifacts:
|
||||||
|
paths: [codeclimate.json]
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npmci trigger
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmpage
|
image: hosttoday/ht-docker-node:npmci
|
||||||
stage: page
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npmpage --host gitlab
|
- npmci command npm install -g typedoc typescript
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
55
README.md
55
README.md
@ -1,43 +1,52 @@
|
|||||||
# smartipc
|
# @pushrocks/smartspawn
|
||||||
smart subprocess handling
|
smart subprocess handling
|
||||||
|
|
||||||
## Availabililty
|
## Availabililty and Links
|
||||||
[](https://www.npmjs.com/package/smartipc)
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartspawn)
|
||||||
[](https://gitlab.com/pushrocks/smartipc)
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartspawn)
|
||||||
[](https://github.com/pushrocks/smartipc)
|
* [github.com (source mirror)](https://github.com/pushrocks/smartspawn)
|
||||||
[](https://pushrocks.gitlab.io/smartipc/)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartspawn/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/smartipc/commits/master)
|
[](https://gitlab.com/pushrocks/smartspawn/commits/master)
|
||||||
[](https://gitlab.com/pushrocks/smartipc/commits/master)
|
[](https://gitlab.com/pushrocks/smartspawn/commits/master)
|
||||||
[](https://david-dm.org/pushrocks/smartipc)
|
[](https://www.npmjs.com/package/@pushrocks/smartspawn)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartipc/master/dependencies/npm)
|
[](https://snyk.io/test/npm/@pushrocks/smartspawn)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartipc)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://prettier.io/)
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
|
### Understand the Purpose
|
||||||
|
|
||||||
## The purpose
|
|
||||||
smartipc makes it easy to spawn tasks into subprocesses without loosing control over what those processes do.
|
smartipc makes it easy to spawn tasks into subprocesses without loosing control over what those processes do.
|
||||||
You can transparently call functions and expect returned data using promises.
|
You can transparently call functions and expect returned data using promises.
|
||||||
|
|
||||||
## Usage
|
### How To
|
||||||
|
|
||||||
**Master.ts:**
|
**Master.ts:**
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import * as smartipc from 'smartipc'
|
import * as smartspawn from 'smartspawn';
|
||||||
smartipc.setBasePathArg(__dirname) // if you want to avoid typings out full paths every time
|
smartspawn.setBasePathArg(__dirname); // if you want to avoid typings out full paths every time
|
||||||
|
|
||||||
let myThread = new smartipc.Thread('worker')
|
let myThread = new smartspawn.Thread('worker');
|
||||||
myThread.send('someMessageOrObject').then(messageResponse => {
|
myThread.send('someMessageOrObject').then(messageResponse => {
|
||||||
console.log(messageResponse)
|
console.log(messageResponse);
|
||||||
})
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
**worker.ts**
|
**worker.ts**
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
```
|
```
|
||||||
[](https://push.rocks)
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
||||||
|
2
assets/typescriptwrap.js
Normal file
2
assets/typescriptwrap.js
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
require('@gitzone/tsrun');
|
||||||
|
require('spawn-wrap').runMain()
|
3
dist/index.d.ts
vendored
3
dist/index.d.ts
vendored
@ -1,3 +0,0 @@
|
|||||||
export * from './smartipc.classes.thread';
|
|
||||||
export * from './smartipc.classes.threadfunction';
|
|
||||||
export * from './smartipc.classes.pool';
|
|
8
dist/index.js
vendored
8
dist/index.js
vendored
@ -1,8 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
function __export(m) {
|
|
||||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
||||||
}
|
|
||||||
__export(require("./smartipc.classes.thread"));
|
|
||||||
__export(require("./smartipc.classes.threadfunction"));
|
|
||||||
__export(require("./smartipc.classes.pool"));
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBQUEsK0NBQXlDO0FBQ3pDLHVEQUFpRDtBQUNqRCw2Q0FBdUMifQ==
|
|
5
dist/smartipc.classes.pool.d.ts
vendored
5
dist/smartipc.classes.pool.d.ts
vendored
@ -1,5 +0,0 @@
|
|||||||
export declare class Pool {
|
|
||||||
pool: any;
|
|
||||||
constructor();
|
|
||||||
run(workerPathArg: string): any;
|
|
||||||
}
|
|
12
dist/smartipc.classes.pool.js
vendored
12
dist/smartipc.classes.pool.js
vendored
@ -1,12 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
const plugins = require("./smartipc.plugins");
|
|
||||||
class Pool {
|
|
||||||
constructor() {
|
|
||||||
this.pool = new plugins.threads.Pool();
|
|
||||||
}
|
|
||||||
run(workerPathArg) {
|
|
||||||
return this.pool.run(workerPathArg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.Pool = Pool;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpcGMuY2xhc3Nlcy5wb29sLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRpcGMuY2xhc3Nlcy5wb29sLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSw4Q0FBNkM7QUFFN0M7SUFFSTtRQUNJLElBQUksQ0FBQyxJQUFJLEdBQUcsSUFBSSxPQUFPLENBQUMsT0FBTyxDQUFDLElBQUksRUFBRSxDQUFBO0lBQzFDLENBQUM7SUFDRCxHQUFHLENBQUMsYUFBcUI7UUFDckIsTUFBTSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLGFBQWEsQ0FBQyxDQUFBO0lBQ3ZDLENBQUM7Q0FDSjtBQVJELG9CQVFDIn0=
|
|
16
dist/smartipc.classes.thread.d.ts
vendored
16
dist/smartipc.classes.thread.d.ts
vendored
@ -1,16 +0,0 @@
|
|||||||
import { Pool } from './smartipc.classes.pool';
|
|
||||||
export declare let setWorkerBasePath: (basePathArg: string) => void;
|
|
||||||
export declare class Thread {
|
|
||||||
thread: any;
|
|
||||||
workerPath: string;
|
|
||||||
running: boolean;
|
|
||||||
assignedPool: Pool;
|
|
||||||
constructor(filePathArg: string);
|
|
||||||
/**
|
|
||||||
* sends a message to the spawned process
|
|
||||||
*/
|
|
||||||
send<T>(message: any): Promise<T>;
|
|
||||||
kill(): void;
|
|
||||||
assignToPool(poolArg: Pool): void;
|
|
||||||
private checkSpawn();
|
|
||||||
}
|
|
54
dist/smartipc.classes.thread.js
vendored
54
dist/smartipc.classes.thread.js
vendored
@ -1,54 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
const plugins = require("./smartipc.plugins");
|
|
||||||
const q = require("smartq");
|
|
||||||
exports.setWorkerBasePath = (basePathArg) => {
|
|
||||||
plugins.threads.config.set({
|
|
||||||
basepath: {
|
|
||||||
node: basePathArg
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
class Thread {
|
|
||||||
constructor(filePathArg) {
|
|
||||||
this.running = false;
|
|
||||||
this.assignedPool = null;
|
|
||||||
this.workerPath = filePathArg;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* sends a message to the spawned process
|
|
||||||
*/
|
|
||||||
send(message) {
|
|
||||||
let done = q.defer();
|
|
||||||
this.checkSpawn();
|
|
||||||
this.thread.send(message);
|
|
||||||
this.thread.on('message', (message) => {
|
|
||||||
done.resolve(message);
|
|
||||||
});
|
|
||||||
this.thread.on('done', (job, message) => {
|
|
||||||
done.resolve(message);
|
|
||||||
});
|
|
||||||
this.thread.on('error', err => {
|
|
||||||
done.reject(err);
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
kill() {
|
|
||||||
this.thread.kill();
|
|
||||||
this.running = false;
|
|
||||||
}
|
|
||||||
assignToPool(poolArg) {
|
|
||||||
this.assignedPool = poolArg;
|
|
||||||
}
|
|
||||||
checkSpawn() {
|
|
||||||
if (!this.running && !this.assignedPool) {
|
|
||||||
this.running = true;
|
|
||||||
this.thread = plugins.threads.spawn(this.workerPath);
|
|
||||||
}
|
|
||||||
else if (!this.running && this.assignedPool) {
|
|
||||||
this.running = true;
|
|
||||||
this.thread = this.assignedPool.run(this.workerPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.Thread = Thread;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjo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QUFDO0lBQ0wsQ0FBQztDQUNKO0FBOUNELHdCQThDQyJ9
|
|
13
dist/smartipc.classes.threadfunction.d.ts
vendored
13
dist/smartipc.classes.threadfunction.d.ts
vendored
@ -1,13 +0,0 @@
|
|||||||
import 'typings-global';
|
|
||||||
export interface IThreadFunction {
|
|
||||||
(input: any, done: any): void;
|
|
||||||
}
|
|
||||||
export declare class ThreadFunction {
|
|
||||||
thread: any;
|
|
||||||
constructor(functionArg: IThreadFunction);
|
|
||||||
/**
|
|
||||||
* sends a message to the spawned process
|
|
||||||
*/
|
|
||||||
send<T>(message: any): Promise<T>;
|
|
||||||
kill(): void;
|
|
||||||
}
|
|
26
dist/smartipc.classes.threadfunction.js
vendored
26
dist/smartipc.classes.threadfunction.js
vendored
@ -1,26 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
const plugins = require("./smartipc.plugins");
|
|
||||||
const q = require("smartq");
|
|
||||||
class ThreadFunction {
|
|
||||||
constructor(functionArg) {
|
|
||||||
this.thread = plugins.threads.spawn(functionArg);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* sends a message to the spawned process
|
|
||||||
*/
|
|
||||||
send(message) {
|
|
||||||
let done = q.defer();
|
|
||||||
this.thread.send(message).on('message', (message) => {
|
|
||||||
done.resolve(message);
|
|
||||||
}).on('error', err => {
|
|
||||||
done.reject(err);
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
kill() {
|
|
||||||
this.thread.kill();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.ThreadFunction = ThreadFunction;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpcGMuY2xhc3Nlcy50aHJlYWRmdW5jdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0aXBjLmNsYXNzZXMudGhyZWFkZnVuY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUN2Qiw4Q0FBNkM7QUFDN0MsNEJBQTJCO0FBTTNCO0lBRUksWUFBWSxXQUE0QjtRQUNwQyxJQUFJLENBQUMsTUFBTSxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLFdBQVcsQ0FBQyxDQUFBO0lBQ3BELENBQUM7SUFFRDs7T0FFRztJQUNILElBQUksQ0FBSSxPQUFZO1FBQ2hCLElBQUksSUFBSSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUssQ0FBQTtRQUN2QixJQUFJLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQyxFQUFFLENBQUMsU0FBUyxFQUFFLENBQUMsT0FBVTtZQUMvQyxJQUFJLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxDQUFBO1FBQ3pCLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxPQUFPLEVBQUUsR0FBRztZQUNkLElBQUksQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUE7UUFDcEIsQ0FBQyxDQUFDLENBQUE7UUFDRixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQTtJQUN2QixDQUFDO0lBRUQsSUFBSTtRQUNBLElBQUksQ0FBQyxNQUFNLENBQUMsSUFBSSxFQUFFLENBQUE7SUFDdEIsQ0FBQztDQUNKO0FBdEJELHdDQXNCQyJ9
|
|
4
dist/smartipc.plugins.d.ts
vendored
4
dist/smartipc.plugins.d.ts
vendored
@ -1,4 +0,0 @@
|
|||||||
import 'typings-global';
|
|
||||||
declare let threads: any;
|
|
||||||
import * as smartq from 'smartq';
|
|
||||||
export { smartq, threads };
|
|
7
dist/smartipc.plugins.js
vendored
7
dist/smartipc.plugins.js
vendored
@ -1,7 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
let threads = require('threads');
|
|
||||||
exports.threads = threads;
|
|
||||||
const smartq = require("smartq");
|
|
||||||
exports.smartq = smartq;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpcGMucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0aXBjLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUN2QixJQUFJLE9BQU8sR0FBRyxPQUFPLENBQUMsU0FBUyxDQUFDLENBQUE7QUFLNUIsMEJBQU87QUFKWCxpQ0FBZ0M7QUFHNUIsd0JBQU0ifQ==
|
|
@ -1,7 +1,21 @@
|
|||||||
{
|
{
|
||||||
"npmts": {
|
"npmts": {
|
||||||
"testTs": {
|
"testTs": {
|
||||||
"./test/*.ts": "./test/"
|
"./test/*.ts": "./test/"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"npmci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartspawn",
|
||||||
|
"shortDescription": "smart subprocess handling",
|
||||||
|
"npmPackagename": "@pushrocks/smartspawn",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
1744
package-lock.json
generated
Normal file
1744
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@ -1,11 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "smartipc",
|
"name": "@pushrocks/smartspawn",
|
||||||
"version": "1.0.2",
|
"version": "2.0.3",
|
||||||
|
"private": false,
|
||||||
"description": "smart subprocess handling",
|
"description": "smart subprocess handling",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(npmts)"
|
"test": "(tstest test/)",
|
||||||
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -18,16 +20,15 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartipc#README",
|
"homepage": "https://gitlab.com/pushrocks/smartipc#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/lodash": "^4.14.50",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
"beautylog": "^6.0.0",
|
"spawn-wrap": "^1.4.2",
|
||||||
"lodash": "^4.17.4",
|
"threads": "^0.8.1"
|
||||||
"smartq": "^1.1.0",
|
|
||||||
"threads": "^0.7.2",
|
|
||||||
"typings-global": "^1.0.14"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/should": "^8.1.30",
|
"@gitzone/tsbuild": "^2.1.8",
|
||||||
"should": "^11.1.2",
|
"@gitzone/tsrun": "^1.1.9",
|
||||||
"typings-test": "^1.0.3"
|
"@gitzone/tstest": "^1.0.12",
|
||||||
|
"@pushrocks/tapbundle": "^3.0.1",
|
||||||
|
"@types/node": "^10.5.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
test/child.d.ts
vendored
2
test/child.d.ts
vendored
@ -1,2 +0,0 @@
|
|||||||
import 'typings-global';
|
|
||||||
import 'smartq';
|
|
@ -1,7 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
require("smartq");
|
|
||||||
module.exports = (input, done) => {
|
|
||||||
done(input);
|
|
||||||
};
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hpbGQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJjaGlsZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLGtCQUFlO0FBQ2YsTUFBTSxDQUFDLE9BQU8sR0FBRyxDQUFDLEtBQUssRUFBRSxJQUFJO0lBQ3pCLElBQUksQ0FBQyxLQUFLLENBQUMsQ0FBQTtBQUNmLENBQUMsQ0FBQSJ9
|
|
@ -1,5 +0,0 @@
|
|||||||
import 'typings-global'
|
|
||||||
import 'smartq'
|
|
||||||
module.exports = (input, done) => {
|
|
||||||
done(input)
|
|
||||||
}
|
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
|||||||
import 'typings-test';
|
|
43
test/test.js
43
test/test.js
@ -1,43 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-test");
|
|
||||||
const smartipc = require("../dist/index");
|
|
||||||
let testThreadFunction;
|
|
||||||
let testThread;
|
|
||||||
let testPool;
|
|
||||||
describe('smartipc', function () {
|
|
||||||
it('should create an instance of ThreadFunction', function () {
|
|
||||||
testThreadFunction = new smartipc.ThreadFunction((input, done) => {
|
|
||||||
let url = require('url');
|
|
||||||
done(url.parse(input));
|
|
||||||
});
|
|
||||||
testThreadFunction.send('https://google.com').then(message => {
|
|
||||||
console.log(message);
|
|
||||||
testThreadFunction.kill();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it('should create an instance of Thread', function () {
|
|
||||||
smartipc.setWorkerBasePath(__dirname);
|
|
||||||
testThread = new smartipc.Thread('child.js');
|
|
||||||
testThread.send('https://google.com').then(message => {
|
|
||||||
console.log(message);
|
|
||||||
testThread.kill();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it('should not spawn when nothing is sent', function () {
|
|
||||||
smartipc.setWorkerBasePath(__dirname);
|
|
||||||
let testThread = new smartipc.Thread('child.js');
|
|
||||||
});
|
|
||||||
it('should run in a Pool', function () {
|
|
||||||
let testPool = new smartipc.Pool();
|
|
||||||
let testThread = new smartipc.Thread('child.js');
|
|
||||||
testThread.assignToPool(testPool);
|
|
||||||
testThread.send('what').then(message => {
|
|
||||||
console.log(message);
|
|
||||||
testThread.send('another').then(message => {
|
|
||||||
console.log(message);
|
|
||||||
testThread.assignedPool.pool.killAll();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQiwwQ0FBeUM7QUFFekMsSUFBSSxrQkFBMkMsQ0FBQTtBQUMvQyxJQUFJLFVBQTJCLENBQUE7QUFDL0IsSUFBSSxRQUF1QixDQUFBO0FBRTNCLFFBQVEsQ0FBQyxVQUFVLEVBQUM7SUFDaEIsRUFBRSxDQUFDLDZDQUE2QyxFQUFDO1FBQzdDLGtCQUFrQixHQUFHLElBQUksUUFBUSxDQUFDLGNBQWMsQ0FBQyxDQUFDLEtBQUssRUFBRSxJQUFJO1lBQ3pELElBQUksR0FBRyxHQUFHLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQTtZQUN4QixJQUFJLENBQUMsR0FBRyxDQUFDLEtBQUssQ0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFBO1FBQzFCLENBQUMsQ0FBQyxDQUFBO1FBQ0Ysa0JBQWtCLENBQUMsSUFBSSxDQUFDLG9CQUFvQixDQUFDLENBQUMsSUFBSSxDQUFDLE9BQU87WUFDdEQsT0FBTyxDQUFDLEdBQUcsQ0FBQyxPQUFPLENBQUMsQ0FBQTtZQUNwQixrQkFBa0IsQ0FBQyxJQUFJLEVBQUUsQ0FBQTtRQUM3QixDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFBO0lBQ0YsRUFBRSxDQUFDLHFDQUFxQyxFQUFDO1F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yxHQUFHLENBQUMsT0FBTyxDQUFDLENBQUE7Z0JBQ3BCLFVBQVUsQ0FBQyxZQUFZLENBQUMsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1lBQzFDLENBQUMsQ0FBQyxDQUFBO1FBQ04sQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
|
|
97
test/test.ts
97
test/test.ts
@ -1,46 +1,61 @@
|
|||||||
import 'typings-test'
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
import * as smartipc from '../dist/index'
|
import * as smartspawn from '../ts/index';
|
||||||
|
|
||||||
let testThreadFunction: smartipc.ThreadFunction
|
let testThreadFunction: smartspawn.ThreadFunction;
|
||||||
let testThread: smartipc.Thread
|
let testThread: smartspawn.Thread;
|
||||||
let testPool: smartipc.Pool
|
let testPool: smartspawn.Pool;
|
||||||
|
|
||||||
describe('smartipc',function(){
|
/**
|
||||||
it('should create an instance of ThreadFunction',function(){
|
* create a normal ThreadFunction
|
||||||
testThreadFunction = new smartipc.ThreadFunction((input, done) => {
|
*/
|
||||||
let url = require('url')
|
tap.test('should create an instance of ThreadFunction', async () => {
|
||||||
done(url.parse(input))
|
testThreadFunction = new smartspawn.ThreadFunction((input, done) => {
|
||||||
})
|
let url = require('url');
|
||||||
testThreadFunction.send('https://google.com').then(message => {
|
done(url.parse(input));
|
||||||
console.log(message)
|
});
|
||||||
testThreadFunction.kill()
|
const message = await testThreadFunction.send('https://google.com');
|
||||||
})
|
console.log(message);
|
||||||
})
|
testThreadFunction.kill();
|
||||||
it('should create an instance of Thread',function(){
|
});
|
||||||
smartipc.setWorkerBasePath(__dirname)
|
|
||||||
testThread = new smartipc.Thread('child.js')
|
|
||||||
testThread.send('https://google.com').then(message => {
|
|
||||||
console.log(message)
|
|
||||||
testThread.kill()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should not spawn when nothing is sent',function(){
|
tap.test('should create an instance of Thread', async () => {
|
||||||
smartipc.setWorkerBasePath(__dirname)
|
smartspawn.setWorkerBasePath(__dirname);
|
||||||
let testThread = new smartipc.Thread('child.js')
|
testThread = new smartspawn.Thread('../testassets/child.ts');
|
||||||
})
|
testThread.enableTypeScript();
|
||||||
|
const message = await testThread.send('https://google.com');
|
||||||
|
console.log(message);
|
||||||
|
testThread.kill();
|
||||||
|
});
|
||||||
|
|
||||||
it('should run in a Pool', function() {
|
tap.test('should not spawn when nothing is sent', async () => {
|
||||||
let testPool = new smartipc.Pool()
|
smartspawn.setWorkerBasePath(__dirname);
|
||||||
let testThread = new smartipc.Thread('child.js')
|
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
||||||
testThread.assignToPool(testPool)
|
});
|
||||||
testThread.send('what').then(message => {
|
|
||||||
console.log(message)
|
tap.test('should run in a Pool', async () => {
|
||||||
testThread.send('another').then(message => {
|
let testPool = new smartspawn.Pool();
|
||||||
console.log(message)
|
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
||||||
testThread.assignedPool.pool.killAll()
|
testThread.assignToPool(testPool);
|
||||||
})
|
|
||||||
})
|
// first run
|
||||||
})
|
let message = await testThread.send('what');
|
||||||
})
|
expect(message).to.equal('what');
|
||||||
|
console.log(message);
|
||||||
|
|
||||||
|
// second run
|
||||||
|
message = await testThread.send('another');
|
||||||
|
expect(message).to.equal('another');
|
||||||
|
console.log(message);
|
||||||
|
|
||||||
|
// kill all
|
||||||
|
testThread.assignedPool.pool.killAll();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should once', async () => {
|
||||||
|
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
||||||
|
const message = await testThread.sendOnce('what');
|
||||||
|
expect(message).to.equal('what');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
||||||
|
3
testassets/child.ts
Normal file
3
testassets/child.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (input, done) => {
|
||||||
|
done(input);
|
||||||
|
};
|
@ -1,3 +1,5 @@
|
|||||||
export * from './smartipc.classes.thread'
|
export * from './smartspawn.classes.thread';
|
||||||
export * from './smartipc.classes.threadfunction'
|
export * from './smartspawn.classes.threadfunction';
|
||||||
export * from './smartipc.classes.pool'
|
export * from './smartspawn.classes.threadsimple';
|
||||||
|
export * from './smartspawn.classes.pool';
|
||||||
|
export * from './smartspawn.wrap';
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
import * as plugins from './smartipc.plugins'
|
|
||||||
|
|
||||||
export class Pool {
|
|
||||||
pool
|
|
||||||
constructor() {
|
|
||||||
this.pool = new plugins.threads.Pool()
|
|
||||||
}
|
|
||||||
run(workerPathArg: string) {
|
|
||||||
return this.pool.run(workerPathArg)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
import * as plugins from './smartipc.plugins'
|
|
||||||
import * as q from 'smartq'
|
|
||||||
|
|
||||||
import { Pool } from './smartipc.classes.pool'
|
|
||||||
|
|
||||||
export let setWorkerBasePath = (basePathArg: string) => {
|
|
||||||
plugins.threads.config.set({
|
|
||||||
basepath: {
|
|
||||||
node: basePathArg
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Thread {
|
|
||||||
thread
|
|
||||||
workerPath: string
|
|
||||||
running: boolean = false
|
|
||||||
assignedPool: Pool = null
|
|
||||||
constructor(filePathArg: string) {
|
|
||||||
this.workerPath = filePathArg
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends a message to the spawned process
|
|
||||||
*/
|
|
||||||
send<T>(message: any): Promise<T> {
|
|
||||||
let done = q.defer<T>()
|
|
||||||
this.checkSpawn()
|
|
||||||
this.thread.send(message)
|
|
||||||
this.thread.on('message', (message: T) => {
|
|
||||||
done.resolve(message)
|
|
||||||
})
|
|
||||||
this.thread.on('done', (job, message: T) => {
|
|
||||||
done.resolve(message)
|
|
||||||
})
|
|
||||||
this.thread.on('error', err => {
|
|
||||||
done.reject(err)
|
|
||||||
})
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
kill() {
|
|
||||||
this.thread.kill()
|
|
||||||
this.running = false
|
|
||||||
}
|
|
||||||
|
|
||||||
assignToPool(poolArg: Pool) {
|
|
||||||
this.assignedPool = poolArg
|
|
||||||
}
|
|
||||||
|
|
||||||
private checkSpawn() {
|
|
||||||
if (!this.running && !this.assignedPool) {
|
|
||||||
this.running = true
|
|
||||||
this.thread = plugins.threads.spawn(this.workerPath)
|
|
||||||
} else if (!this.running && this.assignedPool) {
|
|
||||||
this.running = true
|
|
||||||
this.thread = this.assignedPool.run(this.workerPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
import 'typings-global'
|
|
||||||
import * as plugins from './smartipc.plugins'
|
|
||||||
import * as q from 'smartq'
|
|
||||||
|
|
||||||
export interface IThreadFunction {
|
|
||||||
(input, done): void
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ThreadFunction {
|
|
||||||
thread
|
|
||||||
constructor(functionArg: IThreadFunction) {
|
|
||||||
this.thread = plugins.threads.spawn(functionArg)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends a message to the spawned process
|
|
||||||
*/
|
|
||||||
send<T>(message: any): Promise<T> {
|
|
||||||
let done = q.defer<T>()
|
|
||||||
this.thread.send(message).on('message', (message: T) => {
|
|
||||||
done.resolve(message)
|
|
||||||
}).on('error', err => {
|
|
||||||
done.reject(err)
|
|
||||||
})
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
kill() {
|
|
||||||
this.thread.kill()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
import 'typings-global'
|
|
||||||
let threads = require('threads')
|
|
||||||
import * as smartq from 'smartq'
|
|
||||||
|
|
||||||
export {
|
|
||||||
smartq,
|
|
||||||
threads
|
|
||||||
}
|
|
6
ts/smartspawn.classes.cluster.ts
Normal file
6
ts/smartspawn.classes.cluster.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// ==============
|
||||||
|
// This file handles clustering of nodejs programs
|
||||||
|
// ==============
|
||||||
|
import * as plugins from './smartspawn.plugins';
|
||||||
|
|
||||||
|
export class Cluster {}
|
13
ts/smartspawn.classes.pool.ts
Normal file
13
ts/smartspawn.classes.pool.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import * as plugins from './smartspawn.plugins';
|
||||||
|
|
||||||
|
export class Pool {
|
||||||
|
pool;
|
||||||
|
|
||||||
|
// the constructor for pool
|
||||||
|
constructor() {
|
||||||
|
this.pool = new plugins.threads.Pool();
|
||||||
|
}
|
||||||
|
run(workerPathArg: string) {
|
||||||
|
return this.pool.run(workerPathArg);
|
||||||
|
}
|
||||||
|
}
|
85
ts/smartspawn.classes.thread.ts
Normal file
85
ts/smartspawn.classes.thread.ts
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
import * as plugins from './smartspawn.plugins';
|
||||||
|
import * as paths from './smartspawn.paths';
|
||||||
|
import * as q from 'smartq';
|
||||||
|
|
||||||
|
import { Pool } from './smartspawn.classes.pool';
|
||||||
|
import { startSpawnWrap, endSpawnWrap } from './smartspawn.wrap';
|
||||||
|
|
||||||
|
export let workerBasePath: string = null;
|
||||||
|
|
||||||
|
export const setWorkerBasePath = (basePathArg: string) => {
|
||||||
|
workerBasePath = basePathArg;
|
||||||
|
plugins.threads.config.set({
|
||||||
|
basepath: {
|
||||||
|
node: workerBasePath
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export class Thread {
|
||||||
|
thread;
|
||||||
|
workerPath: string;
|
||||||
|
running: boolean = false;
|
||||||
|
assignedPool: Pool = null;
|
||||||
|
constructor(filePathArg: string) {
|
||||||
|
this.workerPath = filePathArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sends a message to the spawned process
|
||||||
|
* spawns it and keeps running
|
||||||
|
*/
|
||||||
|
send<T>(message: any): Promise<T> {
|
||||||
|
let done = q.defer<T>();
|
||||||
|
this._checkSpawn();
|
||||||
|
this.thread.send(message);
|
||||||
|
this.thread.on('message', (message: T) => {
|
||||||
|
done.resolve(message);
|
||||||
|
});
|
||||||
|
this.thread.on('done', (job, message: T) => {
|
||||||
|
done.resolve(message);
|
||||||
|
});
|
||||||
|
this.thread.on('error', err => {
|
||||||
|
done.reject(err);
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sends a command once and then kills the child process
|
||||||
|
*/
|
||||||
|
sendOnce<T>(message): Promise<T> {
|
||||||
|
let done = q.defer<T>();
|
||||||
|
this.send<T>(message).then(message => {
|
||||||
|
done.resolve(message);
|
||||||
|
this.kill();
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* kills the thread
|
||||||
|
*/
|
||||||
|
kill() {
|
||||||
|
this.thread.kill();
|
||||||
|
this.running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
assignToPool(poolArg: Pool) {
|
||||||
|
this.assignedPool = poolArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
enableTypeScript() {
|
||||||
|
// TODO:
|
||||||
|
}
|
||||||
|
|
||||||
|
private _checkSpawn() {
|
||||||
|
if (!this.running && !this.assignedPool) {
|
||||||
|
this.running = true;
|
||||||
|
this.thread = plugins.threads.spawn(this.workerPath);
|
||||||
|
} else if (!this.running && this.assignedPool) {
|
||||||
|
this.running = true;
|
||||||
|
this.thread = this.assignedPool.run(this.workerPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
ts/smartspawn.classes.threadfunction.ts
Normal file
34
ts/smartspawn.classes.threadfunction.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import 'typings-global';
|
||||||
|
import * as plugins from './smartspawn.plugins';
|
||||||
|
import * as q from 'smartq';
|
||||||
|
|
||||||
|
export interface IThreadFunction {
|
||||||
|
(input, done): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ThreadFunction {
|
||||||
|
thread;
|
||||||
|
constructor(functionArg: IThreadFunction) {
|
||||||
|
this.thread = plugins.threads.spawn(functionArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sends a message to the spawned process
|
||||||
|
*/
|
||||||
|
send<T>(message: any): Promise<T> {
|
||||||
|
let done = q.defer<T>();
|
||||||
|
this.thread
|
||||||
|
.send(message)
|
||||||
|
.on('message', (message: T) => {
|
||||||
|
done.resolve(message);
|
||||||
|
})
|
||||||
|
.on('error', err => {
|
||||||
|
done.reject(err);
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
kill() {
|
||||||
|
this.thread.kill();
|
||||||
|
}
|
||||||
|
}
|
35
ts/smartspawn.classes.threadsimple.ts
Normal file
35
ts/smartspawn.classes.threadsimple.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import * as plugins from './smartspawn.plugins';
|
||||||
|
import * as smartq from 'smartq';
|
||||||
|
import * as childProcess from 'child_process';
|
||||||
|
|
||||||
|
import { workerBasePath } from './smartspawn.classes.thread';
|
||||||
|
|
||||||
|
export class ThreadSimple {
|
||||||
|
workerPath: string;
|
||||||
|
threadChildProcess: childProcess.ChildProcess;
|
||||||
|
forkOptions: childProcess.ForkOptions;
|
||||||
|
argvArgs: string[];
|
||||||
|
constructor(
|
||||||
|
filePathArg: string,
|
||||||
|
argvArgs: string[] = [],
|
||||||
|
forkOptionsArg: childProcess.ForkOptions = {}
|
||||||
|
) {
|
||||||
|
this.workerPath = filePathArg;
|
||||||
|
this.forkOptions = forkOptionsArg;
|
||||||
|
this.argvArgs = argvArgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
run() {
|
||||||
|
let done = smartq.defer<childProcess.ChildProcess>();
|
||||||
|
let forkPath = (() => {
|
||||||
|
if (workerBasePath) {
|
||||||
|
return plugins.path.join(workerBasePath, this.workerPath);
|
||||||
|
} else {
|
||||||
|
return this.workerPath;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
this.threadChildProcess = childProcess.fork(forkPath, this.argvArgs, this.forkOptions);
|
||||||
|
done.resolve(this.threadChildProcess);
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
}
|
4
ts/smartspawn.paths.ts
Normal file
4
ts/smartspawn.paths.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import * as plugins from './smartspawn.plugins';
|
||||||
|
|
||||||
|
export const packageBase = plugins.path.join(__dirname, '../');
|
||||||
|
export const typescriptwrapJs = plugins.path.join(packageBase, 'assets/typescriptwrap.js');
|
5
ts/smartspawn.plugins.ts
Normal file
5
ts/smartspawn.plugins.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import * as path from 'path';
|
||||||
|
let threads = require('threads');
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
|
export { path, smartpromise, threads };
|
18
ts/smartspawn.wrap.ts
Normal file
18
ts/smartspawn.wrap.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import * as spawnWrap from 'spawn-wrap';
|
||||||
|
|
||||||
|
let unwrap: any = null;
|
||||||
|
|
||||||
|
export const startSpawnWrap = (filePath: string, cliArgs: string[] = [], envArgs: any = {}) => {
|
||||||
|
let spawnArray = [filePath];
|
||||||
|
for (let cliArg of cliArgs) {
|
||||||
|
spawnArray.push(cliArg);
|
||||||
|
}
|
||||||
|
unwrap = spawnWrap(spawnArray, envArgs);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const endSpawnWrap = () => {
|
||||||
|
if (unwrap) {
|
||||||
|
unwrap();
|
||||||
|
unwrap = null;
|
||||||
|
}
|
||||||
|
};
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": "tslint-config-standard"
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
|
"rules": {
|
||||||
|
"semicolon": [true, "always"],
|
||||||
|
"no-console": false,
|
||||||
|
"ordered-imports": false,
|
||||||
|
"object-literal-sort-keys": false,
|
||||||
|
"member-ordering": {
|
||||||
|
"options":{
|
||||||
|
"order": [
|
||||||
|
"static-method"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultSeverity": "warning"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user