@onlineapps/infrastructure-tools 1.0.4 → 1.0.5

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/infrastructure-tools",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Infrastructure orchestration utilities for OA Drive infrastructure services (health tracking, queue initialization, service discovery)",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -28,4 +28,3 @@
28
28
  "access": "public"
29
29
  }
30
30
  }
31
-
@@ -12,6 +12,8 @@
12
12
  * - Custom publish function
13
13
  */
14
14
 
15
+ const { createLogger } = require('../utils/logger');
16
+
15
17
  /**
16
18
  * Create infrastructure health publisher
17
19
  * @param {Object} options - Configuration options
@@ -32,9 +34,12 @@ function createHealthPublisher(options) {
32
34
  publishFunction,
33
35
  getHealthData,
34
36
  config = {},
35
- logger = console
37
+ logger: customLogger
36
38
  } = options;
37
39
 
40
+ // Use shared logger utility for consistent logging
41
+ const logger = createLogger(customLogger);
42
+
38
43
  if (!serviceName) {
39
44
  throw new Error('serviceName is required');
40
45
  }
@@ -71,11 +76,19 @@ function createHealthPublisher(options) {
71
76
  components: getHealthData()
72
77
  };
73
78
 
79
+ logger.info(`[InfrastructureHealth:${serviceName}] Publishing health check...`, {
80
+ queue: queueName,
81
+ serviceName,
82
+ status: healthData.status,
83
+ timestamp: healthData.timestamp
84
+ });
85
+
74
86
  await publishFunction(queueName, healthData);
75
87
 
76
- logger.debug(`[InfrastructureHealth:${serviceName}] Published health check`, {
88
+ logger.info(`[InfrastructureHealth:${serviceName}] Published health check`, {
77
89
  queue: queueName,
78
- status: healthData.status
90
+ status: healthData.status,
91
+ timestamp: healthData.timestamp
79
92
  });
80
93
  } catch (error) {
81
94
  logger.error(`[InfrastructureHealth:${serviceName}] Failed to publish health check`, {
@@ -151,12 +164,44 @@ function createHealthPublisher(options) {
151
164
  * @param {Object} logger - Logger instance
152
165
  * @returns {Object} Health publisher instance
153
166
  */
