@onlineapps/mq-client-core 1.0.10 → 1.0.11

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/mq-client-core",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "Core MQ client library for RabbitMQ - shared by infrastructure services and connectors",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * initInfrastructureQueues.js
5
+ *
6
+ * Utility for infrastructure services to initialize infrastructure queues.
7
+ * Uses queueConfig from @onlineapps/conn-infra-mq for configuration.
8
+ *
9
+ * NOTE: This is for infrastructure services (gateway, monitoring, etc.)
10
+ * Business services should use @onlineapps/conn-infra-mq connector instead.
11
+ */
12
+
13
+ /**
14
+ * Initialize all infrastructure queues
15
+ * @param {Object} channel - RabbitMQ channel
16
+ * @param {Object} options - Options
17
+ * @param {Array<string>} [options.queues] - Specific queues to create (default: all infrastructure queues)
18
+ * @param {Object} [options.logger] - Logger instance (default: console)
19
+ * @returns {Promise<void>}
20
+ */
21
+ async function initInfrastructureQueues(channel, options = {}) {
22
+ const logger = options.logger || console;
23
+
24
+ // Load queueConfig from @onlineapps/conn-infra-mq
25
+ let queueConfig;
26
+ try {
27
+ queueConfig = require('@onlineapps/conn-infra-mq/src/config/queueConfig');
28
+ } catch (requireError) {
29
+ throw new Error(`Cannot load queueConfig from @onlineapps/conn-infra-mq: ${requireError.message}. Make sure @onlineapps/conn-infra-mq is installed.`);
30
+ }
31
+
32
+ const queuesToCreate = options.queues || [
33
+ // Workflow infrastructure queues
34
+ 'workflow.init',
35
+ 'workflow.completed',
36
+ 'workflow.failed',
37
+ 'workflow.dlq',
38
+ // Registry infrastructure queues
39
+ 'registry.register',
40
+ 'registry.heartbeats'
41
+ ];
42
+
43
+ logger.log(`[QueueInit] Initializing ${queuesToCreate.length} infrastructure queues...`);
44
+
45
+ for (const queueName of queuesToCreate) {
46
+ try {
47
+ // Verify it's an infrastructure queue
48
+ if (!queueConfig.isInfrastructureQueue(queueName)) {
49
+ logger.warn(`[QueueInit] Skipping ${queueName} - not an infrastructure queue`);
50
+ continue;
51
+ }
52
+
53
+ // Get unified configuration
54
+ const config = queueConfig.getInfrastructureQueueConfig(queueName);
55
+
56
+ // CRITICAL: Check if queue exists with different arguments (406 error)
57
+ // If so, delete it and recreate with correct parameters
58
+ try {
59
+ await channel.assertQueue(queueName, {
60
+ durable: config.durable !== false,
61
+ arguments: { ...config.arguments }
62
+ });
63
+ logger.log(`[QueueInit] ✓ Created/verified infrastructure queue: ${queueName}`);
64
+ } catch (assertError) {
65
+ if (assertError.code === 406) {
66
+ // Queue exists with different arguments - delete and recreate
67
+ logger.warn(`[QueueInit] ⚠ Queue ${queueName} exists with different arguments - deleting and recreating...`);
68
+ try {
69
+ await channel.deleteQueue(queueName);
70
+ logger.log(`[QueueInit] ✓ Deleted queue ${queueName} with incorrect parameters`);
71
+ } catch (deleteError) {
72
+ logger.error(`[QueueInit] ✗ Failed to delete queue ${queueName}:`, deleteError.message);
73
+ throw new Error(`Cannot delete queue ${queueName} with incorrect parameters: ${deleteError.message}`);
74
+ }
75
+
76
+ // Recreate with correct parameters
77
+ await channel.assertQueue(queueName, {
78
+ durable: config.durable !== false,
79
+ arguments: { ...config.arguments }
80
+ });
81
+ logger.log(`[QueueInit] ✓ Recreated infrastructure queue: ${queueName} with correct parameters`);
82
+ } else {
83
+ // Other error - rethrow
84
+ logger.error(`[QueueInit] ✗ Failed to create ${queueName}:`, assertError.message);
85
+ throw assertError;
86
+ }
87
+ }
88
+ } catch (error) {
89
+ logger.error(`[QueueInit] ✗ Failed to initialize queue ${queueName}:`, error.message);
90
+ throw error;
91
+ }
92
+ }
93
+
94
+ logger.log(`[QueueInit] Infrastructure queues initialization complete`);
95
+ }
96
+
97
+ module.exports = {
98
+ initInfrastructureQueues
99
+ };
100
+