smartspawn/ts/smartspawn.classes.threadsimple.ts

36 lines
1.0 KiB
TypeScript
Raw Normal View History

2018-08-02 13:31:05 +00:00
import * as plugins from './smartspawn.plugins';
2019-04-08 13:39:17 +00:00
import * as smartpromise from '@pushrocks/smartpromise';
2018-08-02 13:31:05 +00:00
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() {
2019-04-08 13:39:17 +00:00
let done = smartpromise.defer<childProcess.ChildProcess>();
2018-08-02 13:31:05 +00:00
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;
}
}