-import type { Worker } from 'worker_threads';
+import type { EventEmitterAsyncResource } from 'node:events'
-import { DynamicThreadPool, type ErrorHandler, type ExitHandler } from 'poolifier';
+import { DynamicThreadPool, type PoolInfo } from 'poolifier'
-import type { WorkerData, WorkerOptions } from '../types/Worker';
-import Utils from '../utils/Utils';
-import WorkerAbstract from './WorkerAbstract';
-import { WorkerUtils } from './WorkerUtils';
+import { WorkerAbstract } from './WorkerAbstract.js'
+import type { WorkerData, WorkerOptions } from './WorkerTypes.js'
+import { randomizeDelay, sleep } from './WorkerUtils.js'
-export default class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
- private readonly pool: DynamicThreadPool<WorkerData>;
+export class WorkerDynamicPool<D extends WorkerData, R extends WorkerData> extends WorkerAbstract<
+D,
+R
+> {
+ private readonly pool: DynamicThreadPool<D, R>
/**
- * Create a new `WorkerDynamicPool`.
+ * Creates a new `WorkerDynamicPool`.
*
* @param workerScript -
* @param workerOptions -
*/
- constructor(workerScript: string, workerOptions?: WorkerOptions) {
- super(workerScript, workerOptions);
- this.workerOptions.poolOptions.errorHandler = (
- this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler
- ).bind(this) as ErrorHandler<Worker>;
- this.workerOptions.poolOptions.exitHandler = (
- this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler
- ).bind(this) as ExitHandler<Worker>;
- this.workerOptions.poolOptions.messageHandler.bind(this);
- this.pool = new DynamicThreadPool<WorkerData>(
+ constructor (workerScript: string, workerOptions: WorkerOptions) {
+ super(workerScript, workerOptions)
+ this.pool = new DynamicThreadPool<D, R>(
this.workerOptions.poolMinSize,
this.workerOptions.poolMaxSize,
this.workerScript,
this.workerOptions.poolOptions
- );
+ )
}
- get size(): number {
- return this.pool.workers.length;
+ get info (): PoolInfo {
+ return this.pool.info
}
- get maxElementsPerWorker(): number | null {
- return null;
+ get size (): number {
+ return this.pool.info.workerNodes
}
- /**
- *
- * @returns
- * @public
- */
- public async start(): Promise<void> {
- // This is intentional
+ get maxElementsPerWorker (): number | undefined {
+ return undefined
}
- /**
- *
- * @returns
- * @public
- */
- public async stop(): Promise<void> {
- return this.pool.destroy();
+ get emitter (): EventEmitterAsyncResource | undefined {
+ return this.pool.emitter
}
- /**
- *
- * @param elementData -
- * @returns
- * @public
- */
- public async addElement(elementData: WorkerData): Promise<void> {
- await this.pool.execute(elementData);
+ /** @inheritDoc */
+ public start (): void {
+ this.pool.start()
+ }
+
+ /** @inheritDoc */
+ public async stop (): Promise<void> {
+ await this.pool.destroy()
+ }
+
+ /** @inheritDoc */
+ public async addElement (elementData: D): Promise<R> {
+ const response = await this.pool.execute(elementData)
// Start element sequentially to optimize memory at startup
- this.workerOptions.elementStartDelay > 0 &&
- (await Utils.sleep(this.workerOptions.elementStartDelay));
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.workerOptions.elementAddDelay! > 0 &&
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ (await sleep(randomizeDelay(this.workerOptions.elementAddDelay!)))
+ return response
}
}