- /** {@inheritDoc} */
- public choose (): Worker {
- const chosenWorker = this.pool.workers[this.currentWorkerId].worker
- if (this.isDynamicPool && !this.workersTaskRunTime.has(chosenWorker)) {
- this.initWorkerTaskRunTime(chosenWorker)
- }
- const workerTaskRunTime =
- this.workersTaskRunTime.get(chosenWorker)?.runTime ?? 0
- const workerTaskWeight =
- this.workersTaskRunTime.get(chosenWorker)?.weight ??
- this.defaultWorkerWeight
- if (workerTaskRunTime < workerTaskWeight) {
- this.setWorkerTaskRunTime(
- chosenWorker,
- workerTaskWeight,
- workerTaskRunTime +
- (this.getWorkerVirtualTaskRunTime(chosenWorker) ?? 0)
- )
- } else {
- this.currentWorkerId =
- this.currentWorkerId === this.pool.workers.length - 1
- ? 0
- : this.currentWorkerId + 1
- this.setWorkerTaskRunTime(
- this.pool.workers[this.currentWorkerId].worker,
- workerTaskWeight,
- 0
- )
- }
- return chosenWorker
- }
-
- private initWorkersTaskRunTime (): void {
- for (const workerItem of this.pool.workers) {
- this.initWorkerTaskRunTime(workerItem.worker)
- }
- }
-
- private initWorkerTaskRunTime (worker: Worker): void {
- this.setWorkerTaskRunTime(worker, this.defaultWorkerWeight, 0)