repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
build(deps): apply updates
[e-mobility-charging-stations-simulator.git]
/
src
/
worker
/
WorkerSet.ts
diff --git
a/src/worker/WorkerSet.ts
b/src/worker/WorkerSet.ts
index 7f140527d365bf12dd5b985e3d8c2c583a10de3f..047d03ad0ee09aa9922e4b66667f34ddc7f5b8e6 100644
(file)
--- a/
src/worker/WorkerSet.ts
+++ b/
src/worker/WorkerSet.ts
@@
-1,6
+1,6
@@
// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
-import { EventEmitter } from 'node:events';
+import { EventEmitter
AsyncResource
} from 'node:events';
import { SHARE_ENV, Worker } from 'node:worker_threads';
import { WorkerAbstract } from './WorkerAbstract';
import { SHARE_ENV, Worker } from 'node:worker_threads';
import { WorkerAbstract } from './WorkerAbstract';
@@
-17,7
+17,7
@@
import {
import { randomizeDelay, sleep } from './WorkerUtils';
export class WorkerSet extends WorkerAbstract<WorkerData> {
import { randomizeDelay, sleep } from './WorkerUtils';
export class WorkerSet extends WorkerAbstract<WorkerData> {
- public readonly emitter
!: EventEmitter
;
+ public readonly emitter
: EventEmitterAsyncResource | undefined
;
private readonly workerSet: Set<WorkerSetElement>;
private started: boolean;
private workerStartup: boolean;
private readonly workerSet: Set<WorkerSetElement>;
private started: boolean;
private workerStartup: boolean;
@@
-30,10
+30,7
@@
export class WorkerSet extends WorkerAbstract<WorkerData> {
*/
constructor(workerScript: string, workerOptions: WorkerOptions) {
super(workerScript, workerOptions);
*/
constructor(workerScript: string, workerOptions: WorkerOptions) {
super(workerScript, workerOptions);
- if (
- this.workerOptions.elementsPerWorker === null ||
- this.workerOptions.elementsPerWorker === undefined
- ) {
+ if (this.workerOptions.elementsPerWorker == null) {
throw new TypeError('Elements per worker is not defined');
}
if (!Number.isSafeInteger(this.workerOptions.elementsPerWorker)) {
throw new TypeError('Elements per worker is not defined');
}
if (!Number.isSafeInteger(this.workerOptions.elementsPerWorker)) {
@@
-44,7
+41,7
@@
export class WorkerSet extends WorkerAbstract<WorkerData> {
}
this.workerSet = new Set<WorkerSetElement>();
if (this.workerOptions.poolOptions?.enableEvents) {
}
this.workerSet = new Set<WorkerSetElement>();
if (this.workerOptions.poolOptions?.enableEvents) {
- this.emitter = new EventEmitter
(
);
+ this.emitter = new EventEmitter
AsyncResource({ name: 'workerset' }
);
}
this.started = false;
this.workerStartup = false;
}
this.started = false;
this.workerStartup = false;
@@
-78,6
+75,7
@@
export class WorkerSet extends WorkerAbstract<WorkerData> {
// Add worker set element sequentially to optimize memory at startup
this.workerOptions.workerStartDelay! > 0 &&
(await sleep(randomizeDelay(this.workerOptions.workerStartDelay!)));
// Add worker set element sequentially to optimize memory at startup
this.workerOptions.workerStartDelay! > 0 &&
(await sleep(randomizeDelay(this.workerOptions.workerStartDelay!)));
+ this.emitter?.emit(WorkerSetEvents.started, this.info);
this.started = true;
}
this.started = true;
}
@@
-92,6
+90,9
@@
export class WorkerSet extends WorkerAbstract<WorkerData> {
});
await worker.terminate();
await waitWorkerExit;
});
await worker.terminate();
await waitWorkerExit;
+ this.emitter?.emit(WorkerSetEvents.stopped, this.info);
+ this.emitter?.emitDestroy();
+ this.emitter?.removeAllListeners();
this.started = false;
}
}
this.started = false;
}
}
@@
-101,7
+102,7
@@
export class WorkerSet extends WorkerAbstract<WorkerData> {
if (!this.started) {
throw new Error('Cannot add a WorkerSet element: not started');
}
if (!this.started) {
throw new Error('Cannot add a WorkerSet element: not started');
}
- if (
!this.workerSet
) {
+ if (
this.workerSet == null
) {
throw new Error("Cannot add a WorkerSet element: 'workerSet' property does not exist");
}
const workerSetElement = await this.getWorkerSetElement();
throw new Error("Cannot add a WorkerSet element: 'workerSet' property does not exist");
}
const workerSetElement = await this.getWorkerSetElement();