Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
f12335ea62 | |||
f12a92660f | |||
eda415893c | |||
630b7661d9 | |||
702c0164a2 | |||
0220252b28 | |||
fcdcd4b929 | |||
c2f7e98481 |
20
.gitignore
vendored
20
.gitignore
vendored
@ -1,4 +1,20 @@
|
|||||||
node_modules/
|
.nogit/
|
||||||
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
pages/
|
|
||||||
public/
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
@ -1,10 +1,10 @@
|
|||||||
# gitzone ci_default
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
@ -12,45 +12,41 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- npm install -g @shipzone/npmci
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
auditProductionDependencies:
|
||||||
stage: security
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
snyk:
|
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
- npmci command npm install --production --ignore-scripts
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command snyk test
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
|
auditDevDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install lts
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
testSTABLE:
|
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
@ -60,7 +56,17 @@ testSTABLE:
|
|||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
testBuild:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@ -70,6 +76,7 @@ release:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
@ -78,20 +85,15 @@ release:
|
|||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
only:
|
||||||
- docker:stable-dind
|
- tags
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g typescript
|
||||||
- docker run
|
- npmci npm prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--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:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
|
|
||||||
@ -102,18 +104,19 @@ trigger:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci node install stable
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"command": "npm test",
|
||||||
|
"name": "Run npm test",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "node-terminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
52
README.md
52
README.md
@ -1,52 +0,0 @@
|
|||||||
# @pushrocks/smartspawn
|
|
||||||
smart subprocess handling
|
|
||||||
|
|
||||||
## Availabililty and Links
|
|
||||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartspawn)
|
|
||||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartspawn)
|
|
||||||
* [github.com (source mirror)](https://github.com/pushrocks/smartspawn)
|
|
||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartspawn/)
|
|
||||||
|
|
||||||
## Status for master
|
|
||||||
[](https://gitlab.com/pushrocks/smartspawn/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartspawn/commits/master)
|
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartspawn)
|
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartspawn)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://prettier.io/)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
|
||||||
|
|
||||||
### Understand the Purpose
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
### How To
|
|
||||||
|
|
||||||
**Master.ts:**
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import * as smartspawn from 'smartspawn';
|
|
||||||
smartspawn.setBasePathArg(__dirname); // if you want to avoid typings out full paths every time
|
|
||||||
|
|
||||||
let myThread = new smartspawn.Thread('worker');
|
|
||||||
myThread.send('someMessageOrObject').then(messageResponse => {
|
|
||||||
console.log(messageResponse);
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
**worker.ts**
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
```
|
|
||||||
|
|
||||||
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)
|
|
@ -9,11 +9,12 @@
|
|||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
},
|
},
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartspawn",
|
"gitrepo": "smartspawn",
|
||||||
"shortDescription": "smart subprocess handling",
|
"description": "smart subprocess handling",
|
||||||
"npmPackagename": "@pushrocks/smartspawn",
|
"npmPackagename": "@pushrocks/smartspawn",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
15552
package-lock.json
generated
15552
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
38
package.json
38
package.json
@ -1,13 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartspawn",
|
"name": "@pushrocks/smartspawn",
|
||||||
"version": "2.0.5",
|
"version": "3.0.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "smart subprocess handling",
|
"description": "smart subprocess handling",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild)"
|
"build": "(tsbuild --web)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -21,16 +23,30 @@
|
|||||||
"homepage": "https://gitlab.com/pushrocks/smartipc#README",
|
"homepage": "https://gitlab.com/pushrocks/smartipc#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"spawn-wrap": "^1.4.2",
|
"spawn-wrap": "^2.0.0",
|
||||||
"threads": "^0.12.1"
|
"threads": "1.7.0",
|
||||||
|
"tiny-worker": "^2.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
"@gitzone/tsrun": "^1.2.8",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
"@pushrocks/tapbundle": "^3.0.11",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^12.7.2",
|
"@types/node": "^18.6.2"
|
||||||
"tslint": "^5.19.0",
|
},
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"files": [
|
||||||
}
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
58
test/test.ts
58
test/test.ts
@ -1,61 +1,7 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
import * as smartspawn from '../ts/index';
|
import * as smartspawn from '../ts/index.js';
|
||||||
|
|
||||||
let testThreadFunction: smartspawn.ThreadFunction;
|
tap.test('should create a simple test function', async () => {});
|
||||||
let testThread: smartspawn.Thread;
|
|
||||||
let testPool: smartspawn.Pool;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* create a normal ThreadFunction
|
|
||||||
*/
|
|
||||||
tap.test('should create an instance of ThreadFunction', async () => {
|
|
||||||
testThreadFunction = new smartspawn.ThreadFunction((input, done) => {
|
|
||||||
let url = require('url');
|
|
||||||
done(url.parse(input));
|
|
||||||
});
|
|
||||||
const message = await testThreadFunction.send('https://google.com');
|
|
||||||
console.log(message);
|
|
||||||
testThreadFunction.kill();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should create an instance of Thread', async () => {
|
|
||||||
smartspawn.setWorkerBasePath(__dirname);
|
|
||||||
testThread = new smartspawn.Thread('../testassets/child.ts');
|
|
||||||
testThread.enableTypeScript();
|
|
||||||
const message = await testThread.send('https://google.com');
|
|
||||||
console.log(message);
|
|
||||||
testThread.kill();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should not spawn when nothing is sent', async () => {
|
|
||||||
smartspawn.setWorkerBasePath(__dirname);
|
|
||||||
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should run in a Pool', async () => {
|
|
||||||
let testPool = new smartspawn.Pool();
|
|
||||||
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
|
||||||
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();
|
tap.start();
|
||||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartspawn',
|
||||||
|
version: '3.0.0',
|
||||||
|
description: 'smart subprocess handling'
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
export * from './smartspawn.classes.thread';
|
export * from './smartspawn.classes.threadsimple.js';
|
||||||
export * from './smartspawn.classes.threadfunction';
|
export * from './smartspawn.wrap.js';
|
||||||
export * from './smartspawn.classes.threadsimple';
|
|
||||||
export * from './smartspawn.classes.pool';
|
export * from 'threads';
|
||||||
export * from './smartspawn.wrap';
|
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
// ==============
|
|
||||||
// This file handles clustering of nodejs programs
|
|
||||||
// ==============
|
|
||||||
import * as plugins from './smartspawn.plugins';
|
|
||||||
|
|
||||||
export class Cluster {}
|
|
@ -1,13 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,85 +0,0 @@
|
|||||||
import * as plugins from './smartspawn.plugins';
|
|
||||||
import * as paths from './smartspawn.paths';
|
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
|
||||||
|
|
||||||
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 = smartpromise.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 = smartpromise.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
import * as plugins from './smartspawn.plugins';
|
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
|
||||||
|
|
||||||
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 = smartpromise.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,14 +1,12 @@
|
|||||||
import * as plugins from './smartspawn.plugins';
|
import * as plugins from './smartspawn.plugins.js';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as childProcess from 'child_process';
|
import * as childProcess from 'child_process';
|
||||||
|
|
||||||
import { workerBasePath } from './smartspawn.classes.thread';
|
|
||||||
|
|
||||||
export class ThreadSimple {
|
export class ThreadSimple {
|
||||||
workerPath: string;
|
public workerPath: string;
|
||||||
threadChildProcess: childProcess.ChildProcess;
|
public threadChildProcess: childProcess.ChildProcess;
|
||||||
forkOptions: childProcess.ForkOptions;
|
public forkOptions: childProcess.ForkOptions;
|
||||||
argvArgs: string[];
|
public argvArgs: string[];
|
||||||
constructor(
|
constructor(
|
||||||
filePathArg: string,
|
filePathArg: string,
|
||||||
argvArgs: string[] = [],
|
argvArgs: string[] = [],
|
||||||
@ -19,17 +17,13 @@ export class ThreadSimple {
|
|||||||
this.argvArgs = argvArgs;
|
this.argvArgs = argvArgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
run() {
|
public async start() {
|
||||||
let done = smartpromise.defer<childProcess.ChildProcess>();
|
const forkPath = this.workerPath;
|
||||||
let forkPath = (() => {
|
|
||||||
if (workerBasePath) {
|
|
||||||
return plugins.path.join(workerBasePath, this.workerPath);
|
|
||||||
} else {
|
|
||||||
return this.workerPath;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
this.threadChildProcess = childProcess.fork(forkPath, this.argvArgs, this.forkOptions);
|
this.threadChildProcess = childProcess.fork(forkPath, this.argvArgs, this.forkOptions);
|
||||||
done.resolve(this.threadChildProcess);
|
return this.threadChildProcess;
|
||||||
return done.promise;
|
}
|
||||||
|
|
||||||
|
public async stop() {
|
||||||
|
this.threadChildProcess.kill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './smartspawn.plugins';
|
import * as plugins from './smartspawn.plugins.js';
|
||||||
|
|
||||||
export const packageBase = plugins.path.join(__dirname, '../');
|
export const packageBase = plugins.path.join(__dirname, '../');
|
||||||
export const typescriptwrapJs = plugins.path.join(packageBase, 'assets/typescriptwrap.js');
|
export const typescriptwrapJs = plugins.path.join(packageBase, 'assets/typescriptwrap.js');
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
const threads = require('threads');
|
import * as threads from 'threads';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
export { path, smartpromise, threads };
|
export { path, smartpromise, threads };
|
||||||
|
@ -3,8 +3,8 @@ import * as spawnWrap from 'spawn-wrap';
|
|||||||
let unwrap: any = null;
|
let unwrap: any = null;
|
||||||
|
|
||||||
export const startSpawnWrap = (filePath: string, cliArgs: string[] = [], envArgs: any = {}) => {
|
export const startSpawnWrap = (filePath: string, cliArgs: string[] = [], envArgs: any = {}) => {
|
||||||
let spawnArray = [filePath];
|
const spawnArray = [filePath];
|
||||||
for (let cliArg of cliArgs) {
|
for (const cliArg of cliArgs) {
|
||||||
spawnArray.push(cliArg);
|
spawnArray.push(cliArg);
|
||||||
}
|
}
|
||||||
unwrap = spawnWrap(spawnArray, envArgs);
|
unwrap = spawnWrap(spawnArray, envArgs);
|
||||||
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"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