dt-common-device 13.10.3 → 13.10.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/dist/audit/IAuditProperties.d.ts +2 -1
- package/dist/audit/IAuditProperties.js +1 -0
- package/dist/queue/entities/HybridHttpQueue.d.ts +0 -1
- package/dist/queue/entities/HybridHttpQueue.js +3 -5
- package/dist/queue/utils/queueUtils.d.ts +2 -2
- package/dist/queue/utils/queueUtils.js +5 -24
- package/dist/queue/utils/rateLimit.utils.js +1 -1
- package/package.json +1 -1
|
@@ -20,7 +20,8 @@ export declare enum Resource {
|
|
|
20
20
|
ACCESS_BLE = "accessBle",
|
|
21
21
|
EMERGENCY_CODE = "emergencyCode",
|
|
22
22
|
EMERGENCY_RFID = "emergencyRfid",
|
|
23
|
-
EMERGENCY_BLE = "emergencyBle"
|
|
23
|
+
EMERGENCY_BLE = "emergencyBle",
|
|
24
|
+
SERVER = "server"
|
|
24
25
|
}
|
|
25
26
|
export interface IAuditProperties {
|
|
26
27
|
resource: Resource;
|
|
@@ -54,7 +54,6 @@ let HybridHttpQueue = (() => {
|
|
|
54
54
|
// BullMQ queues and workers for persistent storage
|
|
55
55
|
this.queues = new Map();
|
|
56
56
|
this.workers = new Map();
|
|
57
|
-
this.jobResults = new Map();
|
|
58
57
|
this.rateLimitConfigs = rateLimit_utils_1.RateLimitUtils.initializeRateLimitConfigs();
|
|
59
58
|
}
|
|
60
59
|
async addToQueue(connectionId, provider, url, method, options) {
|
|
@@ -89,7 +88,7 @@ let HybridHttpQueue = (() => {
|
|
|
89
88
|
console.log(`[${new Date().toISOString()}] [HybridHttpQueue] Job added to queue - JobId: ${jobId}, QueueKey: ${queueKey}`);
|
|
90
89
|
// Initialize worker if not exists
|
|
91
90
|
console.log(`[${new Date().toISOString()}] [HybridHttpQueue] Initializing worker for queue: ${queueKey}`);
|
|
92
|
-
queueUtils_1.QueueUtils.getOrCreateWorker(queueKey, this.workers, this.processHttpRequest.bind(this)
|
|
91
|
+
queueUtils_1.QueueUtils.getOrCreateWorker(queueKey, this.workers, this.processHttpRequest.bind(this));
|
|
93
92
|
// Verify worker was created
|
|
94
93
|
if (this.workers.has(queueKey)) {
|
|
95
94
|
console.log(`[${new Date().toISOString()}] [HybridHttpQueue] Worker confirmed for queue: ${queueKey}`);
|
|
@@ -100,7 +99,7 @@ let HybridHttpQueue = (() => {
|
|
|
100
99
|
(0, config_1.getConfig)().LOGGER.info(`Request queued: ${method} ${url} -> ${provider} [${connectionId}]. Job ID: ${jobId}, Delay: ${delay}ms`);
|
|
101
100
|
// Wait for job completion and return result
|
|
102
101
|
// Simple: delay + windowMs + HTTP buffer timeout
|
|
103
|
-
return queueUtils_1.QueueUtils.waitForJobCompletion(jobId, queueKey, delay, windowMs);
|
|
102
|
+
return queueUtils_1.QueueUtils.waitForJobCompletion(jobId, queueKey, delay, windowMs, this.queues);
|
|
104
103
|
}
|
|
105
104
|
async processHttpRequest(job) {
|
|
106
105
|
// Log immediately when worker picks up the job
|
|
@@ -148,7 +147,7 @@ let HybridHttpQueue = (() => {
|
|
|
148
147
|
return this.handleRequest(url, method, httpCallOption);
|
|
149
148
|
}
|
|
150
149
|
async handleRequest(url, method, options) {
|
|
151
|
-
const { connectionId, provider
|
|
150
|
+
const { connectionId, provider } = jobUtils_1.JobUtils.extractConnectionDetails(options);
|
|
152
151
|
// Check rate limit first
|
|
153
152
|
const allowed = await rateLimit_utils_1.RateLimitUtils.isRateLimitAllowed(connectionId, provider, this.rateLimitConfigs);
|
|
154
153
|
if (allowed) {
|
|
@@ -170,7 +169,6 @@ let HybridHttpQueue = (() => {
|
|
|
170
169
|
]);
|
|
171
170
|
this.workers.clear();
|
|
172
171
|
this.queues.clear();
|
|
173
|
-
this.jobResults.clear();
|
|
174
172
|
(0, config_1.getConfig)().LOGGER.info("HTTP queues shutdown complete");
|
|
175
173
|
}
|
|
176
174
|
};
|
|
@@ -4,9 +4,9 @@ export declare class QueueUtils {
|
|
|
4
4
|
static getQueueKey(microservice: string, connectionId: string, provider: string): string;
|
|
5
5
|
static getRequestQueueKey(connectionId: string, provider: string): string;
|
|
6
6
|
static getOrCreateQueue(queueKey: string, queues: Map<string, any>): any;
|
|
7
|
-
static getOrCreateWorker(queueKey: string, workers: Map<string, any>, processFunction: (job: any) => Promise<any
|
|
7
|
+
static getOrCreateWorker(queueKey: string, workers: Map<string, any>, processFunction: (job: any) => Promise<any>): void;
|
|
8
8
|
static waitForRateLimitExpiry(connectionId: string, provider: string, rateLimitConfigs: Map<string, IRateLimitConfig>): Promise<void>;
|
|
9
9
|
static executeHttpRequest(url: string, method: string, options: HttpCallOption, connectionId: string, provider: string): Promise<any>;
|
|
10
10
|
static addJobToQueue(queueKey: string, jobData: any, delay: number, queues: Map<string, any>): Promise<string>;
|
|
11
|
-
static waitForJobCompletion(jobId: string, queueKey: string, jobDelay
|
|
11
|
+
static waitForJobCompletion(jobId: string, queueKey: string, jobDelay: number | undefined, windowMs: number | undefined, queues: Map<string, any>): Promise<any>;
|
|
12
12
|
}
|
|
@@ -24,7 +24,7 @@ class QueueUtils {
|
|
|
24
24
|
}))
|
|
25
25
|
.get(queueKey));
|
|
26
26
|
}
|
|
27
|
-
static getOrCreateWorker(queueKey, workers, processFunction
|
|
27
|
+
static getOrCreateWorker(queueKey, workers, processFunction) {
|
|
28
28
|
if (workers.has(queueKey)) {
|
|
29
29
|
const existingWorker = workers.get(queueKey);
|
|
30
30
|
console.log(`[${new Date().toISOString()}] [getOrCreateWorker] Worker already exists for queue: ${queueKey}, checking if it's running...`);
|
|
@@ -95,24 +95,11 @@ class QueueUtils {
|
|
|
95
95
|
console.log(`[${new Date().toISOString()}] [Worker Event] Job completed - JobId: ${job.id}, QueueKey: ${queueKey}, ReturnValueType: ${typeof job.returnvalue}, HasReturnValue: ${job.returnvalue !== undefined}`);
|
|
96
96
|
(0, config_1.getConfig)().LOGGER.info(`HTTP request completed: ${job.id} [${queueKey}]`);
|
|
97
97
|
const result = job.returnvalue;
|
|
98
|
-
const jobResult = {
|
|
99
|
-
result,
|
|
100
|
-
resolved: true,
|
|
101
|
-
timestamp: Date.now(),
|
|
102
|
-
};
|
|
103
98
|
console.log(`[${new Date().toISOString()}] [Worker Event] Storing job result - JobId: ${job.id}, ResultType: ${typeof result}, Resolved: true`);
|
|
104
|
-
jobResults.set(job.id, jobResult);
|
|
105
|
-
console.log(`[${new Date().toISOString()}] [Worker Event] Job result stored in jobResults - JobId: ${job.id}, StoredResult: ${JSON.stringify(jobResult)}`);
|
|
106
99
|
});
|
|
107
100
|
worker.on("failed", (job, err) => {
|
|
108
101
|
console.log(`[${new Date().toISOString()}] [Worker Event] Job failed - JobId: ${job?.id}, QueueKey: ${queueKey}, Error: ${err.message}`);
|
|
109
102
|
(0, config_1.getConfig)().LOGGER.error(`HTTP request failed: ${job?.id} [${queueKey}], Error: ${err.message}`);
|
|
110
|
-
jobResults.set(job.id, {
|
|
111
|
-
error: err.message,
|
|
112
|
-
resolved: true,
|
|
113
|
-
timestamp: Date.now(),
|
|
114
|
-
});
|
|
115
|
-
console.log(`[${new Date().toISOString()}] [Worker Event] Job error stored in jobResults - JobId: ${job?.id}`);
|
|
116
103
|
});
|
|
117
104
|
worker.on("error", (err) => {
|
|
118
105
|
console.log(`[${new Date().toISOString()}] [Worker Event] Worker error - QueueKey: ${queueKey}, Error: ${err.message}, Stack: ${err.stack}`);
|
|
@@ -221,16 +208,15 @@ class QueueUtils {
|
|
|
221
208
|
}
|
|
222
209
|
return job.id;
|
|
223
210
|
}
|
|
224
|
-
static async waitForJobCompletion(jobId, queueKey, jobDelay = 0, windowMs = 60000) {
|
|
211
|
+
static async waitForJobCompletion(jobId, queueKey, jobDelay = 0, windowMs = 60000, queues) {
|
|
225
212
|
// Simple: delay (wait) + windowMs (execute) + HTTP timeout buffer
|
|
226
213
|
const httpTimeout = 60000;
|
|
227
214
|
const totalTimeout = jobDelay + windowMs + httpTimeout;
|
|
228
215
|
console.log(`[${new Date().toISOString()}] [waitForJobCompletion] Waiting for job ${jobId} - delay: ${jobDelay}ms, timeout: ${totalTimeout}ms`);
|
|
229
216
|
const startTime = Date.now();
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
});
|
|
217
|
+
// Reuse the existing queue instance — never create a new Queue per call
|
|
218
|
+
// (each new Queue duplicates the Redis connection, causing a connection leak)
|
|
219
|
+
const queue = this.getOrCreateQueue(queueKey, queues);
|
|
234
220
|
return new Promise(async (resolve, reject) => {
|
|
235
221
|
const checkInterval = setInterval(async () => {
|
|
236
222
|
try {
|
|
@@ -239,7 +225,6 @@ class QueueUtils {
|
|
|
239
225
|
const elapsed = Date.now() - startTime;
|
|
240
226
|
if (elapsed > totalTimeout) {
|
|
241
227
|
clearInterval(checkInterval);
|
|
242
|
-
await queue.close();
|
|
243
228
|
return reject(new Error(`Job ${jobId} not found and timeout exceeded`));
|
|
244
229
|
}
|
|
245
230
|
return; // Job not found yet, keep checking
|
|
@@ -249,26 +234,22 @@ class QueueUtils {
|
|
|
249
234
|
console.log(`[${new Date().toISOString()}] [waitForJobCompletion] Job ${jobId} completed`);
|
|
250
235
|
clearInterval(checkInterval);
|
|
251
236
|
const result = job.returnvalue;
|
|
252
|
-
await queue.close();
|
|
253
237
|
return resolve(result);
|
|
254
238
|
}
|
|
255
239
|
if (state === "failed") {
|
|
256
240
|
console.log(`[${new Date().toISOString()}] [waitForJobCompletion] Job ${jobId} failed: ${job.failedReason}`);
|
|
257
241
|
clearInterval(checkInterval);
|
|
258
|
-
await queue.close();
|
|
259
242
|
return reject(new Error(job.failedReason || "Job failed"));
|
|
260
243
|
}
|
|
261
244
|
}
|
|
262
245
|
catch (error) {
|
|
263
246
|
clearInterval(checkInterval);
|
|
264
|
-
await queue.close();
|
|
265
247
|
reject(error);
|
|
266
248
|
}
|
|
267
249
|
}, 500); // Check every 500ms
|
|
268
250
|
// Timeout
|
|
269
251
|
setTimeout(() => {
|
|
270
252
|
clearInterval(checkInterval);
|
|
271
|
-
queue.close();
|
|
272
253
|
reject(new Error(`Request timeout: Maximum wait time exceeded (${totalTimeout}ms). Job delay was ${jobDelay}ms.`));
|
|
273
254
|
}, totalTimeout);
|
|
274
255
|
});
|
|
@@ -76,7 +76,7 @@ class RateLimitUtils {
|
|
|
76
76
|
maxTimeoutWindowMs: 120000,
|
|
77
77
|
});
|
|
78
78
|
configs.set(constants_1.CONNECTION_PROVIDERS.SMARTTHINGS, {
|
|
79
|
-
maxRequests:
|
|
79
|
+
maxRequests: 25,
|
|
80
80
|
windowMs: 60000, // 1 minute
|
|
81
81
|
provider: constants_1.CONNECTION_PROVIDERS.SMARTTHINGS,
|
|
82
82
|
maxTimeoutWindowMs: 120000,
|