From b779c0f8db458a7d53e702fbb50ce23a2fe01d39 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 2 Jun 2023 22:04:34 +0200 Subject: [PATCH] feat: expose pool/set information MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/WorkerAbstract.ts | 5 ++++- src/worker/WorkerDynamicPool.ts | 8 ++++++-- src/worker/WorkerSet.ts | 12 ++++++++++++ src/worker/WorkerStaticPool.ts | 8 ++++++-- src/worker/WorkerTypes.ts | 6 ++++++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 4934cff9..d42f629d 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,12 +1,15 @@ import type EventEmitterAsyncResource from 'node:events'; import fs from 'node:fs'; +import type { PoolInfo } from 'poolifier'; + import { WorkerConstants } from './WorkerConstants'; -import type { WorkerData, WorkerOptions } from './WorkerTypes'; +import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; export abstract class WorkerAbstract { protected readonly workerScript: string; protected readonly workerOptions: WorkerOptions; + public abstract readonly info: PoolInfo | SetInfo; public abstract readonly size: number; public abstract readonly maxElementsPerWorker: number | undefined; public abstract readonly emitter: EventEmitterAsyncResource | undefined; diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index b7b39819..38d85aea 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,7 +1,7 @@ import type EventEmitterAsyncResource from 'node:events'; import type { Worker } from 'node:worker_threads'; -import { DynamicThreadPool, type ErrorHandler, type ExitHandler } from 'poolifier'; +import { DynamicThreadPool, type ErrorHandler, type ExitHandler, type PoolInfo } from 'poolifier'; import { WorkerAbstract } from './WorkerAbstract'; import type { WorkerData, WorkerOptions } from './WorkerTypes'; @@ -33,8 +33,12 @@ export class WorkerDynamicPool extends WorkerAbstract { ); } + get info(): PoolInfo { + return this.pool.info; + } + get size(): number { - return this.pool.workerNodes.length; + return this.pool.info.workerNodes; } get maxElementsPerWorker(): number | undefined { diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index ff172d63..b16b9542 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -7,6 +7,7 @@ import { WorkerAbstract } from './WorkerAbstract'; import { WorkerConstants } from './WorkerConstants'; import { type MessageHandler, + type SetInfo, type WorkerData, WorkerMessageEvents, type WorkerOptions, @@ -30,6 +31,17 @@ export class WorkerSet extends WorkerAbstract { this.emitter = new EventEmitterAsyncResource(); } + get info(): SetInfo { + return { + size: this.size, + runningElements: [...this.workerSet].reduce( + (accumulator, workerSetElement) => accumulator + workerSetElement.numberOfWorkerElements, + 0 + ), + elementsPerWorker: this.maxElementsPerWorker, + }; + } + get size(): number { return this.workerSet.size; } diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index af611b63..f5c2a6e8 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,7 +1,7 @@ import type EventEmitterAsyncResource from 'node:events'; import type { Worker } from 'node:worker_threads'; -import { type ErrorHandler, type ExitHandler, FixedThreadPool } from 'poolifier'; +import { type ErrorHandler, type ExitHandler, FixedThreadPool, type PoolInfo } from 'poolifier'; import { WorkerAbstract } from './WorkerAbstract'; import type { WorkerData, WorkerOptions } from './WorkerTypes'; @@ -32,8 +32,12 @@ export class WorkerStaticPool extends WorkerAbstract { ); } + get info(): PoolInfo { + return this.pool.info; + } + get size(): number { - return this.pool.workerNodes.length; + return this.pool.info.workerNodes; } get maxElementsPerWorker(): number | undefined { diff --git a/src/worker/WorkerTypes.ts b/src/worker/WorkerTypes.ts index 1b7cbb30..6f780bce 100644 --- a/src/worker/WorkerTypes.ts +++ b/src/worker/WorkerTypes.ts @@ -8,6 +8,12 @@ export enum WorkerProcessType { staticPool = 'staticPool', } +export type SetInfo = { + size: number; + runningElements: number; + elementsPerWorker: number; +}; + export type MessageHandler = (this: T, message: unknown) => void; export type WorkerOptions = { -- 2.34.1