@onlineapps/service-wrapper 2.1.94 → 2.1.96

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,10 @@
5
5
  "wrapper": {
6
6
  "mq": {
7
7
  "prefetch": 10,
8
- "enabled": true
8
+ "enabled": true,
9
+ "connectionRetries": 10,
10
+ "connectionRetryDelayMs": 2000,
11
+ "connectionRetryMaxDelayMs": 30000
9
12
  },
10
13
  "registry": {
11
14
  "enabled": true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onlineapps/service-wrapper",
3
- "version": "2.1.94",
3
+ "version": "2.1.96",
4
4
  "description": "Thin orchestration layer for microservices - delegates all infrastructure concerns to specialized connectors",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -775,19 +775,39 @@ class ServiceWrapper {
775
775
  // Note: transport is created during connect(), so we need to register hooks after connect
776
776
  // We'll do this in a separate method called after connect
777
777
 
778
- try {
779
- await this.mqClient.connect();
780
- this.logger?.info('MQ connector initialized');
781
-
782
- } catch (error) {
783
- const message = `[MQConnector] Unable to connect to RabbitMQ at ${mqUrl}. ` +
784
- 'Verify that api_services_queuer is running and accessible. ' +
785
- `Original error: ${error.message || error}`;
786
- this.logger?.error(message, { error: error.message, code: error.code });
787
- const wrappedError = new Error(message);
788
- wrappedError.cause = error;
789
- throw wrappedError;
778
+ // Retry configuration from config or defaults
779
+ const maxRetries = this.config.wrapper?.mq?.connectionRetries ?? 10;
780
+ const initialDelayMs = this.config.wrapper?.mq?.connectionRetryDelayMs ?? 2000;
781
+ const maxDelayMs = this.config.wrapper?.mq?.connectionRetryMaxDelayMs ?? 30000;
782
+
783
+ let lastError = null;
784
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
785
+ try {
786
+ await this.mqClient.connect();
787
+ this.logger?.info('MQ connector initialized');
788
+ return; // Success - exit the function
789
+ } catch (error) {
790
+ lastError = error;
791
+ const delay = Math.min(initialDelayMs * Math.pow(2, attempt - 1), maxDelayMs);
792
+
793
+ if (attempt < maxRetries) {
794
+ this.logger?.warn(`[MQConnector] Connection attempt ${attempt}/${maxRetries} failed. Retrying in ${delay}ms...`, {
795
+ error: error.message,
796
+ nextRetryIn: delay
797
+ });
798
+ await new Promise(resolve => setTimeout(resolve, delay));
799
+ }
800
+ }
790
801
  }
802
+
803
+ // All retries exhausted
804
+ const message = `[MQConnector] Unable to connect to RabbitMQ at ${mqUrl} after ${maxRetries} attempts. ` +
805
+ 'Verify that api_services_queuer is running and accessible. ' +
806
+ `Original error: ${lastError?.message || lastError}`;
807
+ this.logger?.error(message, { error: lastError?.message, code: lastError?.code, attempts: maxRetries });
808
+ const wrappedError = new Error(message);
809
+ wrappedError.cause = lastError;
810
+ throw wrappedError;
791
811
 
792
812
  // NOTE: Business services (ServiceWrapper) do NOT create infrastructure queues
793
813
  // Infrastructure queues are created by infrastructure services (Monitoring Consumer, Gateway)
@@ -837,13 +857,7 @@ class ServiceWrapper {
837
857
  );
838
858
  }
839
859
 
840
- // FAIL-FAST: logger must be initialized
841
- if (!this.logger) {
842
- throw new Error(
843
- `[ServiceWrapper] Missing required dependency - logger is not initialized.`
844
- );
845
- }
846
-
860
+ // Logger may not be initialized yet at this point - RegistryClient handles null gracefully
847
861
  this.registryClient = new RegistryConnector.ServiceRegistryClient({
848
862
  amqpUrl: mqUrl,
849
863
  serviceName: serviceName,
@@ -851,7 +865,7 @@ class ServiceWrapper {
851
865
  specificationEndpoint: specificationEndpoint,
852
866
  registryQueue: 'registry.register', // Use correct queue name
853
867
  registryUrl: registryUrl,
854
- logger: this.logger,
868
+ logger: this.logger || console, // Use console as fallback if logger not yet initialized
855
869
  validationProof: this.validationProof // Inject validation proof
856
870
  });
857
871