@onlineapps/service-wrapper 2.0.49 → 2.0.50

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.49",
3
+ "version": "2.0.50",
4
4
  "description": "Thin orchestration layer for microservices - delegates all infrastructure concerns to specialized connectors",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -485,6 +485,11 @@ class ServiceWrapper {
485
485
  console.warn('Monitoring initialization failed (non-critical):', error.message);
486
486
  }
487
487
  }
488
+
489
+ // Register MQ channel close hooks and health monitoring AFTER monitoring is initialized
490
+ if (this.mqClient) {
491
+ await this._registerMQHooks();
492
+ }
488
493
 
489
494
  // Initialize cache if configured
490
495
  if (this.config.wrapper?.cache?.enabled === true) {
@@ -557,6 +562,68 @@ class ServiceWrapper {
557
562
  }
558
563
  }
559
564
 
565
+ /**
566
+ * Register MQ channel close hooks and health monitoring
567
+ * Must be called AFTER monitoring is initialized
568
+ * @private
569
+ */
570
+ async _registerMQHooks() {
571
+ if (!this.mqClient || !this.mqClient._transport) {
572
+ return;
573
+ }
574
+
575
+ // Access the underlying transport (RabbitMQClient) to register hooks
576
+ const transport = this.mqClient._transport;
577
+
578
+ if (typeof transport.onChannelClose === 'function') {
579
+ // Register hook for all channel types
580
+ transport.onChannelClose('all', (details) => {
581
+ const logData = {
582
+ type: details.type,
583
+ reason: details.reason,
584
+ error: details.error ? {
585
+ message: details.error.message,
586
+ code: details.error.code,
587
+ stack: details.error.stack
588
+ } : null,
589
+ timestamp: details.timestamp,
590
+ connectionState: details.connectionState,
591
+ channelCreatedAt: details.channelCreatedAt,
592
+ lastOperation: details.lastOperation
593
+ };
594
+
595
+ if (this.monitoring && this.monitoring.error) {
596
+ this.monitoring.error(`MQ Channel Closed: ${details.type}`, logData);
597
+ }
598
+ console.error(`[ServiceWrapper] [MQ] Channel closed: ${details.type} - ${details.reason}`);
599
+ });
600
+
601
+ this.logger?.info('MQ channel close hooks registered');
602
+ }
603
+
604
+ // Register health shutdown handler
605
+ if (typeof transport.on === 'function') {
606
+ transport.on('health:shutdown', (health) => {
607
+ console.error('[ServiceWrapper] [MQ] Health shutdown triggered - shutting down service');
608
+ if (this.monitoring && this.monitoring.error) {
609
+ this.monitoring.error('MQ Health Shutdown', {
610
+ issues: health.issues,
611
+ timestamp: health.timestamp
612
+ });
613
+ }
614
+ // Graceful shutdown
615
+ this.shutdown().then(() => {
616
+ process.exit(1);
617
+ }).catch((err) => {
618
+ console.error('[ServiceWrapper] Shutdown error:', err);
619
+ process.exit(1);
620
+ });
621
+ });
622
+
623
+ this.logger?.info('MQ health shutdown handler registered');
624
+ }
625
+ }
626
+
560
627
  /**
561
628
  * Initialize MQ connection
562
629
  * @private
@@ -624,51 +691,6 @@ class ServiceWrapper {
624
691
  await this.mqClient.connect();
625
692
  this.logger?.info('MQ connector initialized');
626
693
 
627
- // Register channel close hooks after connection is established
628
- // Access the underlying transport (RabbitMQClient) via _transport
629
- if (this.mqClient._transport && typeof this.mqClient._transport.onChannelClose === 'function') {
630
- // Register hook for all channel types
631
- this.mqClient._transport.onChannelClose('all', (details) => {
632
- const logData = {
633
- type: details.type,
634
- reason: details.reason,
635
- error: details.error ? {
636
- message: details.error.message,
637
- code: details.error.code,
638
- stack: details.error.stack
639
- } : null,
640
- timestamp: details.timestamp,
641
- connectionState: details.connectionState,
642
- channelCreatedAt: details.channelCreatedAt,
643
- lastOperation: details.lastOperation
644
- };
645
-
646
- if (this.monitoring && this.monitoring.error) {
647
- this.monitoring.error(`MQ Channel Closed: ${details.type}`, logData);
648
- }
649
- console.error(`[ServiceWrapper] [MQ] Channel closed: ${details.type} - ${details.reason}`);
650
- });
651
-
652
- // Register health shutdown handler
653
- this.mqClient._transport.on('health:shutdown', (health) => {
654
- console.error('[ServiceWrapper] [MQ] Health shutdown triggered - shutting down service');
655
- if (this.monitoring && this.monitoring.error) {
656
- this.monitoring.error('MQ Health Shutdown', {
657
- issues: health.issues,
658
- timestamp: health.timestamp
659
- });
660
- }
661
- // Graceful shutdown
662
- this.shutdown().then(() => {
663
- process.exit(1);
664
- }).catch((err) => {
665
- console.error('[ServiceWrapper] Shutdown error:', err);
666
- process.exit(1);
667
- });
668
- });
669
-
670
- this.logger?.info('MQ channel close hooks and health monitoring registered');
671
- }
672
694
  } catch (error) {
673
695
  const message = `[MQConnector] Unable to connect to RabbitMQ at ${mqUrl}. ` +
674
696
  'Verify that api_services_queuer is running and accessible. ' +