this.workerNodes[this.getWorkerNodeKey(worker)].tasksUsage
--workerTasksUsage.running
++workerTasksUsage.ran
- if (message.error != null) {
+ if (message.taskError != null) {
++workerTasksUsage.error
}
this.updateRunTimeTasksUsage(workerTasksUsage, message)
// 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)
*
* @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
*/
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- env?: any
+ env?: Record<string, unknown>
/**
* Cluster settings.
*
*/
export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
+export interface TaskError<Data = unknown> {
+ /**
+ * Error message.
+ */
+ message: string
+ /**
+ * Data passed to the worker triggering the error.
+ */
+ data?: Data
+}
+
/**
* Task performance.
*/
*/
export interface MessageValue<
Data = unknown,
+ ErrorData = unknown,
MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
> extends Task<Data> {
/**
/**
* Task error.
*/
- readonly error?: string
- /**
- * Task data triggering task error.
- */
- readonly errorData?: unknown
+ readonly taskError?: TaskError<ErrorData>
/**
* Task performance.
*/
*
* @param message - Message received.
*/
- protected messageListener (message: MessageValue<Data, MainWorker>): void {
+ protected messageListener (
+ message: MessageValue<Data, Data, MainWorker>
+ ): void {
if (message.id != null && message.data != null) {
// Task message received
const fn = this.getTaskFunction(message.name)
*
* @param message - The response message.
*/
- protected abstract sendToMainWorker (message: MessageValue<Response>): void
+ protected abstract sendToMainWorker (
+ message: MessageValue<Response, Data>
+ ): void
/**
* Checks if the worker should be terminated, because its living too long.
} catch (e) {
const err = this.handleError(e as Error)
this.sendToMainWorker({
- error: err,
- errorData: message.data,
+ taskError: {
+ message: err,
+ data: message.data
+ },
id: message.id
})
} finally {
.catch(e => {
const err = this.handleError(e as Error)
this.sendToMainWorker({
- error: err,
- errorData: message.data,
+ taskError: {
+ message: err,
+ data: message.data
+ },
id: message.id
})
})
expect(typeof inError === 'string').toBe(true)
expect(inError).toBe('Error Message from ClusterWorker')
expect(taskError).toStrictEqual({
- error: 'Error Message from ClusterWorker',
- errorData: data
+ message: 'Error Message from ClusterWorker',
+ data
})
expect(
errorPool.workerNodes.some(
expect(typeof inError === 'string').toBe(true)
expect(inError).toBe('Error Message from ClusterWorker:async')
expect(taskError).toStrictEqual({
- error: 'Error Message from ClusterWorker:async',
- errorData: data
+ message: 'Error Message from ClusterWorker:async',
+ data
})
expect(
asyncErrorPool.workerNodes.some(
expect(typeof inError.message === 'string').toBe(true)
expect(inError.message).toBe('Error Message from ThreadWorker')
expect(taskError).toStrictEqual({
- error: new Error('Error Message from ThreadWorker'),
- errorData: data
+ message: new Error('Error Message from ThreadWorker'),
+ data
})
expect(
errorPool.workerNodes.some(
expect(typeof inError.message === 'string').toBe(true)
expect(inError.message).toBe('Error Message from ThreadWorker:async')
expect(taskError).toStrictEqual({
- error: new Error('Error Message from ThreadWorker:async'),
- errorData: data
+ message: new Error('Error Message from ThreadWorker:async'),
+ data
})
expect(
asyncErrorPool.workerNodes.some(