+ protected chooseWorker (): [number, Worker] {
+ let workerKey: number
+ if (
+ this.type === PoolType.DYNAMIC &&
+ !this.full &&
+ this.findFreeWorkerKey() === -1
+ ) {
+ const createdWorker = this.createAndSetupWorker()
+ this.registerWorkerMessageListener(createdWorker, message => {
+ if (
+ isKillBehavior(KillBehaviors.HARD, message.kill) ||
+ (message.kill != null &&
+ this.getWorkerTasksUsage(createdWorker)?.running === 0)
+ ) {
+ // Kill message received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
+ void this.destroyWorker(createdWorker)
+ }
+ })
+ workerKey = this.getWorkerKey(createdWorker)
+ } else {
+ workerKey = this.workerChoiceStrategyContext.execute()
+ }
+ return [workerKey, this.workers[workerKey].worker]