From: Jérôme Benoit Date: Tue, 19 Mar 2024 17:28:48 +0000 (+0100) Subject: fix: untangle worker pool/set init from start X-Git-Tag: v1.3.1~32 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=24dc52e9f4bd2ff4539955f169333ee3902bc95e;p=e-mobility-charging-stations-simulator.git fix: untangle worker pool/set init from start Signed-off-by: Jérôme Benoit --- diff --git a/bundle.js b/bundle.js index 2d4bc863..4443f985 100644 --- a/bundle.js +++ b/bundle.js @@ -22,6 +22,7 @@ await build({ 'basic-ftp', 'chalk', 'date-fns', + 'date-fns/*', 'http-status-codes', 'logform', 'mnemonist', diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 545e8889..6eacfff4 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -81,12 +81,15 @@ export class Bootstrap extends EventEmitter { this.started = false this.starting = false this.stopping = false + this.initializedCounters = false this.uiServerStarted = false + this.templateStatistics = new Map() + this.initializeWorkerImplementation( + Configuration.getConfigurationSection(ConfigurationSection.worker) + ) this.uiServer = UIServerFactory.getUIServerImplementation( Configuration.getConfigurationSection(ConfigurationSection.uiServer) ) - this.templateStatistics = new Map() - this.initializedCounters = false this.initializeCounters() Configuration.configurationChangeCallback = async () => { if (isMainThread) { @@ -175,11 +178,12 @@ export class Bootstrap extends EventEmitter { } ) this.initializeCounters() - const workerConfiguration = Configuration.getConfigurationSection( - ConfigurationSection.worker - ) - this.initializeWorkerImplementation(workerConfiguration) - await this.workerImplementation?.start() + // eslint-disable-next-line @typescript-eslint/unbound-method + if (isAsyncFunction(this.workerImplementation?.start)) { + await this.workerImplementation.start() + } else { + (this.workerImplementation?.start as () => void)() + } const performanceStorageConfiguration = Configuration.getConfigurationSection( ConfigurationSection.performanceStorage @@ -220,6 +224,9 @@ export class Bootstrap extends EventEmitter { ) } } + const workerConfiguration = Configuration.getConfigurationSection( + ConfigurationSection.worker + ) console.info( chalk.green( `Charging stations simulator ${ @@ -269,7 +276,6 @@ export class Bootstrap extends EventEmitter { console.error(chalk.red('Error while waiting for charging stations to stop: '), error) } await this.workerImplementation?.stop() - delete this.workerImplementation this.removeAllListeners() this.uiServer.clearCaches() this.initializedCounters = false @@ -287,6 +293,10 @@ export class Bootstrap extends EventEmitter { private async restart (): Promise { await this.stop() + // FIXME: initialize worker implementation only if the worker section has changed + this.initializeWorkerImplementation( + Configuration.getConfigurationSection(ConfigurationSection.worker) + ) if ( this.uiServerStarted && Configuration.getConfigurationSection(ConfigurationSection.uiServer) diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 916cf499..735a0b39 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -974,7 +974,7 @@ export const prepareChargingProfileKind = ( if (connectorStatus?.transactionStarted === true) { chargingProfile.chargingSchedule.startSchedule = connectorStatus.transactionStart } - // FIXME: Handle relative charging profile duration + // FIXME: handle relative charging profile duration break } return true diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 9a10e6f2..907d0bd0 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -39,7 +39,7 @@ export abstract class WorkerAbstract { /** * Starts the worker pool/set. */ - public abstract start (): Promise + public abstract start (): void | Promise /** * Stops the worker pool/set. */ diff --git a/src/worker/WorkerConstants.ts b/src/worker/WorkerConstants.ts index 28e9927d..ded5c891 100644 --- a/src/worker/WorkerConstants.ts +++ b/src/worker/WorkerConstants.ts @@ -22,6 +22,7 @@ export const DEFAULT_WORKER_OPTIONS: WorkerOptions = Object.freeze({ poolMaxSize: DEFAULT_POOL_MAX_SIZE, elementsPerWorker: DEFAULT_ELEMENTS_PER_WORKER, poolOptions: { + startWorkers: false, enableEvents: true, restartWorkerOnError: true, errorHandler: defaultErrorHandler, diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index 0bdb2619..20432973 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -42,8 +42,8 @@ export class WorkerDynamicPool extends WorkerAbstract { } /** @inheritDoc */ - public async start (): Promise { - // This is intentional + public start (): void { + this.pool.start() } /** @inheritDoc */ diff --git a/src/worker/WorkerFixedPool.ts b/src/worker/WorkerFixedPool.ts index 4eafa1d5..22290666 100644 --- a/src/worker/WorkerFixedPool.ts +++ b/src/worker/WorkerFixedPool.ts @@ -41,8 +41,8 @@ export class WorkerFixedPool extends WorkerAbstract { } /** @inheritDoc */ - public async start (): Promise { - // This is intentional + public start (): void { + this.pool.start() } /** @inheritDoc */ diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 1ad551ef..d0f80b6e 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -99,7 +99,6 @@ export class WorkerSet extends WorkerAbstract { this.emitter?.emit(WorkerSetEvents.stopped, this.info) this.started = false this.emitter?.emitDestroy() - this.emitter?.removeAllListeners() } /** @inheritDoc */