}
const timestamp = performance.now()
const workerNodeKey = this.chooseWorkerNode()
+ const workerInfo = this.getWorkerInfo(workerNodeKey)
if (
name != null &&
- Array.isArray(this.getWorkerInfo(workerNodeKey).taskFunctions) &&
- !(this.getWorkerInfo(workerNodeKey).taskFunctions as string[]).includes(
- name
- )
+ Array.isArray(workerInfo.taskFunctions) &&
+ !workerInfo.taskFunctions.includes(name)
) {
reject(
new Error(`Task function '${name}' is not registered in the pool`)
data: data ?? ({} as Data),
transferList,
timestamp,
- workerId: this.getWorkerInfo(workerNodeKey).id as number,
+ workerId: workerInfo.id as number,
taskId: randomUUID()
}
this.promiseResponseMap.set(task.taskId as string, {
}
private canUpdateTaskWorkerUsage (workerNodeKey: number): boolean {
+ const workerInfo = this.getWorkerInfo(workerNodeKey)
return (
- Array.isArray(this.getWorkerInfo(workerNodeKey).taskFunctions) &&
- (this.getWorkerInfo(workerNodeKey).taskFunctions as string[]).length > 1
+ Array.isArray(workerInfo.taskFunctions) &&
+ workerInfo.taskFunctions.length > 1
)
}
protected workerListener (): (message: MessageValue<Response>) => void {
return (message) => {
this.checkMessageWorkerId(message)
- if (message.ready != null) {
+ if (message.ready != null && message.taskFunctions != null) {
// Worker ready response received from worker
this.handleWorkerReadyResponse(message)
} else if (message.taskId != null) {
if (message.ready === false) {
throw new Error(`Worker ${message.workerId} failed to initialize`)
}
- this.getWorkerInfo(
+ const workerInfo = this.getWorkerInfo(
this.getWorkerNodeKeyByWorkerId(message.workerId)
- ).ready = message.ready as boolean
+ )
+ workerInfo.ready = message.ready as boolean
+ workerInfo.taskFunctions = message.taskFunctions
if (this.emitter != null && this.ready) {
this.emitter.emit(PoolEvents.ready, this.info)
}
this.workerNodes.push(workerNode)
const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
if (workerNodeKey === -1) {
- throw new Error('Worker node not found')
+ throw new Error('Worker node added not found')
}
return workerNodeKey
}
if (message.workerId === this.id && message.ready === false) {
try {
this.getMainWorker()?.on('message', this.messageListener.bind(this))
- this.sendTaskFunctionsListToMainWorker()
- this.sendToMainWorker({ ready: true, workerId: this.id })
+ this.sendToMainWorker({
+ ready: true,
+ taskFunctions: this.listTaskFunctions(),
+ workerId: this.id
+ })
} catch {
- this.sendToMainWorker({ ready: false, workerId: this.id })
+ this.sendToMainWorker({
+ ready: false,
+ taskFunctions: this.listTaskFunctions(),
+ workerId: this.id
+ })
}
}
}
try {
this.port = message.port
this.port.on('message', this.messageListener.bind(this))
- this.sendTaskFunctionsListToMainWorker()
- this.sendToMainWorker({ ready: true, workerId: this.id })
+ this.sendToMainWorker({
+ ready: true,
+ taskFunctions: this.listTaskFunctions(),
+ workerId: this.id
+ })
} catch {
- this.sendToMainWorker({ ready: false, workerId: this.id })
+ this.sendToMainWorker({
+ ready: false,
+ taskFunctions: this.listTaskFunctions(),
+ workerId: this.id
+ })
}
}
}