fix(core): update
This commit is contained in:
parent
fcdcd4b929
commit
0220252b28
78
package-lock.json
generated
78
package-lock.json
generated
@ -435,6 +435,11 @@
|
||||
"integrity": "sha512-SOhuU4wNBxhhTHxYaiG5NY4HBhDIDnJF60GU+2LqHAdKKer86//e4yg69aENCtQ04n0ovz+tq2YPME5t5yp4pw==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/zen-observable": {
|
||||
"version": "0.8.0",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fzen-observable/-/zen-observable-0.8.0.tgz",
|
||||
"integrity": "sha512-te5lMAWii1uEJ4FwLjzdlbw3+n0FZNOvFXHxQDKeT0dilh7HOzdMzV2TrJVUzq8ep7J4Na8OUYPRLSQkJHAlrg=="
|
||||
},
|
||||
"ansi-256-colors": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://verdaccio.lossless.one/ansi-256-colors/-/ansi-256-colors-1.1.0.tgz",
|
||||
@ -518,6 +523,11 @@
|
||||
"integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=",
|
||||
"dev": true
|
||||
},
|
||||
"callsites": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://verdaccio.lossless.one/callsites/-/callsites-3.1.0.tgz",
|
||||
"integrity": "sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ=="
|
||||
},
|
||||
"camelcase": {
|
||||
"version": "5.3.1",
|
||||
"resolved": "https://verdaccio.lossless.one/camelcase/-/camelcase-5.3.1.tgz",
|
||||
@ -690,6 +700,14 @@
|
||||
"which": "^1.2.9"
|
||||
}
|
||||
},
|
||||
"debug": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://verdaccio.lossless.one/debug/-/debug-4.1.1.tgz",
|
||||
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
|
||||
"requires": {
|
||||
"ms": "^2.1.1"
|
||||
}
|
||||
},
|
||||
"decamelize": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://verdaccio.lossless.one/decamelize/-/decamelize-1.2.0.tgz",
|
||||
@ -755,6 +773,11 @@
|
||||
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
|
||||
"dev": true
|
||||
},
|
||||
"esm": {
|
||||
"version": "3.2.25",
|
||||
"resolved": "https://verdaccio.lossless.one/esm/-/esm-3.2.25.tgz",
|
||||
"integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA=="
|
||||
},
|
||||
"esprima": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
|
||||
@ -767,11 +790,6 @@
|
||||
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
|
||||
"dev": true
|
||||
},
|
||||
"eventemitter3": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-2.0.3.tgz",
|
||||
"integrity": "sha1-teEHm1n7XhuidxwKmTvgYKWMmbo="
|
||||
},
|
||||
"execa": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://verdaccio.lossless.one/execa/-/execa-1.0.0.tgz",
|
||||
@ -943,6 +961,14 @@
|
||||
"integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=",
|
||||
"dev": true
|
||||
},
|
||||
"is-observable": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://verdaccio.lossless.one/is-observable/-/is-observable-1.1.0.tgz",
|
||||
"integrity": "sha512-NqCa4Sa2d+u7BWc6CukaObG3Fh+CU9bvixbpcXYhy2VvYS7vVGIdAgnIS5Ks3A/cqk4rebLJ9s8zBstT2aKnIA==",
|
||||
"requires": {
|
||||
"symbol-observable": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"is-promise": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://verdaccio.lossless.one/is-promise/-/is-promise-2.1.0.tgz",
|
||||
@ -1150,17 +1176,17 @@
|
||||
"moment": ">= 2.9.0"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://verdaccio.lossless.one/ms/-/ms-2.1.2.tgz",
|
||||
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
|
||||
},
|
||||
"nan": {
|
||||
"version": "2.14.0",
|
||||
"resolved": "https://verdaccio.lossless.one/nan/-/nan-2.14.0.tgz",
|
||||
"integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==",
|
||||
"dev": true
|
||||
},
|
||||
"native-promise-only": {
|
||||
"version": "0.8.1",
|
||||
"resolved": "https://registry.npmjs.org/native-promise-only/-/native-promise-only-0.8.1.tgz",
|
||||
"integrity": "sha1-IKMYwwy0X3H+et+/eyHJnBRy7xE="
|
||||
},
|
||||
"nice-try": {
|
||||
"version": "1.0.5",
|
||||
"resolved": "https://verdaccio.lossless.one/nice-try/-/nice-try-1.0.5.tgz",
|
||||
@ -1591,6 +1617,11 @@
|
||||
"has-flag": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"symbol-observable": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://verdaccio.lossless.one/symbol-observable/-/symbol-observable-1.2.0.tgz",
|
||||
"integrity": "sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ=="
|
||||
},
|
||||
"symbol-tree": {
|
||||
"version": "3.2.4",
|
||||
"resolved": "https://verdaccio.lossless.one/symbol-tree/-/symbol-tree-3.2.4.tgz",
|
||||
@ -1598,12 +1629,24 @@
|
||||
"dev": true
|
||||
},
|
||||
"threads": {
|
||||
"version": "0.12.1",
|
||||
"resolved": "https://verdaccio.lossless.one/threads/-/threads-0.12.1.tgz",
|
||||
"integrity": "sha512-2USeV2FDNZtVP1xt3TW86solU7W6WYjaTMd+gvAHJat+dEMFh7of2DR9MEFLkhMerxc1t5b1DMJiVx1meRzk0w==",
|
||||
"version": "1.0.0-beta.4",
|
||||
"resolved": "https://verdaccio.lossless.one/threads/-/threads-1.0.0-beta.4.tgz",
|
||||
"integrity": "sha512-iQNEWLuOmdBwMLqKX357VT0hkFS8/fmHXntycnq24ZE8IRh+cAsLvriuhMuv2uRP/ftzK/G8GikyKHZov8nAmg==",
|
||||
"requires": {
|
||||
"eventemitter3": "^2.0.2",
|
||||
"native-promise-only": "^0.8.1"
|
||||
"@types/zen-observable": "^0.8.0",
|
||||
"callsites": "^3.1.0",
|
||||
"debug": "^4.1.1",
|
||||
"is-observable": "^1.1.0",
|
||||
"tiny-worker": ">= 2",
|
||||
"zen-observable": "^0.8.14"
|
||||
}
|
||||
},
|
||||
"tiny-worker": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://verdaccio.lossless.one/tiny-worker/-/tiny-worker-2.2.0.tgz",
|
||||
"integrity": "sha512-/DnTni0fPjjOlqA6BNcUo6pxyfbViJZ7vxwA+RZ+gF0dBevxp0sropQFVZ4NS5PRgQ8J7EBu8zMJYKwoNo5aWg==",
|
||||
"requires": {
|
||||
"esm": "^3.2.22"
|
||||
}
|
||||
},
|
||||
"ts-node": {
|
||||
@ -1841,6 +1884,11 @@
|
||||
"resolved": "https://verdaccio.lossless.one/yn/-/yn-3.1.1.tgz",
|
||||
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
|
||||
"dev": true
|
||||
},
|
||||
"zen-observable": {
|
||||
"version": "0.8.14",
|
||||
"resolved": "https://verdaccio.lossless.one/zen-observable/-/zen-observable-0.8.14.tgz",
|
||||
"integrity": "sha512-kQz39uonEjEESwh+qCi83kcC3rZJGh4mrZW7xjkSQYXkq//JZHTtKo+6yuVloTgMtzsIWOJrjIrKvk/dqm0L5g=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,8 @@
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"spawn-wrap": "^1.4.2",
|
||||
"threads": "^0.12.1"
|
||||
"threads": "1.0.0-beta.4",
|
||||
"tiny-worker": "^2.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.11",
|
||||
|
56
test/test.ts
56
test/test.ts
@ -2,60 +2,6 @@ import { expect, tap } from '@pushrocks/tapbundle';
|
||||
|
||||
import * as smartspawn from '../ts/index';
|
||||
|
||||
let testThreadFunction: smartspawn.ThreadFunction;
|
||||
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.test('should create a simple test function', async () => {});
|
||||
|
||||
tap.start();
|
||||
|
@ -1,5 +1,4 @@
|
||||
export * from './smartspawn.classes.thread';
|
||||
export * from './smartspawn.classes.threadfunction';
|
||||
export * from './smartspawn.classes.threadsimple';
|
||||
export * from './smartspawn.classes.pool';
|
||||
export * from './smartspawn.wrap';
|
||||
|
||||
export * from 'threads';
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -2,13 +2,11 @@ import * as plugins from './smartspawn.plugins';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
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[];
|
||||
public workerPath: string;
|
||||
public threadChildProcess: childProcess.ChildProcess;
|
||||
public forkOptions: childProcess.ForkOptions;
|
||||
public argvArgs: string[];
|
||||
constructor(
|
||||
filePathArg: string,
|
||||
argvArgs: string[] = [],
|
||||
@ -19,17 +17,13 @@ export class ThreadSimple {
|
||||
this.argvArgs = argvArgs;
|
||||
}
|
||||
|
||||
run() {
|
||||
let done = smartpromise.defer<childProcess.ChildProcess>();
|
||||
let forkPath = (() => {
|
||||
if (workerBasePath) {
|
||||
return plugins.path.join(workerBasePath, this.workerPath);
|
||||
} else {
|
||||
return this.workerPath;
|
||||
}
|
||||
})();
|
||||
public async start() {
|
||||
const forkPath = this.workerPath;
|
||||
this.threadChildProcess = childProcess.fork(forkPath, this.argvArgs, this.forkOptions);
|
||||
done.resolve(this.threadChildProcess);
|
||||
return done.promise;
|
||||
return this.threadChildProcess;
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
this.threadChildProcess.kill();
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ 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) {
|
||||
const spawnArray = [filePath];
|
||||
for (const cliArg of cliArgs) {
|
||||
spawnArray.push(cliArg);
|
||||
}
|
||||
unwrap = spawnWrap(spawnArray, envArgs);
|
||||
|
Loading…
Reference in New Issue
Block a user