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`);
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++;
* @public
*/
public async start(): Promise<void> {
- this.startWorker();
- // Start worker sequentially to optimize memory at startup
- this.workerStartDelay > 0 && await Utils.sleep(this.workerStartDelay);
+ await this.startWorker();
}
/**
*
* @private
*/
- private startWorker(): void {
+ private async startWorker(): Promise<void> {
const worker = new Worker(this.workerScript);
worker.on('message', (msg) => {
(async () => {
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 {