From: Jérôme Benoit Date: Thu, 3 Mar 2022 23:01:34 +0000 (+0100) Subject: WorkerSet: consolidate the worker start delay code X-Git-Tag: v1.1.49~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4581acf33b078cca4a2a3e77c774788149dca5e7;p=e-mobility-charging-stations-simulator.git WorkerSet: consolidate the worker start delay code Signed-off-by: Jérôme Benoit --- diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index ccea2a38..a72f2c94 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -26,16 +26,31 @@ export default class WorkerFactory { switch (workerProcessType) { case WorkerProcessType.WORKER_SET: options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; - workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker, { workerStartDelay: options.workerStartDelay, elementStartDelay: options.elementStartDelay }, options); + workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker, + { + workerStartDelay: options.workerStartDelay, + elementStartDelay: options.elementStartDelay + }, + options); break; case WorkerProcessType.STATIC_POOL: options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; - workerImplementation = new WorkerStaticPool(workerScript, options.poolMaxSize, { workerStartDelay: options.workerStartDelay, elementStartDelay: options.elementStartDelay }, options.poolOptions); + workerImplementation = new WorkerStaticPool(workerScript, options.poolMaxSize, + { + workerStartDelay: options.workerStartDelay, + elementStartDelay: options.elementStartDelay + }, + options.poolOptions); break; case WorkerProcessType.DYNAMIC_POOL: options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE; options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE; - workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, { workerStartDelay: options.workerStartDelay, elementStartDelay: options.elementStartDelay }, options.poolOptions); + workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, + { + workerStartDelay: options.workerStartDelay, + elementStartDelay: options.elementStartDelay + }, + options.poolOptions); break; default: throw new Error(`Worker implementation type '${workerProcessType}' not found`); diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index dff07f23..5477ab0d 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -42,9 +42,7 @@ export default class WorkerSet extends WorkerAbstract { throw new Error('Cannot add a WorkerSet element: workers\' set does not exist'); } if (this.getLastWorkerSetElement().numberOfWorkerElements >= this.maxElementsPerWorker) { - this.startWorker(); - // Start worker sequentially to optimize memory at startup - this.workerStartDelay > 0 && await Utils.sleep(this.workerStartDelay); + await this.startWorker(); } this.getLastWorker().postMessage({ id: WorkerMessageEvents.START_WORKER_ELEMENT, data: elementData }); this.getLastWorkerSetElement().numberOfWorkerElements++; @@ -57,9 +55,7 @@ export default class WorkerSet extends WorkerAbstract { * @public */ public async start(): Promise { - this.startWorker(); - // Start worker sequentially to optimize memory at startup - this.workerStartDelay > 0 && await Utils.sleep(this.workerStartDelay); + await this.startWorker(); } /** @@ -78,7 +74,7 @@ export default class WorkerSet extends WorkerAbstract { * * @private */ - private startWorker(): void { + private async startWorker(): Promise { const worker = new Worker(this.workerScript); worker.on('message', (msg) => { (async () => { @@ -91,6 +87,8 @@ export default class WorkerSet extends WorkerAbstract { this.workerSet.delete(this.getWorkerSetElementByWorker(worker)); }); this.workerSet.add({ worker, numberOfWorkerElements: 0 }); + // Start worker sequentially to optimize memory at startup + this.workerStartDelay > 0 && await Utils.sleep(this.workerStartDelay); } private getLastWorkerSetElement(): WorkerSetElement {