@onlineapps/service-wrapper 2.0.30 → 2.0.31

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onlineapps/service-wrapper",
3
- "version": "2.0.30",
3
+ "version": "2.0.31",
4
4
  "description": "Thin orchestration layer for microservices - delegates all infrastructure concerns to specialized connectors",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -137,14 +137,16 @@ class ServiceWrapper {
137
137
  * @async
138
138
  */
139
139
  async initialize() {
140
- if (this.isInitialized) {
141
- console.warn('ServiceWrapper already initialized');
140
+ if (this.isInitialized) {
141
+ // Logger might not be initialized yet, use console as fallback
142
+ (this.logger || console).warn('ServiceWrapper already initialized');
142
143
  return;
143
144
  }
144
145
 
145
146
  try {
146
147
  const serviceName = this.config.service?.name || 'unnamed-service';
147
- console.log(`Initializing ServiceWrapper for ${serviceName}`);
148
+ // Logger might not be initialized yet, use console as fallback
149
+ (this.logger || console).info(`Initializing ServiceWrapper for ${serviceName}`);
148
150
 
149
151
  // 1. Initialize monitoring first (needed for logging)
150
152
  if (this.config.wrapper?.monitoring?.enabled !== false) {
@@ -190,10 +192,15 @@ class ServiceWrapper {
190
192
  }
191
193
 
192
194
  this.isInitialized = true;
193
- console.log(`ServiceWrapper initialized successfully for ${serviceName}`);
195
+ this.logger?.info(`ServiceWrapper initialized successfully for ${serviceName}`);
194
196
 
195
197
  } catch (error) {
196
- console.error('Failed to initialize ServiceWrapper:', error);
198
+ // Always log errors - use logger if available, otherwise console
199
+ if (this.logger) {
200
+ this.logger.error('Failed to initialize ServiceWrapper', { error: error.message, stack: error.stack });
201
+ } else {
202
+ console.error('Failed to initialize ServiceWrapper:', error);
203
+ }
197
204
  throw error;
198
205
  }
199
206
  }
@@ -212,7 +219,8 @@ class ServiceWrapper {
212
219
  });
213
220
 
214
221
  this.logger = this.monitoring;
215
- console.log('Monitoring connector initialized');
222
+ // Logger is now available, use it
223
+ this.logger.info('Monitoring connector initialized');
216
224
 
217
225
  // Add monitoring middleware to Express app
218
226
  if (this.app) {
@@ -257,7 +265,7 @@ class ServiceWrapper {
257
265
  });
258
266
 
259
267
  await this.mqClient.connect();
260
- console.log('MQ connector initialized');
268
+ this.logger?.info('MQ connector initialized');
261
269
 
262
270
  // NOTE: Business services (ServiceWrapper) do NOT create infrastructure queues
263
271
  // Infrastructure queues are created by infrastructure services (Monitoring Consumer, Gateway)
@@ -290,7 +298,7 @@ class ServiceWrapper {
290
298
 
291
299
  // Initialize registry client connection
292
300
  await this.registryClient.init();
293
- console.log('Registry client initialized');
301
+ this.logger?.info('Registry client initialized');
294
302
 
295
303
  // Register service
296
304
  const serviceInfo = {
@@ -308,28 +316,28 @@ class ServiceWrapper {
308
316
 
309
317
  try {
310
318
  const registrationResult = await this.registryClient.register(serviceInfo);
311
- console.log(`Service registered: ${serviceName}`, registrationResult);
319
+ this.logger?.info(`Service registered: ${serviceName}`, { registrationResult });
312
320
 
313
321
  // Store certificate if received
314
322
  if (registrationResult.certificate) {
315
323
  this.certificate = registrationResult.certificate;
316
- console.log(`✓ Certificate received: ${registrationResult.certificate.certificateId}`);
324
+ this.logger?.info(`✓ Certificate received: ${registrationResult.certificate.certificateId}`);
317
325
  }
318
326
 
319
327
  // CRITICAL: Start workflow listeners ONLY after successful validation and registration with certificate
320
328
  // Both workflow.init and service-specific listeners start here
321
329
  if (registrationResult.success && registrationResult.certificate) {
322
- console.log('✓ Certificate validated, starting workflow listeners...');
330
+ this.logger?.info('✓ Certificate validated, starting workflow listeners...');
323
331
  // Start workflow.init listener (for all services)
324
332
  await this._startWorkflowInitListener();
325
333
  // Start service-specific workflow listener
326
334
  await this._startServiceWorkflowListener();
327
335
  } else {
328
- console.warn('⚠ Registration succeeded but no certificate received - workflow listeners NOT started');
336
+ this.logger?.warn('⚠ Registration succeeded but no certificate received - workflow listeners NOT started');
329
337
  }
330
338
 
331
339
  } catch (error) {
332
- console.error(`Service registration failed: ${error.message}`);
340
+ this.logger?.error(`Service registration failed: ${error.message}`, { error: error.message, stack: error.stack });
333
341
  throw new Error(`Failed to register service ${serviceName}: ${error.message}`);
334
342
  }
335
343
 
@@ -338,13 +346,13 @@ class ServiceWrapper {
338
346
  const heartbeatInterval = parseInt(process.env.HEARTBEAT_INTERVAL) ||
339
347
  this.config.wrapper?.registry?.heartbeatInterval ||
340
348
  10000;
341
- console.log(`[ServiceWrapper] Heartbeat interval set to ${heartbeatInterval}ms`);
349
+ this.logger?.info(`[ServiceWrapper] Heartbeat interval set to ${heartbeatInterval}ms`);
342
350
 
343
351
  this.heartbeatTimer = setInterval(async () => {
344
352
  try {
345
353
  await this.registryClient.sendHeartbeat(serviceName);
346
354
  } catch (error) {
347
- console.error('Heartbeat failed:', error.message);
355
+ this.logger?.error('Heartbeat failed', { error: error.message, stack: error.stack });
348
356
  }
349
357
  }, heartbeatInterval);
350
358
  }
@@ -356,7 +364,7 @@ class ServiceWrapper {
356
364
  async _initializeCache() {
357
365
  const cacheUrl = this.config.wrapper?.cache?.url;
358
366
  if (!cacheUrl) {
359
- console.warn('Cache enabled but no URL provided. Skipping cache initialization');
367
+ this.logger?.warn('Cache enabled but no URL provided. Skipping cache initialization');
360
368
  return;
361
369
  }
362
370
 
@@ -369,7 +377,7 @@ class ServiceWrapper {
369
377
  });
370
378
 
371
379
  await this.cacheConnector.connect();
372
- console.log('Cache connector initialized');
380
+ this.logger?.info('Cache connector initialized');
373
381
  }
374
382
 
375
383
  /**
@@ -404,7 +412,7 @@ class ServiceWrapper {
404
412
  res.json(health);
405
413
  });
406
414
 
407
- console.log(`Health check endpoint registered at ${healthEndpoint}`);
415
+ this.logger?.info(`Health check endpoint registered at ${healthEndpoint}`);
408
416
  }
409
417
 
410
418
  /**
@@ -455,7 +463,7 @@ class ServiceWrapper {
455
463
  defaultTimeout: this.config.wrapper?.timeout || 30000
456
464
  });
457
465
 
458
- console.log('Orchestrator initialized');
466
+ this.logger?.info('Orchestrator initialized');
459
467
  }
460
468
 
461
469
  /**
@@ -465,7 +473,7 @@ class ServiceWrapper {
465
473
  */
466
474
  async _startWorkflowInitListener() {
467
475
  if (!this.mqClient) {
468
- console.warn('Cannot start workflow.init listener: MQ client not initialized');
476
+ this.logger?.warn('Cannot start workflow.init listener: MQ client not initialized');
469
477
  return;
470
478
  }
471
479
 
@@ -474,7 +482,7 @@ class ServiceWrapper {
474
482
  await this._processWorkflowMessage(message, 'workflow.init');
475
483
  });
476
484
 
477
- console.log('✓ workflow.init listener started (after successful registration)');
485
+ this.logger?.info('✓ workflow.init listener started (after successful registration)');
478
486
  }
479
487
 
480
488
  /**
@@ -483,7 +491,7 @@ class ServiceWrapper {
483
491
  */
484
492
  async _startServiceWorkflowListener() {
485
493
  if (!this.mqClient) {
486
- console.warn('Cannot start service workflow listener: MQ client not initialized');
494
+ this.logger?.warn('Cannot start service workflow listener: MQ client not initialized');
487
495
  return;
488
496
  }
489
497
 
@@ -495,7 +503,7 @@ class ServiceWrapper {
495
503
  await this._processWorkflowMessage(message, serviceQueue);
496
504
  });
497
505
 
498
- console.log(`✓ Service-specific workflow listener started: ${serviceQueue}`);
506
+ this.logger?.info(`✓ Service-specific workflow listener started: ${serviceQueue}`);
499
507
  }
500
508
 
501
509
  /**
@@ -520,7 +528,7 @@ class ServiceWrapper {
520
528
  const isTest = flags.includes('test');
521
529
  const isTier2Validation = flags.includes('tier2-validation');
522
530
 
523
- console.log(`Processing message from ${queueName}:`, {
531
+ this.logger?.debug(`Processing message from ${queueName}`, {
524
532
  workflow_id: message.workflow_id,
525
533
  step: message.step?.operation || message.operation,
526
534
  flags,
@@ -530,7 +538,7 @@ class ServiceWrapper {
530
538
  // Check if this message is for our service
531
539
  const serviceName = this.config.service?.name || 'unnamed-service';
532
540
  if (message.step?.service && message.step.service !== serviceName) {
533
- console.log(`Message not for this service (target: ${message.step.service})`);
541
+ this.logger?.debug(`Message not for this service (target: ${message.step.service})`);
534
542
  return;
535
543
  }
536
544
 
@@ -635,7 +643,7 @@ class ServiceWrapper {
635
643
  const url = `http://localhost:${servicePort}${operation.endpoint}`;
636
644
  const method = operation.method || 'POST';
637
645
 
638
- console.log(`Executing operation ${operationName} via ${method} ${url}`);
646
+ this.logger?.debug(`Executing operation ${operationName} via ${method} ${url}`);
639
647
 
640
648
  // Make HTTP request using fetch (Node.js 18+) or http module
641
649
  const http = require('http');
@@ -690,7 +698,7 @@ class ServiceWrapper {
690
698
  */
691
699
  async shutdown() {
692
700
  const serviceName = this.config.service?.name || 'unnamed-service';
693
- console.log(`Shutting down ServiceWrapper for ${serviceName}`);
701
+ this.logger?.info(`Shutting down ServiceWrapper for ${serviceName}`);
694
702
 
695
703
  try {
696
704
  // Stop heartbeat
@@ -714,10 +722,10 @@ class ServiceWrapper {
714
722
  }
715
723
 
716
724
  this.isInitialized = false;
717
- console.log('ServiceWrapper shutdown complete');
725
+ this.logger?.info('ServiceWrapper shutdown complete');
718
726
 
719
727
  } catch (error) {
720
- console.error('Error during shutdown:', error);
728
+ this.logger?.error('Error during shutdown', { error: error.message, stack: error.stack });
721
729
  throw error;
722
730
  }
723
731
  }
@@ -735,7 +743,7 @@ class ServiceWrapper {
735
743
  const proofPath = path.join(process.cwd(), '.validation-proof.json');
736
744
 
737
745
  if (!fs.existsSync(proofPath)) {
738
- console.warn('Validation proof not found:', proofPath);
746
+ this.logger?.warn('Validation proof not found', { proofPath });
739
747
  return null;
740
748
  }
741
749
 
@@ -744,15 +752,15 @@ class ServiceWrapper {
744
752
 
745
753
  // Basic validation
746
754
  if (!proof.validationProof || !proof.validationData) {
747
- console.warn('Invalid validation proof format');
755
+ this.logger?.warn('Invalid validation proof format');
748
756
  return null;
749
757
  }
750
758
 
751
- console.log('✓ Validation proof loaded:', proof.validationProof.substring(0, 16) + '...');
759
+ this.logger?.info('✓ Validation proof loaded', { proofHash: proof.validationProof.substring(0, 16) + '...' });
752
760
  return proof;
753
761
 
754
762
  } catch (error) {
755
- console.warn('Failed to load validation proof:', error.message);
763
+ this.logger?.warn('Failed to load validation proof', { error: error.message, stack: error.stack });
756
764
  return null;
757
765
  }
758
766
  }
@@ -783,7 +791,7 @@ class ServiceWrapper {
783
791
  // Build service URL for validation (HTTP calls to running server)
784
792
  const serviceUrl = `http://localhost:${servicePort}`;
785
793
 
786
- console.log('[ServiceWrapper] Checking validation proof...');
794
+ this.logger?.info('[ServiceWrapper] Checking validation proof...');
787
795
 
788
796
  // Use injected orchestrator (for testing) or create new one (production)
789
797
  const orchestrator = this._injectedValidationOrchestrator || new ValidationOrchestrator({
@@ -801,9 +809,9 @@ class ServiceWrapper {
801
809
  }
802
810
 
803
811
  if (result.skipped) {
804
- console.log('[ServiceWrapper] ✓ Using existing validation proof');
812
+ this.logger?.info('[ServiceWrapper] ✓ Using existing validation proof');
805
813
  } else {
806
- console.log('[ServiceWrapper] ✓ Validation completed successfully');
814
+ this.logger?.info('[ServiceWrapper] ✓ Validation completed successfully');
807
815
  }
808
816
 
809
817
  // Store proof for registration