From: Jérôme Benoit Date: Fri, 2 Jun 2023 20:35:04 +0000 (+0200) Subject: feat: expose worker type in pool information X-Git-Tag: v2.5.3~17 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=184855e69fea29f1018024a34be10de2c8e3141a;p=poolifier.git feat: expose worker type in pool information Signed-off-by: Jérôme Benoit --- diff --git a/src/index.ts b/src/index.ts index fd3cefd2..4431b0f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 58fc28ce..55c380b8 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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. */ diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index a0c6b16f..bd5e2844 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -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 diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 0013bf5d..f16c5469 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -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 diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 9939a04b..cc35f0f9 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -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 diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index b41ab1ad..80f43dda 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -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,