+ /** @inheritDoc */
+ public async start(): Promise<void> {
+ this.addWorkerSetElement();
+ // 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;
+ }
+
+ /** @inheritDoc */
+ public async stop(): Promise<void> {
+ for (const workerSetElement of this.workerSet) {
+ const worker = workerSetElement.worker;
+ const waitWorkerExit = new Promise<void>((resolve) => {
+ worker.once('exit', () => {
+ resolve();
+ });
+ });
+ await worker.terminate();
+ await waitWorkerExit;
+ this.emitter?.emit(WorkerSetEvents.stopped, this.info);
+ this.emitter?.emitDestroy();
+ this.started = false;
+ }
+ }
+