import { EventEmitter } from 'node:events';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import { type Worker, isMainThread } from 'node:worker_threads';
+import { isMainThread } from 'node:worker_threads';
import chalk from 'chalk';
handleUnhandledRejection,
logger,
} from '../utils';
-import { type MessageHandler, type WorkerAbstract, WorkerFactory } from '../worker';
+import { type WorkerAbstract, WorkerFactory } from '../worker';
const moduleName = 'Bootstrap';
elementsPerWorker: Configuration.getWorker().elementsPerWorker,
poolOptions: {
workerChoiceStrategy: Configuration.getWorker().poolStrategy,
+ messageHandler: this.messageHandler.bind(this) as (message: unknown) => void,
},
- messageHandler: this.messageHandler.bind(this) as MessageHandler<Worker>,
}
));
}
import type EventEmitterAsyncResource from 'node:events';
import fs from 'node:fs';
+import type { Worker } from 'node:worker_threads';
-import type { PoolInfo } from 'poolifier';
+import type { ErrorHandler, ExitHandler, PoolInfo } from 'poolifier';
import { WorkerConstants } from './WorkerConstants';
import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes';
+import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils';
export abstract class WorkerAbstract<T extends WorkerData> {
protected readonly workerScript: string;
poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
poolOptions: {},
- messageHandler: WorkerConstants.EMPTY_FUNCTION,
}
) {
if (workerScript === null || workerScript === undefined) {
}
this.workerScript = workerScript;
this.workerOptions = workerOptions;
+ this.workerOptions.poolOptions?.messageHandler?.bind(this);
+ this.workerOptions.poolOptions.errorHandler = (
+ this.workerOptions?.poolOptions?.errorHandler ?? defaultErrorHandler
+ ).bind(this) as ErrorHandler<Worker>;
+ this.workerOptions.poolOptions?.onlineHandler?.bind(this);
+ this.workerOptions.poolOptions.exitHandler = (
+ this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler
+ ).bind(this) as ExitHandler<Worker>;
}
/**
import type EventEmitterAsyncResource from 'node:events';
-import type { Worker } from 'node:worker_threads';
-import { DynamicThreadPool, type ErrorHandler, type ExitHandler, type PoolInfo } from 'poolifier';
+import { DynamicThreadPool, type PoolInfo } from 'poolifier';
import { WorkerAbstract } from './WorkerAbstract';
import type { WorkerData, WorkerOptions } from './WorkerTypes';
-import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils';
+import { 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 ?? defaultErrorHandler
- ).bind(this) as ErrorHandler<Worker>;
- this.workerOptions.poolOptions.exitHandler = (
- this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler
- ).bind(this) as ExitHandler<Worker>;
- this.workerOptions.poolOptions.messageHandler.bind(this);
this.pool = new DynamicThreadPool<WorkerData>(
this.workerOptions.poolMinSize,
this.workerOptions.poolMaxSize,
-import { type Worker, isMainThread } from 'node:worker_threads';
+import { isMainThread } from 'node:worker_threads';
-import type { PoolOptions } from 'poolifier';
+import type { ThreadPoolOptions } from 'poolifier';
import type { WorkerAbstract } from './WorkerAbstract';
import { WorkerConstants } from './WorkerConstants';
workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY;
workerOptions.elementStartDelay =
workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY;
- workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions<Worker>);
- workerOptions?.messageHandler &&
- (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler);
+ workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as ThreadPoolOptions);
let workerImplementation: WorkerAbstract<T> | null = null;
switch (workerProcessType) {
case WorkerProcessType.workerSet:
// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
import EventEmitterAsyncResource from 'node:events';
-import { Worker } from 'node:worker_threads';
+import { SHARE_ENV, Worker } from 'node:worker_threads';
import { WorkerAbstract } from './WorkerAbstract';
import { WorkerConstants } from './WorkerConstants';
import {
- type MessageHandler,
type SetInfo,
type WorkerData,
WorkerMessageEvents,
type WorkerSetElement,
WorkerSetEvents,
} from './WorkerTypes';
-import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils';
+import { sleep } from './WorkerUtils';
export class WorkerSet extends WorkerAbstract<WorkerData> {
public readonly emitter: EventEmitterAsyncResource;
* Add a new `WorkerSetElement`.
*/
private addWorkerSetElement(): WorkerSetElement {
- const worker = new Worker(this.workerScript);
+ const worker = new Worker(this.workerScript, {
+ env: SHARE_ENV,
+ ...this.workerOptions.poolOptions.workerOptions,
+ });
worker.on(
'message',
- (this.workerOptions?.messageHandler ?? WorkerConstants.EMPTY_FUNCTION).bind(
- this
- ) as MessageHandler<Worker>
+ this.workerOptions?.poolOptions?.messageHandler ?? WorkerConstants.EMPTY_FUNCTION
+ );
+ worker.on(
+ 'error',
+ this.workerOptions?.poolOptions?.errorHandler ?? WorkerConstants.EMPTY_FUNCTION
);
- worker.on('error', defaultErrorHandler.bind(this) as (err: Error) => void);
worker.on('error', (error) => {
this.emitter.emit(WorkerSetEvents.error, error);
this.addWorkerSetElement();
});
- worker.on('exit', defaultExitHandler.bind(this) as (exitCode: number) => void);
+ worker.on(
+ 'online',
+ this.workerOptions?.poolOptions?.onlineHandler ?? WorkerConstants.EMPTY_FUNCTION
+ );
+ worker.on(
+ 'exit',
+ this.workerOptions?.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION
+ );
worker.on('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker)));
const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 };
this.workerSet.add(workerSetElement);
import type EventEmitterAsyncResource from 'node:events';
-import type { Worker } from 'node:worker_threads';
-import { type ErrorHandler, type ExitHandler, FixedThreadPool, type PoolInfo } from 'poolifier';
+import { FixedThreadPool, type PoolInfo } from 'poolifier';
import { WorkerAbstract } from './WorkerAbstract';
import type { WorkerData, WorkerOptions } from './WorkerTypes';
-import { defaultErrorHandler, defaultExitHandler, sleep } from './WorkerUtils';
+import { 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 ?? defaultErrorHandler
- ).bind(this) as ErrorHandler<Worker>;
- this.workerOptions.poolOptions.exitHandler = (
- this.workerOptions?.poolOptions?.exitHandler ?? defaultExitHandler
- ).bind(this) as ExitHandler<Worker>;
- this.workerOptions.poolOptions.messageHandler.bind(this);
this.pool = new FixedThreadPool(
this.workerOptions.poolMaxSize,
this.workerScript,
import type { Worker } from 'node:worker_threads';
-import { type PoolEvent, PoolEvents, type PoolOptions } from 'poolifier';
+import { type PoolEvent, PoolEvents, type ThreadPoolOptions } from 'poolifier';
export enum WorkerProcessType {
workerSet = 'workerSet',
} as const;
export type WorkerEvents = PoolEvent | WorkerSetEvents;
-export type MessageHandler<T> = (this: T, message: unknown) => void;
-
export type WorkerOptions = {
workerStartDelay?: number;
elementStartDelay?: number;
poolMaxSize: number;
poolMinSize: number;
elementsPerWorker?: number;
- poolOptions?: PoolOptions<Worker>;
- messageHandler?: MessageHandler<Worker>;
+ poolOptions?: ThreadPoolOptions;
};
export type WorkerData = Record<string, unknown>;
export { WorkerConstants } from './WorkerConstants';
export { WorkerFactory } from './WorkerFactory';
export {
- type MessageHandler,
type WorkerData,
WorkerEvents,
type WorkerMessage,