@zintrust/workers 0.1.27

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.
Files changed (178) hide show
  1. package/README.md +861 -0
  2. package/dist/AnomalyDetection.d.ts +102 -0
  3. package/dist/AnomalyDetection.js +321 -0
  4. package/dist/AutoScaler.d.ts +127 -0
  5. package/dist/AutoScaler.js +425 -0
  6. package/dist/BroadcastWorker.d.ts +21 -0
  7. package/dist/BroadcastWorker.js +24 -0
  8. package/dist/CanaryController.d.ts +103 -0
  9. package/dist/CanaryController.js +380 -0
  10. package/dist/ChaosEngineering.d.ts +79 -0
  11. package/dist/ChaosEngineering.js +216 -0
  12. package/dist/CircuitBreaker.d.ts +106 -0
  13. package/dist/CircuitBreaker.js +374 -0
  14. package/dist/ClusterLock.d.ts +90 -0
  15. package/dist/ClusterLock.js +385 -0
  16. package/dist/ComplianceManager.d.ts +177 -0
  17. package/dist/ComplianceManager.js +556 -0
  18. package/dist/DatacenterOrchestrator.d.ts +133 -0
  19. package/dist/DatacenterOrchestrator.js +404 -0
  20. package/dist/DeadLetterQueue.d.ts +122 -0
  21. package/dist/DeadLetterQueue.js +539 -0
  22. package/dist/HealthMonitor.d.ts +42 -0
  23. package/dist/HealthMonitor.js +301 -0
  24. package/dist/MultiQueueWorker.d.ts +89 -0
  25. package/dist/MultiQueueWorker.js +277 -0
  26. package/dist/NotificationWorker.d.ts +21 -0
  27. package/dist/NotificationWorker.js +23 -0
  28. package/dist/Observability.d.ts +153 -0
  29. package/dist/Observability.js +530 -0
  30. package/dist/PluginManager.d.ts +123 -0
  31. package/dist/PluginManager.js +392 -0
  32. package/dist/PriorityQueue.d.ts +117 -0
  33. package/dist/PriorityQueue.js +244 -0
  34. package/dist/ResourceMonitor.d.ts +164 -0
  35. package/dist/ResourceMonitor.js +605 -0
  36. package/dist/SLAMonitor.d.ts +110 -0
  37. package/dist/SLAMonitor.js +274 -0
  38. package/dist/WorkerFactory.d.ts +193 -0
  39. package/dist/WorkerFactory.js +1507 -0
  40. package/dist/WorkerInit.d.ts +85 -0
  41. package/dist/WorkerInit.js +223 -0
  42. package/dist/WorkerMetrics.d.ts +114 -0
  43. package/dist/WorkerMetrics.js +509 -0
  44. package/dist/WorkerRegistry.d.ts +145 -0
  45. package/dist/WorkerRegistry.js +319 -0
  46. package/dist/WorkerShutdown.d.ts +61 -0
  47. package/dist/WorkerShutdown.js +159 -0
  48. package/dist/WorkerVersioning.d.ts +107 -0
  49. package/dist/WorkerVersioning.js +300 -0
  50. package/dist/build-manifest.json +462 -0
  51. package/dist/config/workerConfig.d.ts +3 -0
  52. package/dist/config/workerConfig.js +19 -0
  53. package/dist/createQueueWorker.d.ts +23 -0
  54. package/dist/createQueueWorker.js +113 -0
  55. package/dist/dashboard/index.d.ts +1 -0
  56. package/dist/dashboard/index.js +1 -0
  57. package/dist/dashboard/types.d.ts +117 -0
  58. package/dist/dashboard/types.js +1 -0
  59. package/dist/dashboard/workers-api.d.ts +4 -0
  60. package/dist/dashboard/workers-api.js +638 -0
  61. package/dist/dashboard/workers-dashboard-ui.d.ts +3 -0
  62. package/dist/dashboard/workers-dashboard-ui.js +1026 -0
  63. package/dist/dashboard/workers-dashboard.d.ts +4 -0
  64. package/dist/dashboard/workers-dashboard.js +904 -0
  65. package/dist/helper/index.d.ts +5 -0
  66. package/dist/helper/index.js +10 -0
  67. package/dist/http/WorkerApiController.d.ts +38 -0
  68. package/dist/http/WorkerApiController.js +312 -0
  69. package/dist/http/WorkerController.d.ts +374 -0
  70. package/dist/http/WorkerController.js +1351 -0
  71. package/dist/http/middleware/CustomValidation.d.ts +92 -0
  72. package/dist/http/middleware/CustomValidation.js +270 -0
  73. package/dist/http/middleware/DatacenterValidator.d.ts +3 -0
  74. package/dist/http/middleware/DatacenterValidator.js +94 -0
  75. package/dist/http/middleware/EditWorkerValidation.d.ts +7 -0
  76. package/dist/http/middleware/EditWorkerValidation.js +55 -0
  77. package/dist/http/middleware/FeaturesValidator.d.ts +3 -0
  78. package/dist/http/middleware/FeaturesValidator.js +60 -0
  79. package/dist/http/middleware/InfrastructureValidator.d.ts +31 -0
  80. package/dist/http/middleware/InfrastructureValidator.js +226 -0
  81. package/dist/http/middleware/OptionsValidator.d.ts +3 -0
  82. package/dist/http/middleware/OptionsValidator.js +112 -0
  83. package/dist/http/middleware/PayloadSanitizer.d.ts +7 -0
  84. package/dist/http/middleware/PayloadSanitizer.js +42 -0
  85. package/dist/http/middleware/ProcessorPathSanitizer.d.ts +3 -0
  86. package/dist/http/middleware/ProcessorPathSanitizer.js +74 -0
  87. package/dist/http/middleware/QueueNameSanitizer.d.ts +3 -0
  88. package/dist/http/middleware/QueueNameSanitizer.js +45 -0
  89. package/dist/http/middleware/ValidateDriver.d.ts +7 -0
  90. package/dist/http/middleware/ValidateDriver.js +20 -0
  91. package/dist/http/middleware/VersionSanitizer.d.ts +3 -0
  92. package/dist/http/middleware/VersionSanitizer.js +25 -0
  93. package/dist/http/middleware/WorkerNameSanitizer.d.ts +3 -0
  94. package/dist/http/middleware/WorkerNameSanitizer.js +46 -0
  95. package/dist/http/middleware/WorkerValidationChain.d.ts +27 -0
  96. package/dist/http/middleware/WorkerValidationChain.js +185 -0
  97. package/dist/index.d.ts +46 -0
  98. package/dist/index.js +48 -0
  99. package/dist/routes/workers.d.ts +12 -0
  100. package/dist/routes/workers.js +81 -0
  101. package/dist/storage/WorkerStore.d.ts +45 -0
  102. package/dist/storage/WorkerStore.js +195 -0
  103. package/dist/type.d.ts +76 -0
  104. package/dist/type.js +1 -0
  105. package/dist/ui/router/ui.d.ts +3 -0
  106. package/dist/ui/router/ui.js +83 -0
  107. package/dist/ui/types/worker-ui.d.ts +229 -0
  108. package/dist/ui/types/worker-ui.js +5 -0
  109. package/package.json +53 -0
  110. package/src/AnomalyDetection.ts +434 -0
  111. package/src/AutoScaler.ts +654 -0
  112. package/src/BroadcastWorker.ts +34 -0
  113. package/src/CanaryController.ts +531 -0
  114. package/src/ChaosEngineering.ts +301 -0
  115. package/src/CircuitBreaker.ts +495 -0
  116. package/src/ClusterLock.ts +499 -0
  117. package/src/ComplianceManager.ts +815 -0
  118. package/src/DatacenterOrchestrator.ts +561 -0
  119. package/src/DeadLetterQueue.ts +733 -0
  120. package/src/HealthMonitor.ts +390 -0
  121. package/src/MultiQueueWorker.ts +431 -0
  122. package/src/NotificationWorker.ts +33 -0
  123. package/src/Observability.ts +696 -0
  124. package/src/PluginManager.ts +551 -0
  125. package/src/PriorityQueue.ts +351 -0
  126. package/src/ResourceMonitor.ts +769 -0
  127. package/src/SLAMonitor.ts +408 -0
  128. package/src/WorkerFactory.ts +2108 -0
  129. package/src/WorkerInit.ts +313 -0
  130. package/src/WorkerMetrics.ts +709 -0
  131. package/src/WorkerRegistry.ts +443 -0
  132. package/src/WorkerShutdown.ts +210 -0
  133. package/src/WorkerVersioning.ts +422 -0
  134. package/src/config/workerConfig.ts +25 -0
  135. package/src/createQueueWorker.ts +174 -0
  136. package/src/dashboard/index.ts +6 -0
  137. package/src/dashboard/types.ts +141 -0
  138. package/src/dashboard/workers-api.ts +785 -0
  139. package/src/dashboard/zintrust.svg +30 -0
  140. package/src/helper/index.ts +11 -0
  141. package/src/http/WorkerApiController.ts +369 -0
  142. package/src/http/WorkerController.ts +1512 -0
  143. package/src/http/middleware/CustomValidation.ts +360 -0
  144. package/src/http/middleware/DatacenterValidator.ts +124 -0
  145. package/src/http/middleware/EditWorkerValidation.ts +74 -0
  146. package/src/http/middleware/FeaturesValidator.ts +82 -0
  147. package/src/http/middleware/InfrastructureValidator.ts +295 -0
  148. package/src/http/middleware/OptionsValidator.ts +144 -0
  149. package/src/http/middleware/PayloadSanitizer.ts +52 -0
  150. package/src/http/middleware/ProcessorPathSanitizer.ts +86 -0
  151. package/src/http/middleware/QueueNameSanitizer.ts +55 -0
  152. package/src/http/middleware/ValidateDriver.ts +29 -0
  153. package/src/http/middleware/VersionSanitizer.ts +30 -0
  154. package/src/http/middleware/WorkerNameSanitizer.ts +56 -0
  155. package/src/http/middleware/WorkerValidationChain.ts +230 -0
  156. package/src/index.ts +98 -0
  157. package/src/routes/workers.ts +154 -0
  158. package/src/storage/WorkerStore.ts +240 -0
  159. package/src/type.ts +89 -0
  160. package/src/types/queue-monitor.d.ts +38 -0
  161. package/src/types/queue-redis.d.ts +38 -0
  162. package/src/ui/README.md +13 -0
  163. package/src/ui/components/JsonEditor.js +670 -0
  164. package/src/ui/components/JsonViewer.js +387 -0
  165. package/src/ui/components/WorkerCard.js +178 -0
  166. package/src/ui/components/WorkerExpandPanel.js +257 -0
  167. package/src/ui/components/fetcher.js +42 -0
  168. package/src/ui/components/sla-scorecard.js +32 -0
  169. package/src/ui/components/styles.css +30 -0
  170. package/src/ui/components/table-expander.js +34 -0
  171. package/src/ui/integration/worker-ui-integration.js +565 -0
  172. package/src/ui/router/ui.ts +99 -0
  173. package/src/ui/services/workerApi.js +240 -0
  174. package/src/ui/types/worker-ui.ts +283 -0
  175. package/src/ui/utils/jsonValidator.js +444 -0
  176. package/src/ui/workers/index.html +202 -0
  177. package/src/ui/workers/main.js +1781 -0
  178. package/src/ui/workers/styles.css +1350 -0
