smartspawn/ts/smartipc.classes.threadsimple.ts

32 lines
976 B
TypeScript
Raw Normal View History

2017-03-04 10:40:32 +00:00
import * as plugins from './smartipc.plugins'
import * as smartq from 'smartq'
2017-03-04 10:40:32 +00:00
import * as childProcess from 'child_process'
import { workerBasePath } from './smartipc.classes.thread'
export class ThreadSimple {
workerPath: string
threadChildProcess: childProcess.ChildProcess
2017-03-04 11:24:32 +00:00
forkOptions: childProcess.ForkOptions
2017-03-04 18:20:00 +00:00
argvArgs: string[]
constructor (filePathArg: string, argvArgs: string[] = [], forkOptionsArg: childProcess.ForkOptions = {}) {
2017-03-04 10:40:32 +00:00
this.workerPath = filePathArg
2017-03-04 11:24:32 +00:00
this.forkOptions = forkOptionsArg
2017-03-04 18:20:00 +00:00
this.argvArgs = argvArgs
2017-03-04 10:40:32 +00:00
}
run () {
2017-03-04 11:05:53 +00:00
let done = smartq.defer<childProcess.ChildProcess>()
2017-03-04 10:40:32 +00:00
let forkPath = (() => {
if (workerBasePath) {
return plugins.path.join(workerBasePath, this.workerPath)
} else {
return this.workerPath
}
})()
2017-03-04 18:20:00 +00:00
this.threadChildProcess = childProcess.fork(forkPath, this.argvArgs, this.forkOptions)
done.resolve(this.threadChildProcess)
return done.promise
2017-03-04 10:40:32 +00:00
}
}