@onlineapps/service-common 1.0.2 → 1.0.3
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
package/src/index.js
CHANGED
|
@@ -17,6 +17,15 @@ const {
|
|
|
17
17
|
createRedisClient,
|
|
18
18
|
connectRedis
|
|
19
19
|
} = require('./redisClient');
|
|
20
|
+
const {
|
|
21
|
+
requireEnv,
|
|
22
|
+
optionalEnv,
|
|
23
|
+
optionalNumberEnv,
|
|
24
|
+
getCriticalConfig,
|
|
25
|
+
getOptionalInfraConfig,
|
|
26
|
+
getServiceConfig,
|
|
27
|
+
getInfrastructureHealthConfig
|
|
28
|
+
} = require('./runtime-config');
|
|
20
29
|
|
|
21
30
|
module.exports = {
|
|
22
31
|
// Infrastructure readiness utilities (used by both infrastructure and business services)
|
|
@@ -28,6 +37,15 @@ module.exports = {
|
|
|
28
37
|
createRedisClient,
|
|
29
38
|
connectRedis,
|
|
30
39
|
|
|
40
|
+
// Configuration helpers (NO FALLBACKS for critical infrastructure)
|
|
41
|
+
requireEnv,
|
|
42
|
+
optionalEnv,
|
|
43
|
+
optionalNumberEnv,
|
|
44
|
+
getCriticalConfig,
|
|
45
|
+
getOptionalInfraConfig,
|
|
46
|
+
getServiceConfig,
|
|
47
|
+
getInfrastructureHealthConfig,
|
|
48
|
+
|
|
31
49
|
// Reporting utilities
|
|
32
50
|
sendMonitoringFailFallbackEmail
|
|
33
51
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { connectRedis, buildRedisUrl } = require('../redisClient');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* waitForHealthCheckQueueReady.js
|
|
5
7
|
*
|
|
@@ -31,7 +33,9 @@
|
|
|
31
33
|
* @throws {Error} - If timeout is reached
|
|
32
34
|
*/
|
|
33
35
|
async function waitForHealthCheckQueueReady(options = {}) {
|
|
34
|
-
const redisUrl =
|
|
36
|
+
const redisUrl =
|
|
37
|
+
options.redisUrl ||
|
|
38
|
+
buildRedisUrl({ env: process.env, defaults: { host: 'api_node_cache', port: '6379' } });
|
|
35
39
|
const maxWait = options.maxWait || parseInt(process.env.INFRASTRUCTURE_HEALTH_QUEUE_WAIT_MAX_TIME) || 60000; // 1 minute
|
|
36
40
|
const checkInterval = options.checkInterval || parseInt(process.env.INFRASTRUCTURE_HEALTH_QUEUE_WAIT_CHECK_INTERVAL) || 2000; // 2 seconds
|
|
37
41
|
const logger = options.logger || console;
|
|
@@ -55,15 +59,23 @@ async function waitForHealthCheckQueueReady(options = {}) {
|
|
|
55
59
|
log(`[HealthCheckQueueReady] Max wait: ${maxWait}ms, Check interval: ${checkInterval}ms`);
|
|
56
60
|
|
|
57
61
|
try {
|
|
58
|
-
// Connect to Redis
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
// Connect to Redis using shared helper (consistent config + fail-fast behaviour)
|
|
63
|
+
redis = await connectRedis({
|
|
64
|
+
purpose: 'wait-for-health-check-queue-ready',
|
|
65
|
+
logger: {
|
|
66
|
+
info: (message, meta = {}) => {
|
|
67
|
+
const suffix = meta && meta.url ? ` (${meta.url})` : '';
|
|
68
|
+
log(`[HealthCheckQueueReady] ${message}${suffix}`);
|
|
69
|
+
},
|
|
70
|
+
error: (message, meta = {}) => {
|
|
71
|
+
const detail = meta && meta.error ? ` ${meta.error}` : '';
|
|
72
|
+
log(`[HealthCheckQueueReady] ${message}${detail}`);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
timeoutMs: 10000,
|
|
76
|
+
env: process.env,
|
|
77
|
+
defaults: { host: 'api_node_cache', port: '6379' }
|
|
64
78
|
});
|
|
65
|
-
|
|
66
|
-
await redis.connect();
|
|
67
79
|
log('[HealthCheckQueueReady] Connected to Redis');
|
|
68
80
|
|
|
69
81
|
while (Date.now() - startTime < maxWait) {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { connectRedis, buildRedisUrl } = require('../redisClient');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* waitForInfrastructureReady.js
|
|
5
7
|
*
|
|
@@ -42,7 +44,9 @@
|
|
|
42
44
|
* Registry config (config.infrastructureHealth) is the source of truth for these defaults.
|
|
43
45
|
*/
|
|
44
46
|
async function waitForInfrastructureReady(options = {}) {
|
|
45
|
-
const redisUrl =
|
|
47
|
+
const redisUrl =
|
|
48
|
+
options.redisUrl ||
|
|
49
|
+
buildRedisUrl({ env: process.env, defaults: { host: 'api_node_cache', port: '6379' } });
|
|
46
50
|
const maxWait = options.maxWait || parseInt(process.env.INFRASTRUCTURE_HEALTH_WAIT_MAX_TIME) || 300000; // 5 minutes
|
|
47
51
|
const checkInterval = options.checkInterval || parseInt(process.env.INFRASTRUCTURE_HEALTH_WAIT_CHECK_INTERVAL) || 5000; // 5 seconds
|
|
48
52
|
const logger = options.logger || console;
|
|
@@ -72,35 +76,24 @@ async function waitForInfrastructureReady(options = {}) {
|
|
|
72
76
|
log(`[InfrastructureReady] Max wait: ${maxWait}ms, Check interval: ${checkInterval}ms`);
|
|
73
77
|
|
|
74
78
|
try {
|
|
75
|
-
// Connect to Redis with timeout
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const connectPromise = redis.connect();
|
|
92
|
-
const connectTimeoutPromise = new Promise((_, reject) => {
|
|
93
|
-
setTimeout(() => {
|
|
94
|
-
reject(new Error(`Redis connection timeout after ${CONNECT_TIMEOUT}ms. Redis may be unavailable at ${redisUrl}`));
|
|
95
|
-
}, CONNECT_TIMEOUT);
|
|
79
|
+
// Connect to Redis with timeout using shared helper (fail-fast, no infinite reconnect loops)
|
|
80
|
+
redis = await connectRedis({
|
|
81
|
+
purpose: 'wait-for-infrastructure-ready',
|
|
82
|
+
logger: {
|
|
83
|
+
error: (message, meta = {}) => {
|
|
84
|
+
const msg = meta && meta.error ? `${message} ${meta.error}` : message;
|
|
85
|
+
log(`[InfrastructureReady] ${msg}`);
|
|
86
|
+
},
|
|
87
|
+
info: (message, meta = {}) => {
|
|
88
|
+
const suffix = meta && meta.url ? ` (${meta.url})` : '';
|
|
89
|
+
log(`[InfrastructureReady] ${message}${suffix}`);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
timeoutMs: 10000,
|
|
93
|
+
env: process.env,
|
|
94
|
+
defaults: { host: 'api_node_cache', port: '6379' }
|
|
96
95
|
});
|
|
97
|
-
|
|
98
|
-
try {
|
|
99
|
-
await Promise.race([connectPromise, connectTimeoutPromise]);
|
|
100
|
-
log('[InfrastructureReady] Connected to Redis');
|
|
101
|
-
} catch (connectError) {
|
|
102
|
-
throw new Error(`[InfrastructureReady] Failed to connect to Redis: ${connectError.message}`);
|
|
103
|
-
}
|
|
96
|
+
log('[InfrastructureReady] Connected to Redis');
|
|
104
97
|
|
|
105
98
|
while (Date.now() - startTime < maxWait) {
|
|
106
99
|
attemptCount++;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runtime Configuration Helper
|
|
5
|
+
*
|
|
6
|
+
* NO FALLBACKS for critical infrastructure connections.
|
|
7
|
+
* Services must fail fast if critical config is missing.
|
|
8
|
+
*
|
|
9
|
+
* This ensures:
|
|
10
|
+
* - Services don't silently connect to wrong infrastructure
|
|
11
|
+
* - Configuration problems are detected immediately
|
|
12
|
+
* - No hidden dependencies on hardcoded values
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get required environment variable or throw error
|
|
17
|
+
* @param {string} name - Environment variable name
|
|
18
|
+
* @param {string} description - Description for error message
|
|
19
|
+
* @returns {string} Environment variable value
|
|
20
|
+
* @throws {Error} If variable is not set
|
|
21
|
+
*/
|
|
22
|
+
function requireEnv(name, description) {
|
|
23
|
+
const value = process.env[name];
|
|
24
|
+
if (!value || value.trim() === '') {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Service configuration error: ${name} is required but not set. ${description || ''}\n` +
|
|
27
|
+
`Please set ${name} environment variable in .env file or environment.\n` +
|
|
28
|
+
`Example: ${name}=value`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Get optional environment variable with default
|
|
36
|
+
* Use ONLY for non-critical values (ports, timeouts, log levels)
|
|
37
|
+
* @param {string} name - Environment variable name
|
|
38
|
+
* @param {any} defaultValue - Default value if not set
|
|
39
|
+
* @returns {any} Environment variable value or default
|
|
40
|
+
*/
|
|
41
|
+
function optionalEnv(name, defaultValue) {
|
|
42
|
+
const value = process.env[name];
|
|
43
|
+
if (value === undefined || value === null || value === '') {
|
|
44
|
+
return defaultValue;
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get optional number from environment with default
|
|
51
|
+
* @param {string} name - Environment variable name
|
|
52
|
+
* @param {number} defaultValue - Default value if not set or invalid
|
|
53
|
+
* @returns {number} Parsed number or default
|
|
54
|
+
*/
|
|
55
|
+
function optionalNumberEnv(name, defaultValue) {
|
|
56
|
+
const value = process.env[name];
|
|
57
|
+
if (!value || value.trim() === '') {
|
|
58
|
+
return defaultValue;
|
|
59
|
+
}
|
|
60
|
+
const parsed = parseInt(value, 10);
|
|
61
|
+
return Number.isFinite(parsed) ? parsed : defaultValue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get critical infrastructure configuration
|
|
66
|
+
* These MUST be set - no fallbacks allowed
|
|
67
|
+
* @returns {Object} Critical infrastructure config
|
|
68
|
+
*/
|
|
69
|
+
function getCriticalConfig() {
|
|
70
|
+
return {
|
|
71
|
+
REDIS_URL: requireEnv('REDIS_URL', 'Redis connection URL (e.g., redis://api_node_cache:6379)'),
|
|
72
|
+
RABBITMQ_URL: requireEnv('RABBITMQ_URL', 'RabbitMQ connection URL (e.g., amqp://guest:guest@api_services_queuer:5672)')
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Get optional infrastructure configuration
|
|
78
|
+
* These have sensible defaults for development
|
|
79
|
+
* @returns {Object} Optional infrastructure config
|
|
80
|
+
*/
|
|
81
|
+
function getOptionalInfraConfig() {
|
|
82
|
+
return {
|
|
83
|
+
POSTGRES_URL: optionalEnv('POSTGRES_URL', null), // Optional - only for services that need it
|
|
84
|
+
RABBITMQ_MGMT_URL: optionalEnv('RABBITMQ_MGMT_URL', null),
|
|
85
|
+
RABBITMQ_DEFAULT_USER: optionalEnv('RABBITMQ_DEFAULT_USER', 'guest'),
|
|
86
|
+
RABBITMQ_DEFAULT_PASS: optionalEnv('RABBITMQ_DEFAULT_PASS', 'guest'),
|
|
87
|
+
RABBITMQ_VHOST: optionalEnv('RABBITMQ_VHOST', '/')
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get service configuration with defaults
|
|
93
|
+
* @param {Object} options - Options
|
|
94
|
+
* @param {string} [options.defaultPort] - Default port if PORT not set
|
|
95
|
+
* @param {string} [options.defaultLogLevel] - Default log level
|
|
96
|
+
* @returns {Object} Service config
|
|
97
|
+
*/
|
|
98
|
+
function getServiceConfig(options = {}) {
|
|
99
|
+
const { defaultPort = 3000, defaultLogLevel = 'info' } = options;
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
PORT: optionalNumberEnv('PORT', defaultPort),
|
|
103
|
+
LOG_LEVEL: optionalEnv('LOG_LEVEL', defaultLogLevel),
|
|
104
|
+
SERVICE_NAME: optionalEnv('SERVICE_NAME', 'unknown-service'),
|
|
105
|
+
NODE_ENV: optionalEnv('NODE_ENV', 'development')
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Get infrastructure health configuration with defaults
|
|
111
|
+
* @returns {Object} Infrastructure health config
|
|
112
|
+
*/
|
|
113
|
+
function getInfrastructureHealthConfig() {
|
|
114
|
+
return {
|
|
115
|
+
queueName: optionalEnv('INFRASTRUCTURE_HEALTH_QUEUE', 'infrastructure.health.checks'),
|
|
116
|
+
publishInterval: optionalNumberEnv('INFRASTRUCTURE_HEALTH_PUBLISH_INTERVAL', 5000),
|
|
117
|
+
waitMaxTime: optionalNumberEnv('INFRASTRUCTURE_HEALTH_WAIT_MAX_TIME', 300000),
|
|
118
|
+
waitCheckInterval: optionalNumberEnv('INFRASTRUCTURE_HEALTH_WAIT_CHECK_INTERVAL', 5000),
|
|
119
|
+
healthCheckTimeout: optionalNumberEnv('INFRASTRUCTURE_HEALTH_TIMEOUT', 15000),
|
|
120
|
+
redisKeyTTL: optionalNumberEnv('INFRASTRUCTURE_HEALTH_REDIS_TTL', 30),
|
|
121
|
+
cleanupInterval: optionalNumberEnv('INFRASTRUCTURE_HEALTH_CLEANUP_INTERVAL', 10000)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = {
|
|
126
|
+
requireEnv,
|
|
127
|
+
optionalEnv,
|
|
128
|
+
optionalNumberEnv,
|
|
129
|
+
getCriticalConfig,
|
|
130
|
+
getOptionalInfraConfig,
|
|
131
|
+
getServiceConfig,
|
|
132
|
+
getInfrastructureHealthConfig
|
|
133
|
+
};
|
|
134
|
+
|