+ if (isPlainObject(opts)) {
+ this.opts.workerChoiceStrategy =
+ opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
+ this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
+ this.opts.workerChoiceStrategyOptions =
+ opts.workerChoiceStrategyOptions ??
+ DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ this.checkValidWorkerChoiceStrategyOptions(
+ this.opts.workerChoiceStrategyOptions
+ )
+ this.opts.restartWorkerOnError = opts.restartWorkerOnError ?? true
+ this.opts.enableEvents = opts.enableEvents ?? true
+ this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
+ if (this.opts.enableTasksQueue) {
+ this.checkValidTasksQueueOptions(
+ opts.tasksQueueOptions as TasksQueueOptions
+ )
+ this.opts.tasksQueueOptions = this.buildTasksQueueOptions(
+ opts.tasksQueueOptions as TasksQueueOptions
+ )
+ }
+ } else {
+ throw new TypeError('Invalid pool options: must be a plain object')
+ }
+ }
+
+ private checkValidWorkerChoiceStrategy (
+ workerChoiceStrategy: WorkerChoiceStrategy
+ ): void {
+ if (!Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)) {
+ throw new Error(
+ `Invalid worker choice strategy '${workerChoiceStrategy}'`
+ )
+ }
+ }
+
+ private checkValidWorkerChoiceStrategyOptions (
+ workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
+ ): void {
+ if (!isPlainObject(workerChoiceStrategyOptions)) {
+ throw new TypeError(
+ 'Invalid worker choice strategy options: must be a plain object'
+ )
+ }
+ if (
+ workerChoiceStrategyOptions.weights != null &&
+ Object.keys(workerChoiceStrategyOptions.weights).length !== this.maxSize
+ ) {
+ throw new Error(
+ 'Invalid worker choice strategy options: must have a weight for each worker node'
+ )
+ }
+ if (
+ workerChoiceStrategyOptions.measurement != null &&
+ !Object.values(Measurements).includes(
+ workerChoiceStrategyOptions.measurement
+ )
+ ) {
+ throw new Error(
+ `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'`
+ )
+ }
+ }
+
+ private checkValidTasksQueueOptions (
+ tasksQueueOptions: TasksQueueOptions
+ ): void {
+ if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
+ throw new TypeError('Invalid tasks queue options: must be a plain object')
+ }
+ if (
+ tasksQueueOptions?.concurrency != null &&
+ !Number.isSafeInteger(tasksQueueOptions.concurrency)
+ ) {
+ throw new TypeError(
+ 'Invalid worker tasks concurrency: must be an integer'
+ )
+ }
+ if (
+ tasksQueueOptions?.concurrency != null &&
+ tasksQueueOptions.concurrency <= 0
+ ) {
+ throw new Error(
+ `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}'`
+ )
+ }
+ }
+
+ private startPool (): void {
+ while (
+ this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ !workerNode.info.dynamic ? accumulator + 1 : accumulator,
+ 0
+ ) < this.numberOfWorkers
+ ) {
+ this.createAndSetupWorkerNode()
+ }
+ }
+
+ /** @inheritDoc */
+ public get info (): PoolInfo {
+ return {
+ version,
+ type: this.type,
+ worker: this.worker,
+ ready: this.ready,
+ strategy: this.opts.workerChoiceStrategy as WorkerChoiceStrategy,
+ minSize: this.minSize,
+ maxSize: this.maxSize,
+ ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .runTime.aggregate &&
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .waitTime.aggregate && { utilization: round(this.utilization) }),
+ workerNodes: this.workerNodes.length,
+ idleWorkerNodes: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ workerNode.usage.tasks.executing === 0
+ ? accumulator + 1
+ : accumulator,
+ 0
+ ),
+ busyWorkerNodes: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ workerNode.usage.tasks.executing > 0 ? accumulator + 1 : accumulator,
+ 0
+ ),
+ executedTasks: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.usage.tasks.executed,
+ 0
+ ),
+ executingTasks: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.usage.tasks.executing,
+ 0
+ ),
+ ...(this.opts.enableTasksQueue === true && {
+ queuedTasks: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.usage.tasks.queued,
+ 0
+ )
+ }),
+ ...(this.opts.enableTasksQueue === true && {
+ maxQueuedTasks: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + (workerNode.usage.tasks?.maxQueued ?? 0),
+ 0
+ )
+ }),
+ failedTasks: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.usage.tasks.failed,
+ 0
+ ),
+ ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .runTime.aggregate && {
+ runTime: {
+ minimum: round(
+ Math.min(
+ ...this.workerNodes.map(
+ (workerNode) => workerNode.usage.runTime?.minimum ?? Infinity
+ )
+ )
+ ),
+ maximum: round(
+ Math.max(
+ ...this.workerNodes.map(
+ (workerNode) => workerNode.usage.runTime?.maximum ?? -Infinity
+ )
+ )
+ ),
+ average: round(
+ this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + (workerNode.usage.runTime?.aggregate ?? 0),
+ 0
+ ) /
+ this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + (workerNode.usage.tasks?.executed ?? 0),
+ 0
+ )
+ ),
+ ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .runTime.median && {
+ median: round(
+ median(
+ this.workerNodes.map(
+ (workerNode) => workerNode.usage.runTime?.median ?? 0
+ )
+ )
+ )
+ })
+ }
+ }),
+ ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .waitTime.aggregate && {
+ waitTime: {
+ minimum: round(
+ Math.min(
+ ...this.workerNodes.map(
+ (workerNode) => workerNode.usage.waitTime?.minimum ?? Infinity
+ )
+ )
+ ),
+ maximum: round(
+ Math.max(
+ ...this.workerNodes.map(
+ (workerNode) => workerNode.usage.waitTime?.maximum ?? -Infinity
+ )
+ )
+ ),
+ average: round(
+ this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + (workerNode.usage.waitTime?.aggregate ?? 0),
+ 0
+ ) /
+ this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + (workerNode.usage.tasks?.executed ?? 0),
+ 0
+ )
+ ),
+ ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .waitTime.median && {
+ median: round(
+ median(
+ this.workerNodes.map(
+ (workerNode) => workerNode.usage.waitTime?.median ?? 0
+ )
+ )
+ )
+ })
+ }
+ })
+ }
+ }
+
+ /**
+ * The pool readiness boolean status.
+ */
+ private get ready (): boolean {
+ return (
+ this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ !workerNode.info.dynamic && workerNode.info.ready
+ ? accumulator + 1
+ : accumulator,
+ 0
+ ) >= this.minSize
+ )