fix: untangle worker pool/set init from start
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 19 Mar 2024 17:28:48 +0000 (18:28 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 19 Mar 2024 17:28:48 +0000 (18:28 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
bundle.js
src/charging-station/Bootstrap.ts
src/charging-station/Helpers.ts
src/worker/WorkerAbstract.ts
src/worker/WorkerConstants.ts
src/worker/WorkerDynamicPool.ts
src/worker/WorkerFixedPool.ts
src/worker/WorkerSet.ts

index 2d4bc8631952220d84f8a99141c794795d095606..4443f985160205ae4e0356f8d133f221712a3e6f 100644 (file)
--- a/bundle.js
+++ b/bundle.js
@@ -22,6 +22,7 @@ await build({
     'basic-ftp',
     'chalk',
     'date-fns',
+    'date-fns/*',
     'http-status-codes',
     'logform',
     'mnemonist',
index 545e88899da9a91629e3736d5025670f8b26c263..6eacfff46c462655a10a84d5209f545424694701 100644 (file)
@@ -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<string, TemplateStatistics>()
+    this.initializeWorkerImplementation(
+      Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
+    )
     this.uiServer = UIServerFactory.getUIServerImplementation(
       Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
     )
-    this.templateStatistics = new Map<string, TemplateStatistics>()
-    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<WorkerConfiguration>(
-          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<StorageConfiguration>(
             ConfigurationSection.performanceStorage
@@ -220,6 +224,9 @@ export class Bootstrap extends EventEmitter {
             )
           }
         }
+        const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(
+          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<void> {
     await this.stop()
+    // FIXME: initialize worker implementation only if the worker section has changed
+    this.initializeWorkerImplementation(
+      Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
+    )
     if (
       this.uiServerStarted &&
       Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
index 916cf499ecedaadfe821719658a1608aefbdf880..735a0b39c9bcf967f69297aa1a66618d2b4f9752 100644 (file)
@@ -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
index 9a10e6f2afa1de4c463dd8a2803917b61f5b76b5..907d0bd0e580273bbba8f54c60cfd0b73d4a6c7c 100644 (file)
@@ -39,7 +39,7 @@ export abstract class WorkerAbstract<T extends WorkerData> {
   /**
    * Starts the worker pool/set.
    */
-  public abstract start (): Promise<void>
+  public abstract start (): void | Promise<void>
   /**
    * Stops the worker pool/set.
    */
index 28e9927dd42790bfb43ae74d19141129abdfa162..ded5c89142f8e2aa9f485ed9df6ea476a7832100 100644 (file)
@@ -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,
index 0bdb26194465384f060cf6c01ef858799b3dafe9..2043297318c38e8d298a76c9d660b4d215bc8d71 100644 (file)
@@ -42,8 +42,8 @@ export class WorkerDynamicPool extends WorkerAbstract<WorkerData> {
   }
 
   /** @inheritDoc */
-  public async start (): Promise<void> {
-    // This is intentional
+  public start (): void {
+    this.pool.start()
   }
 
   /** @inheritDoc */
index 4eafa1d59933395ee0fb5f2c2dddc9f5404aeb00..22290666c17546e1f3c0f461a0cbca39e0841de2 100644 (file)
@@ -41,8 +41,8 @@ export class WorkerFixedPool extends WorkerAbstract<WorkerData> {
   }
 
   /** @inheritDoc */
-  public async start (): Promise<void> {
-    // This is intentional
+  public start (): void {
+    this.pool.start()
   }
 
   /** @inheritDoc */
index 1ad551efb0553e47458e2766734fdfd1cbc0ef12..d0f80b6ed59a54342430991ff4d318d86e0a5402 100644 (file)
@@ -99,7 +99,6 @@ export class WorkerSet extends WorkerAbstract<WorkerData> {
     this.emitter?.emit(WorkerSetEvents.stopped, this.info)
     this.started = false
     this.emitter?.emitDestroy()
-    this.emitter?.removeAllListeners()
   }
 
   /** @inheritDoc */