throw new Error(
'Cannot instantiate a pool without specifying the number of workers'
)
- } else if (!Number.isSafeInteger(numberOfWorkers)) {
+ } else if (Number.isSafeInteger(numberOfWorkers) === false) {
throw new Error(
'Cannot instantiate a pool with a non integer number of workers'
)
}
/**
- * Shut down given worker.
+ * Shutdowns given worker.
*
* @param worker A worker within `workers`.
*/
protected removeWorker (worker: Worker): void {
// Clean worker from data structure
this.workers.splice(this.getWorkerIndex(worker), 1)
- this.resetWorkerTasksUsage(worker)
+ this.removeWorkerTasksUsage(worker)
}
/**
- * Choose a worker for the next task.
+ * Chooses a worker for the next task.
*
* The default implementation uses a round robin algorithm to distribute the load.
*
}
/**
- * Send a message to the given worker.
+ * Sends a message to the given worker.
*
* @param worker The worker which should receive the message.
* @param message The message.
): void
/**
- * Register a listener callback on a given worker.
+ * Registers a listener callback on a given worker.
*
* @param worker A worker.
* @param listener A message listener callback.
}
/**
- * Increase the number of tasks that the given worker has applied.
+ * Increases the number of tasks that the given worker has applied.
*
* @param worker Worker which running tasks is increased.
*/
}
/**
- * Decrease the number of tasks that the given worker has applied.
+ * Decreases the number of tasks that the given worker has applied.
*
* @param worker Worker which running tasks is decreased.
*/
}
/**
- * Step the number of tasks that the given worker has applied.
+ * 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.
}
/**
- * Step the number of tasks that the given worker has run.
+ * 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) {
+ private stepWorkerRunTasks (worker: Worker, step: number): void {
const tasksUsage = this.workersTasksUsage.get(worker)
if (tasksUsage !== undefined) {
tasksUsage.run = tasksUsage.run + step
}
/**
- * Update tasks run time for the given worker.
+ * Updates tasks runtime for the given worker.
*
* @param worker Worker which run the task.
- * @param taskRunTime Worker task run time.
+ * @param taskRunTime Worker task runtime.
*/
private updateWorkerTasksRunTime (
worker: Worker,
taskRunTime: number | undefined
- ) {
- const tasksUsage = this.workersTasksUsage.get(worker)
- if (tasksUsage !== undefined && tasksUsage.run !== 0) {
- tasksUsage.runTime += taskRunTime ?? 0
- tasksUsage.avgRunTime = tasksUsage.runTime / tasksUsage.run
- this.workersTasksUsage.set(worker, tasksUsage)
- } else {
- throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP)
+ ): void {
+ if (
+ this.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+ .requiredStatistics.runTime === true
+ ) {
+ const tasksUsage = this.workersTasksUsage.get(worker)
+ if (tasksUsage !== undefined) {
+ tasksUsage.runTime += taskRunTime ?? 0
+ if (tasksUsage.run !== 0) {
+ tasksUsage.avgRunTime = tasksUsage.runTime / tasksUsage.run
+ }
+ this.workersTasksUsage.set(worker, tasksUsage)
+ } else {
+ throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP)
+ }
}
}
/**
- * Reset worker tasks usage statistics.
+ * Removes worker tasks usage statistics.
*
* @param worker The worker.
*/
- private resetWorkerTasksUsage (worker: Worker): void {
+ private removeWorkerTasksUsage (worker: Worker): void {
this.workersTasksUsage.delete(worker)
}
}