refactor: improve task error message
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Jul 2023 11:00:10 +0000 (13:00 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Jul 2023 11:00:10 +0000 (13:00 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/pools/pool.ts
src/pools/selection-strategies/selection-strategies-types.ts
src/pools/worker.ts
src/utility-types.ts
tests/pools/cluster/fixed.test.js
tests/pools/thread/fixed.test.js

index 889390a73c6b4f557afa8c111fab059f69a50737..f71bf2008c6fa955aea588e7b2c5ab905e690f47 100644 (file)
@@ -833,7 +833,7 @@ export abstract class AbstractPool<
               this.emitter.emit(PoolEvents.taskError, message.taskError)
             }
             promiseResponse.reject(
-              `${message.taskError.message} on worker '${message.taskError.workerId}`
+              `${message.taskError.message} on worker '${message.taskError.workerId}'`
             )
           } else {
             promiseResponse.resolve(message.data as Response)
index 87f4a8fa03f781aab2a17926fd98ab088387bf1b..367b45291853f0ca87c3b52ddf2621ae3465c3f4 100644 (file)
@@ -96,7 +96,7 @@ export interface TasksQueueOptions {
    *
    * @defaultValue 1
    */
-  concurrency?: number
+  readonly concurrency?: number
 }
 
 /**
index dc1748140a8a3db121b4f904ccafda718a14411e..9f68c22f892c50a3e424b59a3cb0cffe9f61557f 100644 (file)
@@ -62,7 +62,7 @@ export interface MeasurementOptions {
   /**
    * Set measurement median.
    */
-  median: boolean
+  readonly median: boolean
 }
 
 /**
@@ -72,32 +72,32 @@ export interface WorkerChoiceStrategyOptions {
   /**
    * Measurement to use for worker choice strategy.
    */
-  measurement?: Measurement
+  readonly measurement?: Measurement
   /**
    * Runtime options.
    *
    * @defaultValue \{ median: false \}
    */
-  runTime?: MeasurementOptions
+  readonly runTime?: MeasurementOptions
   /**
    * Wait time options.
    *
    * @defaultValue \{ median: false \}
    */
-  waitTime?: MeasurementOptions
+  readonly waitTime?: MeasurementOptions
   /**
    * Event loop utilization options.
    *
    * @defaultValue \{ median: false \}
    */
-  elu?: MeasurementOptions
+  readonly elu?: MeasurementOptions
   /**
    * Worker weights to use for weighted round robin worker selection strategy.
    * Weight is the tasks maximum average or median runtime in milliseconds.
    *
    * @defaultValue Computed worker weights automatically given the CPU performance.
    */
-  weights?: Record<number, number>
+  readonly weights?: Record<number, number>
 }
 
 /**
@@ -129,15 +129,15 @@ export interface TaskStatisticsRequirements {
   /**
    * Tasks runtime requirements.
    */
-  runTime: MeasurementStatisticsRequirements
+  readonly runTime: MeasurementStatisticsRequirements
   /**
    * Tasks wait time requirements.
    */
-  waitTime: MeasurementStatisticsRequirements
+  readonly waitTime: MeasurementStatisticsRequirements
   /**
    * Tasks event loop utilization requirements.
    */
-  elu: MeasurementStatisticsRequirements
+  readonly elu: MeasurementStatisticsRequirements
 }
 
 /**
@@ -149,7 +149,7 @@ export interface StrategyPolicy {
   /**
    * Expects direct usage of dynamic worker.
    */
-  useDynamicWorker: boolean
+  readonly useDynamicWorker: boolean
 }
 
 /**
index 664b6a7695ef42a088732812a7eac270e9f87a00..379d350b9c095b69304bd591e87b0bf4457b74e2 100644 (file)
@@ -76,7 +76,7 @@ export interface MeasurementStatistics {
   /**
    * Measurement history.
    */
-  history: CircularArray<number>
+  readonly history: CircularArray<number>
 }
 
 /**
@@ -85,8 +85,8 @@ export interface MeasurementStatistics {
  * @internal
  */
 export interface EventLoopUtilizationMeasurementStatistics {
-  idle: MeasurementStatistics
-  active: MeasurementStatistics
+  readonly idle: MeasurementStatistics
+  readonly active: MeasurementStatistics
   utilization: number
 }
 
