feat: expose worker type in pool information
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 2 Jun 2023 20:35:04 +0000 (22:35 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 2 Jun 2023 20:35:04 +0000 (22:35 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/index.ts
src/pools/abstract-pool.ts
src/pools/cluster/fixed.ts
src/pools/pool.ts
src/pools/thread/fixed.ts
tests/pools/abstract/abstract-pool.test.js

index fd3cefd29305757d0c474994dedfbeaec2259192..4431b0f98f2617376df1d82937316633267f16de 100644 (file)
@@ -2,7 +2,7 @@ export { DynamicClusterPool } from './pools/cluster/dynamic'
 export { FixedClusterPool } from './pools/cluster/fixed'
 export type { ClusterPoolOptions } from './pools/cluster/fixed'
 export type { AbstractPool } from './pools/abstract-pool'
-export { PoolEvents, PoolTypes } from './pools/pool'
+export { PoolEvents, PoolTypes, WorkerTypes } from './pools/pool'
 export type {
   IPool,
   PoolEmitter,
@@ -10,7 +10,8 @@ export type {
   PoolInfo,
   PoolOptions,
   PoolType,
-  TasksQueueOptions
+  TasksQueueOptions,
+  WorkerType
 } from './pools/pool'
 export type {
   ErrorHandler,
index 58fc28ce12d6a56416b6db05cec3e5186c549a14..55c380b80c5b92de72bb1c4c87e57f4614c56f37 100644 (file)
@@ -17,7 +17,8 @@ import {
   type PoolOptions,
   type PoolType,
   PoolTypes,
-  type TasksQueueOptions
+  type TasksQueueOptions,
+  type WorkerType
 } from './pool'
 import type { IWorker, Task, TasksUsage, WorkerNode } from './worker'
 import {
@@ -217,6 +218,7 @@ export abstract class AbstractPool<
   public get info (): PoolInfo {
     return {
       type: this.type,
+      worker: this.worker,
       minSize: this.minSize,
       maxSize: this.maxSize,
       workerNodes: this.workerNodes.length,
@@ -247,6 +249,11 @@ export abstract class AbstractPool<
     }
   }
 
+  /**
+   * Gets the worker type.
+   */
+  protected abstract get worker (): WorkerType
+
   /**
    * Pool minimum size.
    */
index a0c6b16f6ed7b5a16e38c30c75c62302cd54b9c7..bd5e284496fa82826b51980fdb199c4d4dcbf884 100644 (file)
@@ -2,7 +2,13 @@ import type { ClusterSettings, Worker } from 'node:cluster'
 import cluster from 'node:cluster'
 import type { MessageValue } from '../../utility-types'
 import { AbstractPool } from '../abstract-pool'
-import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
+import {
+  type PoolOptions,
+  type PoolType,
+  PoolTypes,
+  type WorkerType,
+  WorkerTypes
+} from '../pool'
 
 /**
  * Options for a poolifier cluster pool.
@@ -99,6 +105,11 @@ export class FixedClusterPool<
     return PoolTypes.fixed
   }
 
+  /** @inheritDoc */
+  protected get worker (): WorkerType {
+    return WorkerTypes.cluster
+  }
+
   /** @inheritDoc */
   protected get minSize (): number {
     return this.numberOfWorkers
index 0013bf5dc20a27a9b84b63645233d2e636c9df49..f16c54694ab37240e22d47fca1d97fb3d2ec7196 100644 (file)
@@ -31,6 +31,19 @@ export const PoolTypes = Object.freeze({
  */
 export type PoolType = keyof typeof PoolTypes
 
+/**
+ * Enumeration of worker types.
+ */
+export const WorkerTypes = Object.freeze({
+  cluster: 'cluster',
+  thread: 'thread'
+} as const)
+
+/**
+ * Worker type.
+ */
+export type WorkerType = keyof typeof WorkerTypes
+
 /**
  * Pool events emitter.
  */
@@ -56,6 +69,7 @@ export type PoolEvent = keyof typeof PoolEvents
  */
 export interface PoolInfo {
   type: PoolType
+  worker: WorkerType
   minSize: number
   maxSize: number
   workerNodes: number
index 9939a04b9e497f5e78a84396493db7819f837508..cc35f0f971abdb9d1d808e57298a600466c66c73 100644 (file)
@@ -6,7 +6,13 @@ import {
 } from 'node:worker_threads'
 import type { Draft, MessageValue } from '../../utility-types'
 import { AbstractPool } from '../abstract-pool'
-import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
+import {
+  type PoolOptions,
+  type PoolType,
+  PoolTypes,
+  type WorkerType,
+  WorkerTypes
+} from '../pool'
 
 /**
  * A thread worker with message channels for communication between main thread and thread worker.
@@ -95,6 +101,11 @@ export class FixedThreadPool<
     return PoolTypes.fixed
   }
 
+  /** @inheritDoc */
+  protected get worker (): WorkerType {
+    return WorkerTypes.thread
+  }
+
   /** @inheritDoc */
   protected get minSize (): number {
     return this.numberOfWorkers
index b41ab1adc0265986b7f03603a4ca6e8553f5a0f9..80f43dda845c5e4e06b5cc0448b8ef4031f3dad7 100644 (file)
@@ -6,7 +6,8 @@ const {
   FixedThreadPool,
   PoolEvents,
   WorkerChoiceStrategies,
-  PoolTypes
+  PoolTypes,
+  WorkerTypes
 } = require('../../../lib')
 const { CircularArray } = require('../../../lib/circular-array')
 const { Queue } = require('../../../lib/queue')
@@ -282,6 +283,7 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.info).toStrictEqual({
       type: PoolTypes.fixed,
+      worker: WorkerTypes.thread,
       minSize: numberOfWorkers,
       maxSize: numberOfWorkers,
       workerNodes: numberOfWorkers,
@@ -299,6 +301,7 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.info).toStrictEqual({
       type: PoolTypes.dynamic,
+      worker: WorkerTypes.cluster,
       minSize: numberOfWorkers,
       maxSize: numberOfWorkers * 2,
       workerNodes: numberOfWorkers,