@onlineapps/mq-client-core 1.0.10 → 1.0.12
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 +1 -1
- package/src/index.js +2 -0
- package/src/utils/initInfrastructureQueues.js +100 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -16,10 +16,12 @@ const {
|
|
|
16
16
|
ConsumeError,
|
|
17
17
|
SerializationError,
|
|
18
18
|
} = require('./utils/errorHandler');
|
|
19
|
+
const { initInfrastructureQueues } = require('./utils/initInfrastructureQueues');
|
|
19
20
|
|
|
20
21
|
module.exports = BaseClient;
|
|
21
22
|
module.exports.BaseClient = BaseClient;
|
|
22
23
|
module.exports.RabbitMQClient = RabbitMQClient;
|
|
24
|
+
module.exports.initInfrastructureQueues = initInfrastructureQueues;
|
|
23
25
|
module.exports.errors = {
|
|
24
26
|
ValidationError,
|
|
25
27
|
ConnectionError,
|
|
@@ -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
|
+
|