import crypto from 'node:crypto'
+import { performance } from 'node:perf_hooks'
import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
import {
DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
type TasksQueueOptions,
type WorkerType
} from './pool'
-import type { IWorker, Task, TasksUsage, WorkerNode } from './worker'
+import type {
+ IWorker,
+ Task,
+ TaskStatistics,
+ WorkerNode,
+ WorkerUsage
+} from './worker'
import {
WorkerChoiceStrategies,
type WorkerChoiceStrategy,
/**
* Worker choice strategy context referencing a worker choice algorithm implementation.
- *
- * Default to a round robin algorithm.
*/
protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<
Worker,
this.enqueueTask = this.enqueueTask.bind(this)
this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
- this.setupHook()
-
- for (let i = 1; i <= this.numberOfWorkers; i++) {
- this.createAndSetupWorker()
- }
-
if (this.opts.enableEvents === true) {
this.emitter = new PoolEmitter()
}
this.opts.workerChoiceStrategy,
this.opts.workerChoiceStrategyOptions
)
+
+ this.setupHook()
+
+ for (let i = 1; i <= this.numberOfWorkers; i++) {
+ this.createAndSetupWorker()
+ }
}
private checkFilePath (filePath: string): void {
workerNodes: this.workerNodes.length,
idleWorkerNodes: this.workerNodes.reduce(
(accumulator, workerNode) =>
- workerNode.tasksUsage.running === 0 ? accumulator + 1 : accumulator,
+ workerNode.workerUsage.tasks.executing === 0
+ ? accumulator + 1
+ : accumulator,
0
),
busyWorkerNodes: this.workerNodes.reduce(
(accumulator, workerNode) =>
- workerNode.tasksUsage.running > 0 ? accumulator + 1 : accumulator,
+ workerNode.workerUsage.tasks.executing > 0
+ ? accumulator + 1
+ : accumulator,
+ 0
+ ),
+ executedTasks: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.workerUsage.tasks.executed,
0
),
- runningTasks: this.workerNodes.reduce(
+ executingTasks: this.workerNodes.reduce(
(accumulator, workerNode) =>
- accumulator + workerNode.tasksUsage.running,
+ accumulator + workerNode.workerUsage.tasks.executing,
0
),
queuedTasks: this.workerNodes.reduce(
(accumulator, workerNode) =>
accumulator + workerNode.tasksQueue.maxSize,
0
+ ),
+ failedTasks: this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.workerUsage.tasks.failed,
+ 0
)
}
}
): void {
this.checkValidWorkerChoiceStrategy(workerChoiceStrategy)
this.opts.workerChoiceStrategy = workerChoiceStrategy
- for (const workerNode of this.workerNodes) {
- this.setWorkerNodeTasksUsage(workerNode, {
- ran: 0,
- running: 0,
- runTime: 0,
- runTimeHistory: new CircularArray(),
- avgRunTime: 0,
- medRunTime: 0,
- waitTime: 0,
- waitTimeHistory: new CircularArray(),
- avgWaitTime: 0,
- medWaitTime: 0,
- error: 0
- })
- }
this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
this.opts.workerChoiceStrategy
)
if (workerChoiceStrategyOptions != null) {
this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
}
+ for (const workerNode of this.workerNodes) {
+ this.setWorkerNodeTasksUsage(
+ workerNode,
+ this.getWorkerUsage(workerNode.worker)
+ )
+ this.setWorkerStatistics(workerNode.worker)
+ }
}
/** @inheritDoc */
this.checkValidTasksQueueOptions(tasksQueueOptions)
this.opts.tasksQueueOptions =
this.buildTasksQueueOptions(tasksQueueOptions)
- } else {
+ } else if (this.opts.tasksQueueOptions != null) {
delete this.opts.tasksQueueOptions
}
}
protected internalBusy (): boolean {
return (
this.workerNodes.findIndex(workerNode => {
- return workerNode.tasksUsage.running === 0
+ return workerNode.workerUsage.tasks.executing === 0
}) === -1
)
}
/** @inheritDoc */
public async execute (data?: Data, name?: string): Promise<Response> {
- const submissionTimestamp = performance.now()
+ const timestamp = performance.now()
const workerNodeKey = this.chooseWorkerNode()
const submittedTask: Task<Data> = {
name,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
data: data ?? ({} as Data),
- submissionTimestamp,
+ timestamp,
id: crypto.randomUUID()
}
const res = new Promise<Response>((resolve, reject) => {
if (
this.opts.enableTasksQueue === true &&
(this.busy ||
- this.workerNodes[workerNodeKey].tasksUsage.running >=
+ this.workerNodes[workerNodeKey].workerUsage.tasks.executing >=
((this.opts.tasksQueueOptions as TasksQueueOptions)
.concurrency as number))
) {
* Can be overridden.
*
* @param workerNodeKey - The worker node key.
+ * @param task - The task to execute.
*/
- protected beforeTaskExecutionHook (workerNodeKey: number): void {
- ++this.workerNodes[workerNodeKey].tasksUsage.running
+ protected beforeTaskExecutionHook (
+ workerNodeKey: number,
+ task: Task<Data>
+ ): void {
+ const workerUsage = this.workerNodes[workerNodeKey].workerUsage
+ ++workerUsage.tasks.executing
+ this.updateWaitTimeWorkerUsage(workerUsage, task)
}
/**
worker: Worker,
message: MessageValue<Response>
): void {
- const workerTasksUsage =
- this.workerNodes[this.getWorkerNodeKey(worker)].tasksUsage
- --workerTasksUsage.running
- ++workerTasksUsage.ran
- if (message.error != null) {
- ++workerTasksUsage.error
+ const workerUsage =
+ this.workerNodes[this.getWorkerNodeKey(worker)].workerUsage
+ const workerTaskStatistics = workerUsage.tasks
+ --workerTaskStatistics.executing
+ ++workerTaskStatistics.executed
+ if (message.taskError != null) {
+ ++workerTaskStatistics.failed
}
- this.updateRunTimeTasksUsage(workerTasksUsage, message)
- this.updateWaitTimeTasksUsage(workerTasksUsage, message)
+ this.updateRunTimeWorkerUsage(workerUsage, message)
+ this.updateEluWorkerUsage(workerUsage, message)
}
- private updateRunTimeTasksUsage (
- workerTasksUsage: TasksUsage,
+ private updateRunTimeWorkerUsage (
+ workerUsage: WorkerUsage,
message: MessageValue<Response>
): void {
- if (this.workerChoiceStrategyContext.getRequiredStatistics().runTime) {
- workerTasksUsage.runTime += message.runTime ?? 0
+ if (
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
+ .aggregate
+ ) {
+ workerUsage.runTime.aggregate += message.taskPerformance?.runTime ?? 0
+ if (
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
+ .average &&
+ workerUsage.tasks.executed !== 0
+ ) {
+ workerUsage.runTime.average =
+ workerUsage.runTime.aggregate / workerUsage.tasks.executed
+ }
+ if (
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
+ .median &&
+ message.taskPerformance?.runTime != null
+ ) {
+ workerUsage.runTime.history.push(message.taskPerformance.runTime)
+ workerUsage.runTime.median = median(workerUsage.runTime.history)
+ }
+ }
+ }
+
+ private updateWaitTimeWorkerUsage (
+ workerUsage: WorkerUsage,
+ task: Task<Data>
+ ): void {
+ const timestamp = performance.now()
+ const taskWaitTime = timestamp - (task.timestamp ?? timestamp)
+ if (
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime
+ .aggregate
+ ) {
+ workerUsage.waitTime.aggregate += taskWaitTime ?? 0
if (
- this.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime &&
- workerTasksUsage.ran !== 0
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .waitTime.average &&
+ workerUsage.tasks.executed !== 0
) {
- workerTasksUsage.avgRunTime =
- workerTasksUsage.runTime / workerTasksUsage.ran
+ workerUsage.waitTime.average =
+ workerUsage.waitTime.aggregate / workerUsage.tasks.executed
}
if (
- this.workerChoiceStrategyContext.getRequiredStatistics().medRunTime &&
- message.runTime != null
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .waitTime.median &&
+ taskWaitTime != null
) {
- workerTasksUsage.runTimeHistory.push(message.runTime)
- workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory)
+ workerUsage.waitTime.history.push(taskWaitTime)
+ workerUsage.waitTime.median = median(workerUsage.waitTime.history)
}
}
}
- private updateWaitTimeTasksUsage (
- workerTasksUsage: TasksUsage,
+ private updateEluWorkerUsage (
+ workerUsage: WorkerUsage,
message: MessageValue<Response>
): void {
- if (this.workerChoiceStrategyContext.getRequiredStatistics().waitTime) {
- workerTasksUsage.waitTime += message.waitTime ?? 0
+ if (
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu
+ .aggregate
+ ) {
+ if (workerUsage.elu != null && message.taskPerformance?.elu != null) {
+ workerUsage.elu.idle.aggregate += message.taskPerformance.elu.idle
+ workerUsage.elu.active.aggregate += message.taskPerformance.elu.active
+ workerUsage.elu.utilization =
+ (workerUsage.elu.utilization +
+ message.taskPerformance.elu.utilization) /
+ 2
+ } else if (message.taskPerformance?.elu != null) {
+ workerUsage.elu.idle.aggregate = message.taskPerformance.elu.idle
+ workerUsage.elu.active.aggregate = message.taskPerformance.elu.active
+ workerUsage.elu.utilization = message.taskPerformance.elu.utilization
+ }
if (
- this.workerChoiceStrategyContext.getRequiredStatistics().avgWaitTime &&
- workerTasksUsage.ran !== 0
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu
+ .average &&
+ workerUsage.tasks.executed !== 0
) {
- workerTasksUsage.avgWaitTime =
- workerTasksUsage.waitTime / workerTasksUsage.ran
+ workerUsage.elu.idle.average =
+ workerUsage.elu.idle.aggregate / workerUsage.tasks.executed
+ workerUsage.elu.active.average =
+ workerUsage.elu.active.aggregate / workerUsage.tasks.executed
}
if (
- this.workerChoiceStrategyContext.getRequiredStatistics().medWaitTime &&
- message.waitTime != null
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu
+ .median &&
+ message.taskPerformance?.elu != null
) {
- workerTasksUsage.waitTimeHistory.push(message.waitTime)
- workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory)
+ workerUsage.elu.idle.history.push(message.taskPerformance.elu.idle)
+ workerUsage.elu.active.history.push(message.taskPerformance.elu.active)
+ workerUsage.elu.idle.median = median(workerUsage.elu.idle.history)
+ workerUsage.elu.active.median = median(workerUsage.elu.active.history)
}
}
}
if (
isKillBehavior(KillBehaviors.HARD, message.kill) ||
(message.kill != null &&
- this.workerNodes[currentWorkerNodeKey].tasksUsage.running === 0)
+ this.workerNodes[currentWorkerNodeKey].workerUsage.tasks
+ .executing === 0)
) {
// Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime)
this.flushTasksQueue(currentWorkerNodeKey)
this.emitter.emit(PoolEvents.error, error)
}
})
- if (this.opts.restartWorkerOnError === true) {
- worker.on('error', () => {
+ worker.on('error', () => {
+ if (this.opts.restartWorkerOnError === true) {
this.createAndSetupWorker()
- })
- }
+ }
+ })
worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)
worker.once('exit', () => {
this.pushWorkerNode(worker)
+ this.setWorkerStatistics(worker)
+
this.afterWorkerSetup(worker)
return worker
// Task execution response received
const promiseResponse = this.promiseResponseMap.get(message.id)
if (promiseResponse != null) {
- if (message.error != null) {
- promiseResponse.reject(message.error)
+ if (message.taskError != null) {
+ promiseResponse.reject(message.taskError.message)
if (this.emitter != null) {
- this.emitter.emit(PoolEvents.taskError, {
- error: message.error,
- errorData: message.errorData
- })
+ this.emitter.emit(PoolEvents.taskError, message.taskError)
}
} else {
promiseResponse.resolve(message.data as Response)
* Sets the given worker node its tasks usage in the pool.
*
* @param workerNode - The worker node.
- * @param tasksUsage - The worker node tasks usage.
+ * @param workerUsage - The worker usage.
*/
private setWorkerNodeTasksUsage (
workerNode: WorkerNode<Worker, Data>,
- tasksUsage: TasksUsage
+ workerUsage: WorkerUsage
): void {
- workerNode.tasksUsage = tasksUsage
+ workerNode.workerUsage = workerUsage
}
/**
private pushWorkerNode (worker: Worker): number {
return this.workerNodes.push({
worker,
- tasksUsage: {
- ran: 0,
- running: 0,
- runTime: 0,
- runTimeHistory: new CircularArray(),
- avgRunTime: 0,
- medRunTime: 0,
- waitTime: 0,
- waitTimeHistory: new CircularArray(),
- avgWaitTime: 0,
- medWaitTime: 0,
- error: 0
- },
+ workerUsage: this.getWorkerUsage(worker),
tasksQueue: new Queue<Task<Data>>()
})
}
- /**
- * Sets the given worker in the pool worker nodes.
- *
- * @param workerNodeKey - The worker node key.
- * @param worker - The worker.
- * @param tasksUsage - The worker tasks usage.
- * @param tasksQueue - The worker task queue.
- */
- private setWorkerNode (
- workerNodeKey: number,
- worker: Worker,
- tasksUsage: TasksUsage,
- tasksQueue: Queue<Task<Data>>
- ): void {
- this.workerNodes[workerNodeKey] = {
- worker,
- tasksUsage,
- tasksQueue
- }
- }
+ // /**
+ // * Sets the given worker in the pool worker nodes.
+ // *
+ // * @param workerNodeKey - The worker node key.
+ // * @param worker - The worker.
+ // * @param workerUsage - The worker usage.
+ // * @param tasksQueue - The worker task queue.
+ // */
+ // private setWorkerNode (
+ // workerNodeKey: number,
+ // worker: Worker,
+ // workerUsage: WorkerUsage,
+ // tasksQueue: Queue<Task<Data>>
+ // ): void {
+ // this.workerNodes[workerNodeKey] = {
+ // worker,
+ // workerUsage,
+ // tasksQueue
+ // }
+ // }
/**
* Removes the given worker from the pool worker nodes.
}
private executeTask (workerNodeKey: number, task: Task<Data>): void {
- this.beforeTaskExecutionHook(workerNodeKey)
+ this.beforeTaskExecutionHook(workerNodeKey, task)
this.sendToWorker(this.workerNodes[workerNodeKey].worker, task)
}
this.flushTasksQueue(workerNodeKey)
}
}
+
+ private setWorkerStatistics (worker: Worker): void {
+ this.sendToWorker(worker, {
+ statistics: {
+ runTime:
+ this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .runTime.aggregate,
+ elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ .elu.aggregate
+ }
+ })
+ }
+
+ private getWorkerUsage (worker: Worker): WorkerUsage {
+ return {
+ tasks: this.getTaskStatistics(worker),
+ runTime: {
+ aggregate: 0,
+ average: 0,
+ median: 0,
+ history: new CircularArray()
+ },
+ waitTime: {
+ aggregate: 0,
+ average: 0,
+ median: 0,
+ history: new CircularArray()
+ },
+ elu: {
+ idle: {
+ aggregate: 0,
+ average: 0,
+ median: 0,
+ history: new CircularArray()
+ },
+ active: {
+ aggregate: 0,
+ average: 0,
+ median: 0,
+ history: new CircularArray()
+ },
+ utilization: 0
+ }
+ }
+ }
+
+ private getTaskStatistics (worker: Worker): TaskStatistics {
+ const queueSize =
+ this.workerNodes[this.getWorkerNodeKey(worker)]?.tasksQueue?.size
+ return {
+ executed: 0,
+ executing: 0,
+ get queued (): number {
+ return queueSize ?? 0
+ },
+ failed: 0
+ }
+ }
}