-import { FixedThreadPool } from 'poolifier';
+import { FixedThreadPool, type PoolEmitter, 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';
+import type { WorkerData, WorkerOptions } from './WorkerTypes';
+import { sleep } from './WorkerUtils';
-export default class WorkerStaticPool extends WorkerAbstract<WorkerData> {
+export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
private readonly pool: FixedThreadPool<WorkerData>;
/**
- * Create a new `WorkerStaticPool`.
+ * Creates a new `WorkerStaticPool`.
*
- * @param workerScript
- * @param workerOptions
+ * @param workerScript -
+ * @param workerOptions -
*/
- constructor(workerScript: string, workerOptions?: WorkerOptions) {
+ constructor(workerScript: string, workerOptions: WorkerOptions) {
super(workerScript, workerOptions);
- this.workerOptions.poolOptions.exitHandler =
- this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler;
this.pool = new FixedThreadPool(
this.workerOptions.poolMaxSize,
this.workerScript,
- this.workerOptions.poolOptions
+ this.workerOptions.poolOptions,
);
}
+ get info(): PoolInfo {
+ return this.pool.info;
+ }
+
get size(): number {
- return this.pool.workers.length;
+ return this.pool.info.workerNodes;
}
- get maxElementsPerWorker(): number | null {
- return null;
+ get maxElementsPerWorker(): number | undefined {
+ return undefined;
}
- /**
- *
- * @returns
- * @public
- */
+ get emitter(): PoolEmitter | undefined {
+ return this.pool?.emitter;
+ }
+
+ /** @inheritDoc */
public async start(): Promise<void> {
// This is intentional
}
- /**
- *
- * @returns
- * @public
- */
+ /** @inheritDoc */
public async stop(): Promise<void> {
return this.pool.destroy();
}
- /**
- *
- * @param elementData
- * @returns
- * @public
- */
+ /** @inheritDoc */
public async addElement(elementData: WorkerData): Promise<void> {
await this.pool.execute(elementData);
// Start element sequentially to optimize memory at startup
- this.workerOptions.elementStartDelay > 0 &&
- (await Utils.sleep(this.workerOptions.elementStartDelay));
+ this.workerOptions.elementStartDelay! > 0 &&
+ (await sleep(this.workerOptions.elementStartDelay!));
}
}