*/
constructor(workerScript: string, workerOptions?: WorkerOptions) {
super(workerScript, workerOptions);
+ this.workerOptions.poolOptions = {
+ ...{
+ enableEvents: true,
+ restartWorkerOnError: true,
+ },
+ ...this.workerOptions.poolOptions,
+ };
this.workerSet = new Set<WorkerSetElement>();
- this.emitter = new EventEmitterAsyncResource();
+ if (this.workerOptions?.poolOptions?.enableEvents) {
+ this.emitter = new EventEmitterAsyncResource();
+ }
}
get info(): SetInfo {
this.workerOptions?.poolOptions?.errorHandler ?? WorkerConstants.EMPTY_FUNCTION
);
worker.on('error', (error) => {
- this.emitter.emit(WorkerSetEvents.error, error);
- this.addWorkerSetElement();
+ if (this.emitter !== undefined) {
+ this.emitter.emit(WorkerSetEvents.error, error);
+ }
+ if (this.workerOptions?.poolOptions?.restartWorkerOnError) {
+ this.addWorkerSetElement();
+ }
});
worker.on(
'online',
'exit',
this.workerOptions?.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION
);
- worker.on('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker)));
+ worker.once('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker)));
const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 };
this.workerSet.add(workerSetElement);
return workerSetElement;