154
- function createBaseClientAdapter(baseClient, serviceName, getHealthData, config, logger) {
167
+ function createBaseClientAdapter(baseClient, serviceName, getHealthData, config, customLogger) {
168
+ // Use shared logger utility for consistent logging
169
+ const logger = createLogger(customLogger);
155
170
  const publishFunction = async (queueName, data) => {
171
+ logger.info(`[InfrastructureHealth:${serviceName}] [PUBLISH] Starting publish to queue '${queueName}'...`, {
172
+ queueName,
173
+ serviceName,
174
+ status: data.status,
175
+ timestamp: data.timestamp
176
+ });
177
+
156
178
  if (!baseClient || typeof baseClient.publish !== 'function') {
157
- throw new Error('BaseClient instance must have publish() method');
179
+ const error = new Error('BaseClient instance must have publish() method');
180
+ logger.error(`[InfrastructureHealth:${serviceName}] [PUBLISH] ✗ BaseClient invalid`, {
181
+ queueName,
182
+ error: error.message,
183
+ hasBaseClient: !!baseClient,
184
+ hasPublish: baseClient && typeof baseClient.publish === 'function'
185
+ });
186
+ throw error;
187
+ }
188
+
189
+ try {
190
+ await baseClient.publish(queueName, data);
191
+ logger.info(`[InfrastructureHealth:${serviceName}] [PUBLISH] ✓ Message sent to queue '${queueName}'`, {
192
+ queueName,
193
+ serviceName,
194
+ status: data.status
195
+ });
196
+ } catch (error) {
197
+ logger.error(`[InfrastructureHealth:${serviceName}] [PUBLISH] ✗ Failed to publish health check`, {
198
+ queueName,
199
+ error: error.message,
200
+ stack: error.stack,
201
+ code: error.code
202
+ });
203
+ throw error;
158
204
  }
159
- await baseClient.publish(queueName, data);
160
205
  };
161
206
 
162
207
  return createHealthPublisher({
@@ -178,18 +223,105 @@ function createBaseClientAdapter(baseClient, serviceName, getHealthData, config,
178
223
  * @param {Object} logger - Logger instance
179
224
  * @returns {Object} Health publisher instance
180
225
  */
181
- function createAmqplibAdapter(connection, channel, serviceName, getHealthData, config, logger) {
226
+ function createAmqplibAdapter(connection, channel, serviceName, getHealthData, config, customLogger) {
227
+ // Use shared logger utility for consistent logging
228
+ const logger = createLogger(customLogger);
229
+
230
+ // Create a dedicated channel for health checks to avoid conflicts with consumer channels
231
+ let healthCheckChannel = null;
232
+
182
233
  const publishFunction = async (queueName, data) => {
183
- if (!channel || channel.closed) {
184
- throw new Error('AMQP channel is not available or closed');
185
- }
186
- // Ensure queue exists (it should be created by Registry, but assert just in case)
187
- await channel.assertQueue(queueName, { durable: true });
188
- await channel.sendToQueue(
234
+ logger.info(`[InfrastructureHealth:${serviceName}] [PUBLISH] Starting publish to queue '${queueName}'...`, {
189
235
  queueName,
190
- Buffer.from(JSON.stringify(data)),
191
- { persistent: true }
192
- );
236
+ serviceName,
237
+ status: data.status,
238
+ timestamp: data.timestamp
239
+ });
240
+
241
+ // Check connection first
242
+ if (!connection || connection.closed) {
243
+ const error = new Error('AMQP connection is not available or closed');
244
+ logger.error(`[InfrastructureHealth:${serviceName}] [PUBLISH] ✗ Connection not available`, {
245
+ queueName,
246
+ error: error.message
247
+ });
248
+ throw error;
249
+ }
250
+
251
+ // Always use dedicated channel for health checks to avoid conflicts with consumer channels
252
+ // Check if we need to create or recreate the channel
253
+ if (!healthCheckChannel || healthCheckChannel.closed) {
254
+ if (healthCheckChannel && healthCheckChannel.closed) {
255
+ logger.warn(`[InfrastructureHealth:${serviceName}] [PUBLISH] Health check channel closed, recreating...`);
256
+ }
257
+
258
+ try {
259
+ healthCheckChannel = await connection.createChannel();
260
+ logger.info(`[InfrastructureHealth:${serviceName}] [PUBLISH] Created dedicated health check channel`);
261
+ } catch (createErr) {
262
+ logger.error(`[InfrastructureHealth:${serviceName}] [PUBLISH] ✗ Failed to create health check channel`, {
263
+ error: createErr.message
264
+ });
265
+ throw createErr;
266
+ }
267
+ }
268
+
269
+ const activeChannel = healthCheckChannel;
270
+
271
+ try {
272
+ // Queue should already exist (created by Registry), so we don't assert it
273
+ // This avoids 406 PRECONDITION-FAILED errors if queue exists with different parameters
274
+ // Just send the message directly
275
+ logger.debug(`[InfrastructureHealth:${serviceName}] [PUBLISH] Sending message to queue '${queueName}' (queue should already exist)...`);
276
+
277
+ await activeChannel.sendToQueue(
278
+ queueName,
279
+ Buffer.from(JSON.stringify(data)),
280
+ { persistent: true }
281
+ );
282
+
283
+ logger.info(`[InfrastructureHealth:${serviceName}] [PUBLISH] ✓ Message sent to queue '${queueName}'`, {
284
+ queueName,
285
+ serviceName,
286
+ status: data.status
287
+ });
288
+ } catch (error) {
289
+ // If channel was closed during operation, mark it for recreation
290
+ if (error.message && (error.message.includes('Channel closed') || error.message.includes('channel is closed'))) {
291
+ logger.warn(`[InfrastructureHealth:${serviceName}] [PUBLISH] Channel closed during publish, will recreate on next attempt`);
292
+ if (healthCheckChannel) {
293
+ try {
294
+ await healthCheckChannel.close().catch(() => {});
295
+ } catch (closeErr) {
296
+ // Ignore close errors
297
+ }
298
+ healthCheckChannel = null;
299
+ }
300
+ }
301
+
302
+ logger.error(`[InfrastructureHealth:${serviceName}] [PUBLISH] ✗ Failed to publish health check`, {
303
+ queueName,
304
+ error: error.message,
305
+ stack: error.stack,
306
+ code: error.code
307
+ });
308
+ throw error;
309
+ }
310
+ };
311
+
312
+ // Cleanup function to close health check channel
313
+ const cleanup = async () => {
314
+ if (healthCheckChannel && !healthCheckChannel.closed) {
315
+ try {
316
+ await healthCheckChannel.close();
317
+ logger.info(`[InfrastructureHealth:${serviceName}] Closed dedicated health check channel`);
318
+ } catch (closeErr) {
319
+ logger.warn(`[InfrastructureHealth:${serviceName}] Failed to close health check channel`, {
320
+ error: closeErr.message
321
+ });
322
+ }
323
+ healthCheckChannel = null;
324
+ }
193
325
  };
194
326
 
195
327
  return createHealthPublisher({
package/src/index.js CHANGED
@@ -10,8 +10,8 @@
10
10
  * Business services should NOT use this library.
11
11
  */
12
12
 
13
- // Re-export waitForInfrastructureReady from service-common (no duplication)
14
- const { waitForInfrastructureReady } = require('@onlineapps/service-common');
13
+ // Re-export infrastructure readiness utilities from service-common (no duplication)
14
+ const { waitForInfrastructureReady, waitForHealthCheckQueueReady } = require('@onlineapps/service-common');
15
15
 
16
16
  const { initInfrastructureQueues } = require('./orchestration/initInfrastructureQueues');
17
17
  const {
@@ -19,16 +19,21 @@ const {
19
19
  createBaseClientAdapter,
20
20
  createAmqplibAdapter
21
21
  } = require('./health/healthPublisher');
22
+ const { createLogger } = require('./utils/logger');
22
23
 
23
24
  module.exports = {
24
25
  // Orchestration utilities
25
- // waitForInfrastructureReady is re-exported from @onlineapps/service-common
26
+ // waitForInfrastructureReady and waitForHealthCheckQueueReady are re-exported from @onlineapps/service-common
26
27
  waitForInfrastructureReady,
28
+ waitForHealthCheckQueueReady,
27
29
  initInfrastructureQueues,
28
30
 
29
31
  // Health tracking utilities
30
32
  createHealthPublisher,
31
33
  createBaseClientAdapter,
32
- createAmqplibAdapter
34
+ createAmqplibAdapter,
35
+
36
+ // Logger utility (consistent with conn-infra-mq pattern)
37
+ createLogger
33
38
  };
34
39
 
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * logger.js
5
+ *
6
+ * Provides a simple abstraction over console or a custom logger.
7
+ * If the user passes a custom logger object with methods { info, warn, error, debug },
8
+ * these are used; otherwise console.* is used as fallback.
9
+ *
10
+ * Similar to conn-infra-mq logger utility for consistency across infrastructure libraries.
11
+ */
12
+
13
+ function createLogger(customLogger) {
14
+ const methods = ['info', 'warn', 'error', 'debug'];
15
+ if (
16
+ customLogger &&
17
+ typeof customLogger === 'object' &&
18
+ methods.every((fn) => typeof customLogger[fn] === 'function')
19
+ ) {
20
+ // Wrap custom logger to ensure consistent signature
21
+ return {
22
+ info: (...args) => customLogger.info(...args),
23
+ warn: (...args) => customLogger.warn(...args),
24
+ error: (...args) => customLogger.error(...args),
25
+ debug: (...args) => customLogger.debug(...args),
26
+ };
27
+ }
28
+
29
+ // Fallback to console
30
+ return {
31
+ info: (...args) => console.log('[INFO]', ...args),
32
+ warn: (...args) => console.warn('[WARN]', ...args),
33
+ error: (...args) => console.error('[ERROR]', ...args),
34
+ debug: (...args) => console.debug('[DEBUG]', ...args),
35
+ };
36
+ }
37
+
38
+ module.exports = {
39
+ createLogger,
40
+ };
41
+