+ }
+
+ private async internalExecute (
+ worker: Worker,
+ messageId: string
+ ): Promise<Response> {
+ this.beforePromiseWorkerResponseHook(worker)
+ return await new Promise<Response>((resolve, reject) => {
+ this.promiseMap.set(messageId, { resolve, reject, worker })
+ })
+ }
+
+ private checkAndEmitBusy (): void {
+ if (this.opts.enableEvents === true && this.busy) {
+ this.emitter?.emit('busy')
+ }
+ }
+
+ /**
+ * Increases the number of tasks that the given worker has applied.
+ *
+ * @param worker - Worker which running tasks is increased.
+ */
+ private increaseWorkerRunningTasks (worker: Worker): void {
+ this.stepWorkerRunningTasks(worker, 1)
+ }
+
+ /**
+ * Decreases the number of tasks that the given worker has applied.
+ *
+ * @param worker - Worker which running tasks is decreased.
+ */
+ private decreaseWorkerRunningTasks (worker: Worker): void {
+ this.stepWorkerRunningTasks(worker, -1)
+ }
+
+ /**
+ * Gets tasks usage of the given worker.
+ *
+ * @param worker - Worker which tasks usage is returned.
+ */
+ private getWorkerTasksUsage (worker: Worker): TasksUsage | undefined {
+ if (this.checkWorker(worker)) {
+ const workerKey = this.getWorkerKey(worker) as number
+ const workerEntry = this.workers.get(workerKey) as WorkerType<Worker>
+ return workerEntry.tasksUsage
+ }
+ }
+
+ /**
+ * Steps the number of tasks that the given worker has applied.
+ *
+ * @param worker - Worker which running tasks are stepped.
+ * @param step - Number of running tasks step.
+ */
+ private stepWorkerRunningTasks (worker: Worker, step: number): void {
+ // prettier-ignore
+ (this.getWorkerTasksUsage(worker) as TasksUsage).running += step
+ }
+
+ /**
+ * Steps the number of tasks that the given worker has run.
+ *
+ * @param worker - Worker which has run tasks.
+ * @param step - Number of run tasks step.
+ */
+ private stepWorkerRunTasks (worker: Worker, step: number): void {
+ // prettier-ignore
+ (this.getWorkerTasksUsage(worker) as TasksUsage).run += step
+ }
+
+ /**
+ * Updates tasks runtime for the given worker.
+ *
+ * @param worker - Worker which run the task.
+ * @param taskRunTime - Worker task runtime.
+ */
+ private updateWorkerTasksRunTime (
+ worker: Worker,
+ taskRunTime: number | undefined
+ ): void {
+ if (
+ this.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime
+ ) {
+ const workerTasksUsage = this.getWorkerTasksUsage(worker) as TasksUsage
+ workerTasksUsage.runTime += taskRunTime ?? 0
+ if (workerTasksUsage.run !== 0) {
+ workerTasksUsage.avgRunTime =
+ workerTasksUsage.runTime / workerTasksUsage.run
+ }
+ }
+ }
+
+ /**
+ * Sets the given worker.
+ *
+ * @param workerKey - The worker key.
+ * @param worker - The worker.
+ * @param tasksUsage - The worker tasks usage.
+ */
+ private setWorker (
+ workerKey: number,
+ worker: Worker,
+ tasksUsage: TasksUsage
+ ): void {
+ this.workers.set(workerKey, {
+ worker,
+ tasksUsage
+ })
+ }
+
+ /**
+ * Checks if the given worker is registered in the pool.
+ *
+ * @param worker - Worker to check.
+ * @returns `true` if the worker is registered in the pool.
+ */
+ private checkWorker (worker: Worker): boolean {
+ if (this.getWorkerKey(worker) == null) {
+ throw new Error('Worker could not be found in the pool')
+ }
+ return true