@@ -0,0 +1,539 @@
1
+ /**
2
+ * Dead Letter Queue Manager
3
+ * Failed job handling with compliance tracking (GDPR/HIPAA/SOC2)
4
+ * Sealed namespace for immutability
5
+ */
6
+ import { ErrorFactory, Logger, createRedisConnection } from '@zintrust/core';
7
+ // Redis key prefixes
8
+ const DLQ_PREFIX = 'worker:dlq:';
9
+ const AUDIT_PREFIX = 'worker:dlq:audit:';
10
+ // Internal state
11
+ let redisClient = null;
12
+ let retentionPolicy = null;
13
+ let cleanupInterval = null;
14
+ /**
15
+ * Helper: Get DLQ key
16
+ */
17
+ const getDLQKey = (queueName) => {
18
+ return `${DLQ_PREFIX}${queueName}`;
19
+ };
20
+ /**
21
+ * Helper: Get audit key
22
+ */
23
+ const getAuditKey = (failedJobId) => {
24
+ return `${AUDIT_PREFIX}${failedJobId}`;
25
+ };
26
+ /**
27
+ * Helper: Record audit entry
28
+ */
29
+ const recordAuditEntry = async (entry) => {
30
+ if (!redisClient)
31
+ return;
32
+ try {
33
+ const auditKey = getAuditKey(entry.failedJobId);
34
+ const auditData = JSON.stringify(entry);
35
+ // Store in sorted set with timestamp as score
36
+ await redisClient.zadd(auditKey, entry.timestamp.getTime(), auditData);
37
+ // Keep audit logs indefinitely (or per compliance requirements)
38
+ // HIPAA requires 6 years, SOC2 requires 1 year minimum
39
+ const hipaaCompliant = retentionPolicy?.hipaaCompliant ?? false;
40
+ const retentionDays = hipaaCompliant ? 6 * 365 : 365;
41
+ await redisClient.expire(auditKey, retentionDays * 24 * 60 * 60);
42
+ Logger.debug('DLQ audit entry recorded', {
43
+ action: entry.action,
44
+ failedJobId: entry.failedJobId,
45
+ userId: entry.userId,
46
+ });
47
+ }
48
+ catch (error) {
49
+ Logger.error('Failed to record DLQ audit entry', error);
50
+ // Don't throw - audit failure shouldn't break operations
51
+ }
52
+ };
53
+ /**
54
+ * Helper: Anonymize sensitive data
55
+ */
56
+ const anonymizeData = (data) => {
57
+ if (typeof data !== 'object' || data === null) {
58
+ return '[REDACTED]';
59
+ }
60
+ const sensitiveFields = [
61
+ 'email',
62
+ 'phone',
63
+ 'ssn',
64
+ 'password',
65
+ 'creditCard',
66
+ 'address',
67
+ 'name',
68
+ 'firstName',
69
+ 'lastName',
70
+ 'dateOfBirth',
71
+ 'birthDate',
72
+ ];
73
+ const anonymized = { ...data };
74
+ for (const key of Object.keys(anonymized)) {
75
+ const lowerKey = key.toLowerCase();
76
+ if (sensitiveFields.some((field) => lowerKey.includes(field))) {
77
+ anonymized[key] = '[REDACTED]';
78
+ }
79
+ else if (typeof anonymized[key] === 'object' && anonymized[key] !== null) {
80
+ anonymized[key] = anonymizeData(anonymized[key]);
81
+ }
82
+ }
83
+ return anonymized;
84
+ };
85
+ /**
86
+ * Helper: Check retention violations
87
+ */
88
+ const checkRetentionViolation = (failedJobEntry) => {
89
+ if (retentionPolicy?.enabled !== true)
90
+ return false;
91
+ const retentionDays = retentionPolicy.autoDeleteAfterDays ?? retentionPolicy.defaultRetentionDays;
92
+ const cutoffDate = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000);
93
+ return failedJobEntry.failedAt < cutoffDate;
94
+ };
95
+ /**
96
+ * Helper: Cleanup old entries
97
+ */
98
+ const cleanupOldEntries = async () => {
99
+ const client = redisClient;
100
+ const policy = retentionPolicy;
101
+ if (!client || policy?.enabled !== true || policy.autoDeleteAfterDays === undefined) {
102
+ return 0;
103
+ }
104
+ try {
105
+ const cutoffTimestamp = Date.now() - policy.autoDeleteAfterDays * 24 * 60 * 60 * 1000;
106
+ // Find all DLQ keys
107
+ const pattern = `${DLQ_PREFIX}*`;
108
+ const keys = await client.keys(pattern);
109
+ const cleanedCounts = await Promise.all(keys.map(async (key) => {
110
+ const oldEntries = await client.zrangebyscore(key, '-inf', cutoffTimestamp);
111
+ await Promise.all(oldEntries.map(async (entryJson) => {
112
+ const entry = JSON.parse(entryJson);
113
+ if (policy.anonymizeInsteadOfDelete) {
114
+ entry.data = anonymizeData(entry.data);
115
+ entry.complianceFlags.containsPII = false;
116
+ entry.complianceFlags.containsPHI = false;
117
+ await client.zrem(key, entryJson);
118
+ await client.zadd(key, entry.failedAt.getTime(), JSON.stringify(entry));
119
+ Logger.info('Anonymized old DLQ entry', { id: entry.id });
120
+ return entry.id;
121
+ }
122
+ await client.zrem(key, entryJson);
123
+ await recordAuditEntry({
124
+ timestamp: new Date(),
125
+ action: 'delete',
126
+ failedJobId: entry.id,
127
+ userId: 'system',
128
+ reason: 'Automatic retention policy cleanup',
129
+ result: 'success',
130
+ });
131
+ Logger.info('Deleted old DLQ entry', { id: entry.id });
132
+ return entry.id;
133
+ }));
134
+ return oldEntries.length;
135
+ }));
136
+ const totalCleaned = cleanedCounts.reduce((sum, count) => sum + count, 0);
137
+ if (totalCleaned > 0) {
138
+ Logger.info(`DLQ cleanup completed: ${totalCleaned} entries processed`);
139
+ }
140
+ return totalCleaned;
141
+ }
142
+ catch (error) {
143
+ Logger.error('DLQ cleanup failed', error);
144
+ return 0;
145
+ }
146
+ };
147
+ /**
148
+ * Dead Letter Queue Manager - Sealed namespace
149
+ */
150
+ export const DeadLetterQueue = Object.freeze({
151
+ /**
152
+ * Initialize DLQ with Redis and retention policy
153
+ */
154
+ initialize(config, policy) {
155
+ if (redisClient) {
156
+ Logger.warn('DeadLetterQueue already initialized');
157
+ return;
158
+ }
159
+ redisClient = createRedisConnection(config);
160
+ retentionPolicy = policy;
161
+ // Start cleanup interval if auto-delete is enabled
162
+ if (policy.enabled && policy.autoDeleteAfterDays !== undefined) {
163
+ cleanupInterval = setInterval(() => {
164
+ cleanupOldEntries().catch((error) => {
165
+ Logger.error('DLQ cleanup interval failed', error);
166
+ });
167
+ }, 24 * 60 * 60 * 1000); // Run daily
168
+ Logger.info('DLQ cleanup scheduler started', {
169
+ interval: '24 hours',
170
+ autoDeleteAfterDays: policy.autoDeleteAfterDays,
171
+ });
172
+ }
173
+ Logger.info('DeadLetterQueue initialized', { policy });
174
+ },
175
+ /**
176
+ * Add failed job to DLQ
177
+ */
178
+ async addFailedJob(entry) {
179
+ if (!redisClient) {
180
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
181
+ }
182
+ const key = getDLQKey(entry.queueName);
183
+ const score = entry.failedAt.getTime();
184
+ const data = JSON.stringify(entry);
185
+ try {
186
+ await redisClient.zadd(key, score, data);
187
+ // Set expiry based on retention policy
188
+ const policy = retentionPolicy;
189
+ if (policy?.enabled === true && policy.autoDeleteAfterDays !== undefined) {
190
+ const ttl = policy.autoDeleteAfterDays * 24 * 60 * 60 * 2; // 2x for safety
191
+ await redisClient.expire(key, ttl);
192
+ }
193
+ Logger.info('Added failed job to DLQ', {
194
+ id: entry.id,
195
+ queue: entry.queueName,
196
+ worker: entry.workerName,
197
+ error: entry.error.message,
198
+ containsPII: entry.complianceFlags.containsPII,
199
+ containsPHI: entry.complianceFlags.containsPHI,
200
+ });
201
+ await recordAuditEntry({
202
+ timestamp: new Date(),
203
+ action: 'access',
204
+ failedJobId: entry.id,
205
+ userId: 'system',
206
+ reason: 'Job failed and added to DLQ',
207
+ result: 'success',
208
+ });
209
+ }
210
+ catch (error) {
211
+ Logger.error('Failed to add job to DLQ', error);
212
+ throw error;
213
+ }
214
+ },
215
+ /**
216
+ * Get failed job by ID
217
+ */
218
+ async getFailedJob(queueName, jobId, userId, reason) {
219
+ if (!redisClient) {
220
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
221
+ }
222
+ try {
223
+ const key = getDLQKey(queueName);
224
+ const entries = await redisClient.zrange(key, 0, -1);
225
+ const entry = entries.map((e) => JSON.parse(e)).find((e) => e.id === jobId);
226
+ if (entry) {
227
+ await recordAuditEntry({
228
+ timestamp: new Date(),
229
+ action: 'access',
230
+ failedJobId: jobId,
231
+ userId,
232
+ reason,
233
+ dataAccessed: Object.keys(entry.data ?? {}),
234
+ result: 'success',
235
+ });
236
+ }
237
+ return entry ?? null;
238
+ }
239
+ catch (error) {
240
+ Logger.error(`Failed to get DLQ entry: ${jobId}`, error);
241
+ await recordAuditEntry({
242
+ timestamp: new Date(),
243
+ action: 'access',
244
+ failedJobId: jobId,
245
+ userId,
246
+ reason,
247
+ result: 'failure',
248
+ errorMessage: error.message,
249
+ });
250
+ return null;
251
+ }
252
+ },
253
+ /**
254
+ * Get all failed jobs for a queue
255
+ */
256
+ async getFailedJobs(queueName, limit = 100) {
257
+ if (!redisClient) {
258
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
259
+ }
260
+ try {
261
+ const key = getDLQKey(queueName);
262
+ // Get most recent failures first (highest scores)
263
+ const entries = await redisClient.zrevrange(key, 0, limit - 1);
264
+ return entries.map((e) => JSON.parse(e));
265
+ }
266
+ catch (error) {
267
+ Logger.error(`Failed to get DLQ entries for queue: ${queueName}`, error);
268
+ return [];
269
+ }
270
+ },
271
+ /**
272
+ * Retry a failed job
273
+ */
274
+ async retry(queueName, jobId, userId, reason) {
275
+ if (!redisClient) {
276
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
277
+ }
278
+ try {
279
+ const entry = await DeadLetterQueue.getFailedJob(queueName, jobId, userId, reason);
280
+ if (!entry) {
281
+ Logger.warn(`Failed job not found for retry: ${jobId}`);
282
+ return false;
283
+ }
284
+ // Remove from DLQ
285
+ const key = getDLQKey(queueName);
286
+ const entryJson = JSON.stringify(entry);
287
+ await redisClient.zrem(key, entryJson);
288
+ await recordAuditEntry({
289
+ timestamp: new Date(),
290
+ action: 'retry',
291
+ failedJobId: jobId,
292
+ userId,
293
+ reason,
294
+ result: 'success',
295
+ });
296
+ Logger.info(`Failed job marked for retry: ${jobId}`, { userId, reason });
297
+ return true;
298
+ }
299
+ catch (error) {
300
+ Logger.error(`Failed to retry job: ${jobId}`, error);
301
+ await recordAuditEntry({
302
+ timestamp: new Date(),
303
+ action: 'retry',
304
+ failedJobId: jobId,
305
+ userId,
306
+ reason,
307
+ result: 'failure',
308
+ errorMessage: error.message,
309
+ });
310
+ return false;
311
+ }
312
+ },
313
+ /**
314
+ * Delete a failed job (GDPR right to deletion)
315
+ */
316
+ async deleteFailedJob(queueName, jobId, userId, reason) {
317
+ if (!redisClient) {
318
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
319
+ }
320
+ try {
321
+ const entry = await DeadLetterQueue.getFailedJob(queueName, jobId, userId, reason);
322
+ if (!entry) {
323
+ Logger.warn(`Failed job not found for deletion: ${jobId}`);
324
+ return false;
325
+ }
326
+ const key = getDLQKey(queueName);
327
+ const entryJson = JSON.stringify(entry);
328
+ await redisClient.zrem(key, entryJson);
329
+ await recordAuditEntry({
330
+ timestamp: new Date(),
331
+ action: 'delete',
332
+ failedJobId: jobId,
333
+ userId,
334
+ reason,
335
+ result: 'success',
336
+ });
337
+ Logger.info(`Failed job deleted: ${jobId}`, { userId, reason });
338
+ return true;
339
+ }
340
+ catch (error) {
341
+ Logger.error(`Failed to delete job: ${jobId}`, error);
342
+ await recordAuditEntry({
343
+ timestamp: new Date(),
344
+ action: 'delete',
345
+ failedJobId: jobId,
346
+ userId,
347
+ reason,
348
+ result: 'failure',
349
+ errorMessage: error.message,
350
+ });
351
+ return false;
352
+ }
353
+ },
354
+ /**
355
+ * Anonymize a failed job (GDPR/HIPAA compliance)
356
+ */
357
+ async anonymizeFailedJob(queueName, jobId, userId, reason) {
358
+ if (!redisClient) {
359
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
360
+ }
361
+ try {
362
+ const entry = await DeadLetterQueue.getFailedJob(queueName, jobId, userId, reason);
363
+ if (!entry) {
364
+ Logger.warn(`Failed job not found for anonymization: ${jobId}`);
365
+ return false;
366
+ }
367
+ // Anonymize sensitive data
368
+ entry.data = anonymizeData(entry.data);
369
+ entry.complianceFlags.containsPII = false;
370
+ entry.complianceFlags.containsPHI = false;
371
+ // Update in Redis
372
+ const key = getDLQKey(queueName);
373
+ const oldEntryJson = JSON.stringify(entry);
374
+ await redisClient.zrem(key, oldEntryJson);
375
+ await redisClient.zadd(key, entry.failedAt.getTime(), JSON.stringify(entry));
376
+ await recordAuditEntry({
377
+ timestamp: new Date(),
378
+ action: 'anonymize',
379
+ failedJobId: jobId,
380
+ userId,
381
+ reason,
382
+ result: 'success',
383
+ });
384
+ Logger.info(`Failed job anonymized: ${jobId}`, { userId, reason });
385
+ return true;
386
+ }
387
+ catch (error) {
388
+ Logger.error(`Failed to anonymize job: ${jobId}`, error);
389
+ await recordAuditEntry({
390
+ timestamp: new Date(),
391
+ action: 'anonymize',
392
+ failedJobId: jobId,
393
+ userId,
394
+ reason,
395
+ result: 'failure',
396
+ errorMessage: error.message,
397
+ });
398
+ return false;
399
+ }
400
+ },
401
+ /**
402
+ * Get audit log for a failed job
403
+ */
404
+ async getAuditLog(failedJobId, limit = 100) {
405
+ if (!redisClient) {
406
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
407
+ }
408
+ try {
409
+ const auditKey = getAuditKey(failedJobId);
410
+ const entries = await redisClient.zrevrange(auditKey, 0, limit - 1);
411
+ return entries.map((e) => JSON.parse(e));
412
+ }
413
+ catch (error) {
414
+ Logger.error(`Failed to get audit log for: ${failedJobId}`, error);
415
+ return [];
416
+ }
417
+ },
418
+ /**
419
+ * Get DLQ statistics
420
+ */
421
+ async getStats() {
422
+ if (!redisClient) {
423
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
424
+ }
425
+ try {
426
+ const client = redisClient;
427
+ const pattern = `${DLQ_PREFIX}*`;
428
+ const keys = await client.keys(pattern);
429
+ const entriesByQueue = await Promise.all(keys.map(async (key) => {
430
+ const queueName = key.replace(DLQ_PREFIX, '');
431
+ const entries = await client.zrange(key, 0, -1);
432
+ return {
433
+ queueName,
434
+ count: entries.length,
435
+ entries: entries.map((e) => JSON.parse(e)),
436
+ };
437
+ }));
438
+ const stats = {
439
+ totalFailed: 0,
440
+ byQueue: {},
441
+ byWorker: {},
442
+ byErrorType: {},
443
+ oldestFailure: null,
444
+ newestFailure: null,
445
+ averageAttempts: 0,
446
+ retentionViolations: 0,
447
+ };
448
+ let totalAttempts = 0;
449
+ entriesByQueue.forEach(({ queueName, count, entries }) => {
450
+ stats.totalFailed += count;
451
+ stats.byQueue[queueName] = count;
452
+ entries.forEach((entry) => {
453
+ stats.byWorker[entry.workerName] = (stats.byWorker[entry.workerName] || 0) + 1;
454
+ stats.byErrorType[entry.error.name] = (stats.byErrorType[entry.error.name] || 0) + 1;
455
+ totalAttempts += entry.attemptsMade;
456
+ if (!stats.oldestFailure || entry.failedAt < stats.oldestFailure) {
457
+ stats.oldestFailure = entry.failedAt;
458
+ }
459
+ if (!stats.newestFailure || entry.failedAt > stats.newestFailure) {
460
+ stats.newestFailure = entry.failedAt;
461
+ }
462
+ if (checkRetentionViolation(entry)) {
463
+ stats.retentionViolations++;
464
+ }
465
+ });
466
+ });
467
+ stats.averageAttempts = stats.totalFailed > 0 ? totalAttempts / stats.totalFailed : 0;
468
+ return stats;
469
+ }
470
+ catch (error) {
471
+ Logger.error('Failed to get DLQ stats', error);
472
+ throw error;
473
+ }
474
+ },
475
+ /**
476
+ * Export failed jobs (compliance)
477
+ */
478
+ async exportFailedJobs(queueName, userId, reason) {
479
+ if (!redisClient) {
480
+ throw ErrorFactory.createConfigError('DeadLetterQueue not initialized');
481
+ }
482
+ try {
483
+ const entries = await DeadLetterQueue.getFailedJobs(queueName, 1000);
484
+ await recordAuditEntry({
485
+ timestamp: new Date(),
486
+ action: 'export',
487
+ failedJobId: `${queueName}:export`,
488
+ userId,
489
+ reason,
490
+ result: 'success',
491
+ });
492
+ Logger.info('Exported DLQ entries', { queueName, userId, count: entries.length });
493
+ return entries;
494
+ }
495
+ catch (error) {
496
+ Logger.error('Failed to export DLQ entries', error);
497
+ await recordAuditEntry({
498
+ timestamp: new Date(),
499
+ action: 'export',
500
+ failedJobId: `${queueName}:export`,
501
+ userId,
502
+ reason,
503
+ result: 'failure',
504
+ errorMessage: error.message,
505
+ });
506
+ return [];
507
+ }
508
+ },
509
+ /**
510
+ * Update retention policy
511
+ */
512
+ updateRetentionPolicy(policy) {
513
+ retentionPolicy = policy;
514
+ Logger.info('DLQ retention policy updated', { policy });
515
+ },
516
+ /**
517
+ * Get current retention policy
518
+ */
519
+ getRetentionPolicy() {
520
+ return retentionPolicy ? { ...retentionPolicy } : null;
521
+ },
522
+ /**
523
+ * Shutdown DLQ manager
524
+ */
525
+ async shutdown() {
526
+ Logger.info('DeadLetterQueue shutting down...');
527
+ if (cleanupInterval) {
528
+ clearInterval(cleanupInterval);
529
+ cleanupInterval = null;
530
+ }
531
+ if (redisClient) {
532
+ await redisClient.quit();
533
+ redisClient = null;
534
+ }
535
+ retentionPolicy = null;
536
+ Logger.info('DeadLetterQueue shutdown complete');
537
+ },
538
+ });
539
+ // Graceful shutdown handled by WorkerShutdown
@@ -0,0 +1,42 @@
1
+ import type { Worker } from 'bullmq';
2
+ export type HealthCheckResult = {
3
+ timestamp: Date;
4
+ status: 'healthy' | 'degraded' | 'critical';
5
+ latency: number;
6
+ message?: string;
7
+ meta?: Record<string, unknown>;
8
+ };
9
+ type HealthMonitorConfig = {
10
+ enabled: boolean;
11
+ tickIntervalMs: number;
12
+ concurrencyLimit: number;
13
+ checkTimeoutMs: number;
14
+ intervalHealthyMs: number;
15
+ intervalSuspectMs: number;
16
+ failureThreshold: number;
17
+ historyLimit: number;
18
+ };
19
+ type WorkerMonitorConfig = {
20
+ degradedCallback?: (name: string, result: HealthCheckResult) => void;
21
+ criticalCallback?: (name: string, result: HealthCheckResult) => void;
22
+ [key: string]: unknown;
23
+ };
24
+ export declare const HealthMonitor: Readonly<{
25
+ configure: (newConfig: Partial<HealthMonitorConfig>) => void;
26
+ register: (name: string, worker: Worker, queueName: string) => void;
27
+ unregister: (name: string) => void;
28
+ start: () => void;
29
+ stop: () => void;
30
+ startMonitoring: (name: string, monitorConfig?: WorkerMonitorConfig) => void;
31
+ stopMonitoring: (name: string) => void;
32
+ updateConfig: (name: string, monitorConfig: WorkerMonitorConfig) => void;
33
+ getCurrentHealth: (name: string) => HealthCheckResult | null;
34
+ getHealthHistory: (name: string, limit?: number) => HealthCheckResult[];
35
+ getHealthTrend: (name: string) => {
36
+ uptime: number;
37
+ samples: number;
38
+ };
39
+ getSummary: () => Promise<unknown>;
40
+ shutdown: () => void;
41
+ }>;
42
+ export {};