if (!Number.isSafeInteger(this.workerOptions.elementsPerWorker)) {
throw new TypeError('Elements per worker must be an integer')
}
- if (this.workerOptions.elementsPerWorker <= 0) {
+ if (
+ typeof this.workerOptions.elementsPerWorker === 'number' &&
+ this.workerOptions.elementsPerWorker <= 0
+ ) {
throw new RangeError('Elements per worker must be greater than zero')
}
this.workerSet = new Set<WorkerSetElement>()
version: workerSetVersion,
type: 'set',
worker: 'thread',
+ started: this.started,
size: this.size,
elementsExecuting: [...this.workerSet].reduce(
(accumulator, workerSetElement) => accumulator + workerSetElement.numberOfWorkerElements,
resolve()
})
})
+ worker.unref()
await worker.terminate()
await waitWorkerExit
- this.emitter?.emit(WorkerSetEvents.stopped, this.info)
- this.emitter?.emitDestroy()
- this.emitter?.removeAllListeners()
- this.started = false
}
+ this.emitter?.emit(WorkerSetEvents.stopped, this.info)
+ this.started = false
+ this.emitter?.emitDestroy()
}
/** @inheritDoc */
++workerSetElement.numberOfWorkerElements
// Add element sequentially to optimize memory at startup
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- if (this.workerOptions.elementStartDelay! > 0) {
+ if (this.workerOptions.elementAddDelay! > 0) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- await sleep(randomizeDelay(this.workerOptions.elementStartDelay!))
+ await sleep(randomizeDelay(this.workerOptions.elementAddDelay!))
}
}
) {
this.addWorkerSetElement()
}
+ worker.unref()
+ worker.terminate().catch((error: unknown) => this.emitter?.emit(WorkerSetEvents.error, error))
})
worker.on('online', this.workerOptions.poolOptions?.onlineHandler ?? EMPTY_FUNCTION)
worker.on('exit', this.workerOptions.poolOptions?.exitHandler ?? EMPTY_FUNCTION)