@@ -127,7 +127,7 @@ export interface WorkerInfo {
   /**
    * Worker id.
    */
-  id: number | undefined
+  readonly id: number | undefined
   /**
    * Started flag.
    */
@@ -143,19 +143,19 @@ export interface WorkerUsage {
   /**
    * Tasks statistics.
    */
-  tasks: TaskStatistics
+  readonly tasks: TaskStatistics
   /**
    * Tasks runtime statistics.
    */
-  runTime: MeasurementStatistics
+  readonly runTime: MeasurementStatistics
   /**
    * Tasks wait time statistics.
    */
-  waitTime: MeasurementStatistics
+  readonly waitTime: MeasurementStatistics
   /**
    * Tasks event loop utilization statistics.
    */
-  elu: EventLoopUtilizationMeasurementStatistics
+  readonly elu: EventLoopUtilizationMeasurementStatistics
 }
 
 /**
@@ -201,7 +201,7 @@ export interface WorkerNode<Worker extends IWorker, Data = unknown> {
   /**
    * Worker node worker info.
    */
-  info: WorkerInfo
+  readonly info: WorkerInfo
   /**
    * Worker node worker usage statistics.
    */
index bca2de2b91eec91a2c1113e5b422f5cc03301cba..a2f3de1c293475ba8089c91a5715508c4e9d52cb 100644 (file)
@@ -11,15 +11,15 @@ export interface TaskError<Data = unknown> {
   /**
    * Worker id.
    */
-  workerId: number
+  readonly workerId: number
   /**
    * Error message.
    */
-  message: string
+  readonly message: string
   /**
    * Data passed to the worker triggering the error.
    */
-  data?: Data
+  readonly data?: Data
 }
 
 /**
@@ -29,15 +29,15 @@ export interface TaskPerformance {
   /**
    * Task performance timestamp.
    */
-  timestamp: number
+  readonly timestamp: number
   /**
    * Task runtime.
    */
-  runTime?: number
+  readonly runTime?: number
   /**
    * Task event loop utilization.
    */
-  elu?: EventLoopUtilization
+  readonly elu?: EventLoopUtilization
 }
 
 /**
index 631c59f02d177b739c85bf11f2ae219087353f43..60dac4017c72781b6b1e33a78169910438e3030a 100644 (file)
@@ -145,11 +145,8 @@ describe('Fixed cluster pool test suite', () => {
     }
     expect(inError).toBeDefined()
     expect(typeof inError === 'string').toBe(true)
-    expect(inError).toBe('Error Message from ClusterWorker')
-    expect(taskError).toStrictEqual({
-      message: 'Error Message from ClusterWorker',
-      data
-    })
+    expect(inError).toContain('Error Message from ClusterWorker on worker')
+    expect(taskError.data).toStrictEqual(data)
     expect(
       errorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1
@@ -171,11 +168,10 @@ describe('Fixed cluster pool test suite', () => {
     }
     expect(inError).toBeDefined()
     expect(typeof inError === 'string').toBe(true)
-    expect(inError).toBe('Error Message from ClusterWorker:async')
-    expect(taskError).toStrictEqual({
-      message: 'Error Message from ClusterWorker:async',
-      data
-    })
+    expect(inError).toContain(
+      'Error Message from ClusterWorker:async on worker'
+    )
+    expect(taskError.data).toStrictEqual(data)
     expect(
       asyncErrorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1
index 8a0cfeadcbd4660b63e604d5214674d4328f8adb..f044895335af397c1599faeee10f6e90d63dcf42 100644 (file)
@@ -147,11 +147,10 @@ describe('Fixed thread pool test suite', () => {
     expect(inError).toBeInstanceOf(Error)
     expect(inError.message).toBeDefined()
     expect(typeof inError.message === 'string').toBe(true)
-    expect(inError.message).toBe('Error Message from ThreadWorker')
-    expect(taskError).toStrictEqual({
-      message: new Error('Error Message from ThreadWorker'),
-      data
-    })
+    expect(inError.message).toContain(
+      'Error Message from ThreadWorker on worker'
+    )
+    expect(taskError.data).toStrictEqual(data)
     expect(
       errorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1
@@ -175,11 +174,10 @@ describe('Fixed thread pool test suite', () => {
     expect(inError).toBeInstanceOf(Error)
     expect(inError.message).toBeDefined()
     expect(typeof inError.message === 'string').toBe(true)
-    expect(inError.message).toBe('Error Message from ThreadWorker:async')
-    expect(taskError).toStrictEqual({
-      message: new Error('Error Message from ThreadWorker:async'),
-      data
-    })
+    expect(inError.message).toContain(
+      'Error Message from ThreadWorker:async on worker'
+    )
+    expect(taskError.data).toStrictEqual(data)
     expect(
       asyncErrorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1