update smartipc internal code
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
export * from './smartipc.classes.thread'
|
||||
export * from './smartipc.classes.threadfunction'
|
||||
export * from './smartipc.classes.pool'
|
||||
export * from './smartipc.wrap'
|
||||
|
@ -4,57 +4,73 @@ import * as q from 'smartq'
|
||||
import { Pool } from './smartipc.classes.pool'
|
||||
|
||||
export let setWorkerBasePath = (basePathArg: string) => {
|
||||
plugins.threads.config.set({
|
||||
basepath: {
|
||||
node: basePathArg
|
||||
}
|
||||
})
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
||||
kill() {
|
||||
this.thread.kill()
|
||||
this.running = false
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
||||
assignToPool(poolArg: Pool) {
|
||||
this.assignedPool = poolArg
|
||||
}
|
||||
/**
|
||||
* kills the thread
|
||||
*/
|
||||
kill() {
|
||||
this.thread.kill()
|
||||
this.running = false
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,29 +3,29 @@ import * as plugins from './smartipc.plugins'
|
||||
import * as q from 'smartq'
|
||||
|
||||
export interface IThreadFunction {
|
||||
(input, done): void
|
||||
(input, done): void
|
||||
}
|
||||
|
||||
export class ThreadFunction {
|
||||
thread
|
||||
constructor(functionArg: IThreadFunction) {
|
||||
this.thread = plugins.threads.spawn(functionArg)
|
||||
}
|
||||
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
|
||||
}
|
||||
/**
|
||||
* 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()
|
||||
}
|
||||
kill() {
|
||||
this.thread.kill()
|
||||
}
|
||||
}
|
||||
|
18
ts/smartipc.wrap.ts
Normal file
18
ts/smartipc.wrap.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import * as spawnWrap from 'spawn-wrap'
|
||||
|
||||
let unwrap: any = null
|
||||
|
||||
export let startSpawnWrap = (filePath: string, cliArgs: string[], envArgs: any) => {
|
||||
let spawnArray = [filePath]
|
||||
for (let cliArg of cliArgs) {
|
||||
spawnArray.push(cliArg)
|
||||
}
|
||||
unwrap = spawnWrap(spawnArray, envArgs)
|
||||
}
|
||||
|
||||
export let endSpawnWrap = () => {
|
||||
if (unwrap) {
|
||||
unwrap()
|
||||
unwrap = null
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user