import { WorkerAbstract } from './WorkerAbstract';
import type { WorkerData, WorkerOptions } from './WorkerTypes';
-import { WorkerUtils } from './WorkerUtils';
+import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils';
export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
private readonly pool: DynamicThreadPool<WorkerData>;
constructor(workerScript: string, workerOptions?: WorkerOptions) {
super(workerScript, workerOptions);
this.workerOptions.poolOptions.errorHandler = (
- this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler
+ this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler
).bind(this) as ErrorHandler<Worker>;
this.workerOptions.poolOptions.exitHandler = (
- this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler
+ this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler
).bind(this) as ExitHandler<Worker>;
this.workerOptions.poolOptions.messageHandler.bind(this);
this.pool = new DynamicThreadPool<WorkerData>(
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 WorkerUtils.sleep(this.workerOptions.elementStartDelay));
+ this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay));
}
}
type WorkerOptions,
type WorkerSetElement,
} from './WorkerTypes';
-import { WorkerUtils } from './WorkerUtils';
+import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils';
export class WorkerSet extends WorkerAbstract<WorkerData> {
private readonly workerSet: Set<WorkerSetElement>;
++this.getLastWorkerSetElement().numberOfWorkerElements;
// Start element sequentially to optimize memory at startup
if (this.workerOptions.elementStartDelay > 0) {
- await WorkerUtils.sleep(this.workerOptions.elementStartDelay);
+ await sleep(this.workerOptions.elementStartDelay);
}
}
this
) as MessageHandler<Worker>
);
- worker.on('error', WorkerUtils.defaultErrorHandler.bind(this) as (err: Error) => void);
+ worker.on('error', defaultErrorHandler.bind(this) as (err: Error) => void);
worker.on('exit', (code) => {
- WorkerUtils.defaultExitHandler(code);
+ defaultExitHandler(code);
this.workerSet.delete(this.getWorkerSetElementByWorker(worker));
});
this.workerSet.add({ worker, numberOfWorkerElements: 0 });
// Start worker sequentially to optimize memory at startup
- this.workerOptions.workerStartDelay > 0 &&
- (await WorkerUtils.sleep(this.workerOptions.workerStartDelay));
+ this.workerOptions.workerStartDelay > 0 && (await sleep(this.workerOptions.workerStartDelay));
}
private getLastWorkerSetElement(): WorkerSetElement {
import { WorkerAbstract } from './WorkerAbstract';
import type { WorkerData, WorkerOptions } from './WorkerTypes';
-import { WorkerUtils } from './WorkerUtils';
+import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils';
export class WorkerStaticPool extends WorkerAbstract<WorkerData> {
private readonly pool: FixedThreadPool<WorkerData>;
constructor(workerScript: string, workerOptions?: WorkerOptions) {
super(workerScript, workerOptions);
this.workerOptions.poolOptions.errorHandler = (
- this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler
+ this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler
).bind(this) as ErrorHandler<Worker>;
this.workerOptions.poolOptions.exitHandler = (
- this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler
+ this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler
).bind(this) as ExitHandler<Worker>;
this.workerOptions.poolOptions.messageHandler.bind(this);
this.pool = new FixedThreadPool(
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 WorkerUtils.sleep(this.workerOptions.elementStartDelay));
+ this.workerOptions.elementStartDelay > 0 && (await sleep(this.workerOptions.elementStartDelay));
}
}
import chalk from 'chalk';
-export class WorkerUtils {
- private constructor() {
- // This is intentional
- }
+export const sleep = async (milliSeconds: number): Promise<NodeJS.Timeout> => {
+ return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds));
+};
- public static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
- return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds));
+export const defaultExitHandler = (code: number): void => {
+ if (code === 0) {
+ console.info(chalk.green('Worker exited successfully'));
+ } else if (code === 1) {
+ console.info(chalk.green('Worker terminated successfully'));
+ } else if (code > 1) {
+ console.error(chalk.red(`Worker exited with exit code: ${code.toString()}`));
}
+};
- public static defaultExitHandler = (code: number): void => {
- if (code === 0) {
- console.info(chalk.green('Worker exited successfully'));
- } else if (code === 1) {
- console.info(chalk.green('Worker terminated successfully'));
- } else if (code > 1) {
- console.error(chalk.red(`Worker exited with exit code: ${code.toString()}`));
- }
- };
-
- public static defaultErrorHandler = (error: Error): void => {
- console.error(chalk.red('Worker errored: ', error));
- };
-}
+export const defaultErrorHandler = (error: Error): void => {
+ console.error(chalk.red('Worker errored: ', error));
+};