import { performance } from 'node:perf_hooks'
import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
import {
+ DEFAULT_TASK_NAME,
DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
EMPTY_FUNCTION,
isKillBehavior,
const timestamp = performance.now()
const workerNodeKey = this.chooseWorkerNode()
const submittedTask: Task<Data> = {
- name,
+ name: name ?? DEFAULT_TASK_NAME,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
data: data ?? ({} as Data),
timestamp,
const workerUsage = this.workerNodes[workerNodeKey].usage
++workerUsage.tasks.executing
this.updateWaitTimeWorkerUsage(workerUsage, task)
+ const tasksWorkerUsage = this.workerNodes[
+ workerNodeKey
+ ].getTasksWorkerUsage(task.name as string) as WorkerUsage
+ ++tasksWorkerUsage.tasks.executing
+ this.updateWaitTimeWorkerUsage(tasksWorkerUsage, task)
}
/**
worker: Worker,
message: MessageValue<Response>
): void {
- const workerUsage = this.workerNodes[this.getWorkerNodeKey(worker)].usage
+ const workerNodeKey = this.getWorkerNodeKey(worker)
+ const workerUsage = this.workerNodes[workerNodeKey].usage
this.updateTaskStatisticsWorkerUsage(workerUsage, message)
this.updateRunTimeWorkerUsage(workerUsage, message)
this.updateEluWorkerUsage(workerUsage, message)
+ const tasksWorkerUsage = this.workerNodes[
+ workerNodeKey
+ ].getTasksWorkerUsage(message.name as string) as WorkerUsage
+ this.updateTaskStatisticsWorkerUsage(tasksWorkerUsage, message)
+ this.updateRunTimeWorkerUsage(tasksWorkerUsage, message)
+ this.updateEluWorkerUsage(tasksWorkerUsage, message)
}
private updateTaskStatisticsWorkerUsage (
public readonly worker: Worker
public readonly info: WorkerInfo
public usage: WorkerUsage
+ private readonly tasksUsage: Map<string, WorkerUsage>
private readonly tasksQueue: Queue<Task<Data>>
/**
this.worker = worker
this.info = this.initWorkerInfo(worker, workerType)
this.usage = this.initWorkerUsage()
+ this.tasksUsage = new Map<string, WorkerUsage>()
this.tasksQueue = new Queue<Task<Data>>()
}
this.tasksQueue.clear()
}
+ /** @inheritdoc */
public resetUsage (): void {
this.usage = this.initWorkerUsage()
+ this.tasksUsage.clear()
+ }
+
+ /** @inheritdoc */
+ public getTasksWorkerUsage (name: string): WorkerUsage | undefined {
+ if (!this.tasksUsage.has(name)) {
+ this.tasksUsage.set(name, this.initWorkerUsage())
+ }
+ return this.tasksUsage.get(name)
}
private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo {
* Worker node reset usage statistics .
*/
readonly resetUsage: () => void
+ /**
+ * Worker node get tasks usage statistics.
+ */
+ readonly getTasksWorkerUsage: (name: string) => WorkerUsage | undefined
}
* @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
*/
export interface TaskError<Data = unknown> {
+ /**
+ * Task name triggering the error.
+ */
+ readonly name: string
/**
* Error message.
*/
readonly message: string
/**
- * Data passed to the worker triggering the error.
+ * Data triggering the error.
*/
readonly data?: Data
}
} from './pools/selection-strategies/selection-strategies-types'
import type { KillBehavior } from './worker/worker-options'
+/**
+ * Default task name.
+ */
+export const DEFAULT_TASK_NAME = 'default'
+
/**
* An intentional empty function.
*/
TaskPerformance,
WorkerStatistics
} from '../utility-types'
-import { EMPTY_FUNCTION, isAsyncFunction, isPlainObject } from '../utils'
+import {
+ DEFAULT_TASK_NAME,
+ EMPTY_FUNCTION,
+ isAsyncFunction,
+ isPlainObject
+} from '../utils'
import {
type KillBehavior,
KillBehaviors,
WorkerSyncFunction
} from './worker-functions'
-const DEFAULT_FUNCTION_NAME = 'default'
const DEFAULT_MAX_INACTIVE_TIME = 60000
const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
}
this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
if (typeof taskFunctions === 'function') {
- this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this))
+ this.taskFunctions.set(DEFAULT_TASK_NAME, taskFunctions.bind(this))
} else if (isPlainObject(taskFunctions)) {
let firstEntry = true
for (const [name, fn] of Object.entries(taskFunctions)) {
}
this.taskFunctions.set(name, fn.bind(this))
if (firstEntry) {
- this.taskFunctions.set(DEFAULT_FUNCTION_NAME, fn.bind(this))
+ this.taskFunctions.set(DEFAULT_TASK_NAME, fn.bind(this))
firstEntry = false
}
}
const errorMessage = this.handleError(e as Error | string)
this.sendToMainWorker({
taskError: {
+ name: message.name ?? DEFAULT_TASK_NAME,
message: errorMessage,
data: message.data
},
const errorMessage = this.handleError(e as Error | string)
this.sendToMainWorker({
taskError: {
+ name: message.name ?? DEFAULT_TASK_NAME,
message: errorMessage,
data: message.data
},
/**
* Gets the task function in the given scope.
*
- * @param name - Name of the function that will be returned.
+ * @param name - Name of the task function that will be returned.
*/
private getTaskFunction (name?: string): WorkerFunction<Data, Response> {
- name = name ?? DEFAULT_FUNCTION_NAME
+ name = name ?? DEFAULT_TASK_NAME
const fn = this.taskFunctions.get(name)
if (fn == null) {
throw new Error(`Task function '${name}' not found`)
private beginTaskPerformance (name?: string): TaskPerformance {
this.checkStatistics()
return {
- name: name ?? DEFAULT_FUNCTION_NAME,
+ name: name ?? DEFAULT_TASK_NAME,
timestamp: performance.now(),
...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
}
expect(typeof inError === 'string').toBe(true)
expect(inError).toBe('Error Message from ClusterWorker')
expect(taskError).toStrictEqual({
+ name: 'default',
message: 'Error Message from ClusterWorker',
data
})
expect(typeof inError === 'string').toBe(true)
expect(inError).toBe('Error Message from ClusterWorker:async')
expect(taskError).toStrictEqual({
+ name: 'default',
message: 'Error Message from ClusterWorker:async',
data
})
expect(typeof inError.message === 'string').toBe(true)
expect(inError.message).toBe('Error Message from ThreadWorker')
expect(taskError).toStrictEqual({
+ name: 'default',
message: new Error('Error Message from ThreadWorker'),
data
})
expect(typeof inError.message === 'string').toBe(true)
expect(inError.message).toBe('Error Message from ThreadWorker:async')
expect(taskError).toStrictEqual({
+ name: 'default',
message: new Error('Error Message from ThreadWorker:async'),
data
})