-import { FixedThreadPool, FixedThreadPoolOptions } from 'poolifier';
+import { FixedThreadPool, PoolOptions } from 'poolifier';
import Constants from '../utils/Constants';
import Utils from '../utils/Utils';
+import { Worker } from 'worker_threads';
+import WorkerAbstract from './WorkerAbstract';
import { WorkerData } from '../types/Worker';
-import Wrk from './Wrk';
-import { threadId } from 'worker_threads';
-export default class WorkerStaticPool extends Wrk {
+export default class WorkerStaticPool<T> extends WorkerAbstract {
private pool: StaticPool;
/**
*
* @param {string} workerScript
*/
- constructor(workerScript: string, numThreads: number) {
+ constructor(workerScript: string, numberOfThreads: number) {
super(workerScript);
- this.pool = StaticPool.getInstance(numThreads, this.workerScript);
+ this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript);
}
get size(): number {
}
get maxElementsPerWorker(): number {
- return 1;
+ return null;
}
/**
* @return {Promise<void>}
* @public
*/
- public async addElement(elementData: WorkerData): Promise<void> {
+ public async stop(): Promise<void> {
+ return this.pool.destroy();
+ }
+
+ /**
+ *
+ * @return {Promise<void>}
+ * @public
+ */
+ public async addElement(elementData: T): Promise<void> {
await this.pool.execute(elementData);
// Start worker sequentially to optimize memory at startup
await Utils.sleep(Constants.START_WORKER_DELAY);
class StaticPool extends FixedThreadPool<WorkerData> {
private static instance: StaticPool;
- private constructor(numThreads: number, workerScript: string, opts?: FixedThreadPoolOptions) {
- super(numThreads, workerScript, opts);
+ private constructor(numberOfThreads: number, workerScript: string, opts?: PoolOptions<Worker>) {
+ super(numberOfThreads, workerScript, opts);
}
- public static getInstance(numThreads: number, workerScript: string): StaticPool {
+ public static getInstance(numberOfThreads: number, workerScript: string): StaticPool {
if (!StaticPool.instance) {
- StaticPool.instance = new StaticPool(numThreads, workerScript,
+ StaticPool.instance = new StaticPool(numberOfThreads, workerScript,
{
exitHandler: (code) => {
if (code !== 0) {
- console.error(`Worker ${threadId} stopped with exit code ${code}`);
+ console.error(`Worker stopped with exit code ${code}`);
}
}
}