@zintrust/workers 0.1.29 → 0.1.30

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 (111) hide show
  1. package/README.md +16 -1
  2. package/dist/AnomalyDetection.d.ts +4 -0
  3. package/dist/AnomalyDetection.js +8 -0
  4. package/dist/BroadcastWorker.d.ts +2 -0
  5. package/dist/CanaryController.js +49 -5
  6. package/dist/ChaosEngineering.js +13 -0
  7. package/dist/ClusterLock.js +21 -10
  8. package/dist/DeadLetterQueue.js +12 -8
  9. package/dist/MultiQueueWorker.d.ts +1 -1
  10. package/dist/MultiQueueWorker.js +12 -7
  11. package/dist/NotificationWorker.d.ts +2 -0
  12. package/dist/PriorityQueue.d.ts +2 -2
  13. package/dist/PriorityQueue.js +20 -21
  14. package/dist/ResourceMonitor.js +65 -38
  15. package/dist/WorkerFactory.d.ts +23 -3
  16. package/dist/WorkerFactory.js +420 -40
  17. package/dist/WorkerInit.js +8 -3
  18. package/dist/WorkerMetrics.d.ts +2 -1
  19. package/dist/WorkerMetrics.js +152 -93
  20. package/dist/WorkerRegistry.d.ts +6 -0
  21. package/dist/WorkerRegistry.js +70 -1
  22. package/dist/WorkerShutdown.d.ts +21 -0
  23. package/dist/WorkerShutdown.js +82 -9
  24. package/dist/WorkerShutdownDurableObject.d.ts +12 -0
  25. package/dist/WorkerShutdownDurableObject.js +41 -0
  26. package/dist/build-manifest.json +171 -99
  27. package/dist/createQueueWorker.d.ts +2 -0
  28. package/dist/createQueueWorker.js +42 -27
  29. package/dist/dashboard/types.d.ts +5 -0
  30. package/dist/dashboard/workers-api.js +136 -43
  31. package/dist/http/WorkerApiController.js +1 -0
  32. package/dist/http/WorkerController.js +133 -85
  33. package/dist/http/WorkerMonitoringService.d.ts +11 -0
  34. package/dist/http/WorkerMonitoringService.js +62 -0
  35. package/dist/http/middleware/CustomValidation.js +1 -1
  36. package/dist/http/middleware/EditWorkerValidation.d.ts +1 -1
  37. package/dist/http/middleware/EditWorkerValidation.js +7 -6
  38. package/dist/http/middleware/ProcessorPathSanitizer.js +101 -35
  39. package/dist/http/middleware/WorkerValidationChain.js +1 -0
  40. package/dist/index.d.ts +2 -1
  41. package/dist/index.js +1 -0
  42. package/dist/routes/workers.js +48 -6
  43. package/dist/storage/WorkerStore.d.ts +4 -1
  44. package/dist/storage/WorkerStore.js +55 -7
  45. package/dist/telemetry/api/TelemetryAPI.d.ts +46 -0
  46. package/dist/telemetry/api/TelemetryAPI.js +219 -0
  47. package/dist/telemetry/api/TelemetryMonitoringService.d.ts +17 -0
  48. package/dist/telemetry/api/TelemetryMonitoringService.js +113 -0
  49. package/dist/telemetry/components/AlertPanel.d.ts +1 -0
  50. package/dist/telemetry/components/AlertPanel.js +13 -0
  51. package/dist/telemetry/components/CostTracking.d.ts +1 -0
  52. package/dist/telemetry/components/CostTracking.js +14 -0
  53. package/dist/telemetry/components/ResourceUsageChart.d.ts +1 -0
  54. package/dist/telemetry/components/ResourceUsageChart.js +11 -0
  55. package/dist/telemetry/components/WorkerHealthChart.d.ts +1 -0
  56. package/dist/telemetry/components/WorkerHealthChart.js +11 -0
  57. package/dist/telemetry/index.d.ts +15 -0
  58. package/dist/telemetry/index.js +60 -0
  59. package/dist/telemetry/routes/dashboard.d.ts +6 -0
  60. package/dist/telemetry/routes/dashboard.js +608 -0
  61. package/dist/ui/router/EmbeddedAssets.d.ts +4 -0
  62. package/dist/ui/router/EmbeddedAssets.js +13 -0
  63. package/dist/ui/router/ui.js +100 -4
  64. package/package.json +10 -6
  65. package/src/AnomalyDetection.ts +9 -0
  66. package/src/CanaryController.ts +41 -5
  67. package/src/ChaosEngineering.ts +14 -0
  68. package/src/ClusterLock.ts +22 -9
  69. package/src/DeadLetterQueue.ts +13 -8
  70. package/src/MultiQueueWorker.ts +15 -8
  71. package/src/PriorityQueue.ts +21 -22
  72. package/src/ResourceMonitor.ts +72 -40
  73. package/src/WorkerFactory.ts +545 -49
  74. package/src/WorkerInit.ts +8 -3
  75. package/src/WorkerMetrics.ts +183 -105
  76. package/src/WorkerRegistry.ts +80 -1
  77. package/src/WorkerShutdown.ts +115 -9
  78. package/src/WorkerShutdownDurableObject.ts +64 -0
  79. package/src/createQueueWorker.ts +73 -30
  80. package/src/dashboard/types.ts +5 -0
  81. package/src/dashboard/workers-api.ts +165 -52
  82. package/src/http/WorkerApiController.ts +1 -0
  83. package/src/http/WorkerController.ts +167 -90
  84. package/src/http/WorkerMonitoringService.ts +77 -0
  85. package/src/http/middleware/CustomValidation.ts +1 -1
  86. package/src/http/middleware/EditWorkerValidation.ts +7 -6
  87. package/src/http/middleware/ProcessorPathSanitizer.ts +123 -36
  88. package/src/http/middleware/WorkerValidationChain.ts +1 -0
  89. package/src/index.ts +6 -1
  90. package/src/routes/workers.ts +66 -9
  91. package/src/storage/WorkerStore.ts +59 -9
  92. package/src/telemetry/api/TelemetryAPI.ts +292 -0
  93. package/src/telemetry/api/TelemetryMonitoringService.ts +149 -0
  94. package/src/telemetry/components/AlertPanel.ts +13 -0
  95. package/src/telemetry/components/CostTracking.ts +14 -0
  96. package/src/telemetry/components/ResourceUsageChart.ts +11 -0
  97. package/src/telemetry/components/WorkerHealthChart.ts +11 -0
  98. package/src/telemetry/index.ts +121 -0
  99. package/src/telemetry/public/assets/zintrust-logo.svg +15 -0
  100. package/src/telemetry/routes/dashboard.ts +638 -0
  101. package/src/telemetry/styles/tailwind.css +1 -0
  102. package/src/telemetry/styles/zintrust-theme.css +8 -0
  103. package/src/ui/router/EmbeddedAssets.ts +13 -0
  104. package/src/ui/router/ui.ts +112 -5
  105. package/src/ui/workers/index.html +2 -2
  106. package/src/ui/workers/main.js +232 -61
  107. package/src/ui/workers/zintrust.svg +30 -0
  108. package/dist/dashboard/workers-dashboard-ui.d.ts +0 -3
  109. package/dist/dashboard/workers-dashboard-ui.js +0 -1026
  110. package/dist/dashboard/workers-dashboard.d.ts +0 -4
  111. package/dist/dashboard/workers-dashboard.js +0 -904
@@ -88,6 +88,15 @@ const safeUptime = (): number => {
88
88
  }
89
89
  };
90
90
 
91
+ const safeProcessCpuUsage = (): NodeJS.CpuUsage => {
92
+ if (!process?.cpuUsage) return { user: 0, system: 0 };
93
+ try {
94
+ return process.cpuUsage();
95
+ } catch {
96
+ return { user: 0, system: 0 };
97
+ }
98
+ };
99
+
91
100
  export type ResourceSnapshot = {
92
101
  timestamp: Date;
93
102
  cpu: {
@@ -182,6 +191,10 @@ let monitoringInterval: NodeJS.Timeout | null = null;
182
191
  const resourceHistory = new Map<string, ResourceSnapshot[]>();
183
192
  const alertHistory = new Map<string, ResourceAlert[]>();
184
193
 
194
+ // Memory management constants
195
+ const MAX_HISTORY_SIZE = 1000; // Keep last 1000 snapshots per worker
196
+ const MAX_ALERT_HISTORY = 100; // Keep last 100 alerts per worker
197
+
185
198
  // Resource thresholds
186
199
  const THRESHOLDS = {
187
200
  cpu: { warning: 70, critical: 90 },
@@ -190,36 +203,64 @@ const THRESHOLDS = {
190
203
  costPerHour: { warning: 10, critical: 50 },
191
204
  };
192
205
 
193
- // Previous CPU usage for delta calculation
194
- let previousCpuUsage: NodeJS.CpuUsage | null = null;
195
- let previousCpuTimestamp: number | null = null;
206
+ // Store previous CPU measurements for calculating real-time usage
207
+ let previousCpuTimes: {
208
+ idle: number;
209
+ total: number;
210
+ timestamp: number;
211
+ } | null = null;
196
212
 
197
213
  /**
198
- * Helper: Calculate CPU usage percentage
214
+ * Helper: Calculate CPU usage percentage (real-time)
199
215
  */
200
216
  const calculateCpuUsage = (): number => {
201
- const currentCpuUsage = process.cpuUsage();
202
- const currentTimestamp = Date.now();
217
+ const os = getOsModule();
218
+ if (!os?.cpus) return 0;
203
219
 
204
- if (previousCpuUsage === null || previousCpuTimestamp === null) {
205
- previousCpuUsage = currentCpuUsage;
206
- previousCpuTimestamp = currentTimestamp;
207
- return 0;
208
- }
220
+ try {
221
+ const cpus = os.cpus();
222
+ let totalIdle = 0;
223
+ let totalTick = 0;
224
+
225
+ cpus.forEach((cpu) => {
226
+ for (const type in cpu.times) {
227
+ totalTick += cpu.times[type as keyof typeof cpu.times];
228
+ }
229
+ totalIdle += cpu.times.idle;
230
+ });
209
231
 
210
- const userDelta = currentCpuUsage.user - previousCpuUsage.user;
211
- const systemDelta = currentCpuUsage.system - previousCpuUsage.system;
212
- const timeDelta = (currentTimestamp - previousCpuTimestamp) * 1000; // Convert ms to microseconds
232
+ const now = Date.now();
213
233
 
214
- previousCpuUsage = currentCpuUsage;
215
- previousCpuTimestamp = currentTimestamp;
234
+ // If we have previous measurements, calculate real-time usage
235
+ if (previousCpuTimes) {
236
+ const timeDiff = now - previousCpuTimes.timestamp;
216
237
 
217
- if (timeDelta === 0) return 0;
238
+ // Only calculate if enough time has passed (minimum 100ms)
239
+ if (timeDiff >= 100) {
240
+ const idleDiff = totalIdle - previousCpuTimes.idle;
241
+ const totalDiff = totalTick - previousCpuTimes.total;
218
242
 
219
- const totalCpuDelta = userDelta + systemDelta;
220
- const cpuPercentage = (totalCpuDelta / timeDelta) * 100;
243
+ // Calculate CPU usage percentage for this interval
244
+ const cpuPercentage = totalDiff > 0 ? ((totalDiff - idleDiff) / totalDiff) * 100 : 0;
221
245
 
222
- return Math.min(100, Math.max(0, cpuPercentage));
246
+ // Update previous measurements
247
+ previousCpuTimes = { idle: totalIdle, total: totalTick, timestamp: now };
248
+
249
+ return Math.min(100, Math.max(0, cpuPercentage));
250
+ }
251
+ }
252
+
253
+ // First measurement or not enough time passed
254
+ previousCpuTimes = { idle: totalIdle, total: totalTick, timestamp: now };
255
+
256
+ // For first call, return a reasonable estimate or use load average
257
+ const loadAvg = safeLoadAverage();
258
+ const cpuCount = safeCpuCount();
259
+ return Math.min(100, Math.max(0, (loadAvg[0] / cpuCount) * 100));
260
+ } catch (error) {
261
+ Logger.error('Failed to calculate system CPU usage', error as Error);
262
+ return 0;
263
+ }
223
264
  };
224
265
 
225
266
  /**
@@ -268,7 +309,7 @@ const captureSnapshot = (): ResourceSnapshot => {
268
309
  pid: process.pid,
269
310
  uptime: process.uptime(),
270
311
  memoryUsage: process.memoryUsage(),
271
- cpuUsage: process.cpuUsage(),
312
+ cpuUsage: safeProcessCpuUsage(),
272
313
  },
273
314
  };
274
315
  };
@@ -439,17 +480,10 @@ const storeAlert = (alert: ResourceAlert): void => {
439
480
 
440
481
  history.push(alert);
441
482
 
442
- // Keep only last 1000 alerts
443
- if (history.length > 1000) {
444
- history.shift();
483
+ // Trim alert history to prevent memory leaks
484
+ if (history.length > MAX_ALERT_HISTORY) {
485
+ alertHistory.set(alert.workerName, history.slice(-MAX_ALERT_HISTORY));
445
486
  }
446
-
447
- Logger.warn(`Resource alert: ${alert.workerName}`, {
448
- type: alert.alertType,
449
- severity: alert.severity,
450
- message: alert.message,
451
- recommendation: alert.recommendation,
452
- });
453
487
  };
454
488
 
455
489
  /**
@@ -500,7 +534,7 @@ const calculateTrend = (
500
534
 
501
535
  // Simple linear regression for trend
502
536
  const firstValue = values[0];
503
- const lastValue = values.at(-1) ?? values[0];
537
+ const lastValue = values[values.length - 1] ?? values[0];
504
538
  const changePercentage = ((lastValue - firstValue) / firstValue) * 100;
505
539
 
506
540
  let trend: ResourceTrend['trend'];
@@ -566,10 +600,6 @@ export const ResourceMonitor = Object.freeze({
566
600
  return;
567
601
  }
568
602
 
569
- // Initial snapshot
570
- previousCpuUsage = process.cpuUsage();
571
- previousCpuTimestamp = Date.now();
572
-
573
603
  monitoringInterval = setInterval(() => {
574
604
  const snapshot = captureSnapshot();
575
605
  // Store snapshot for later analysis
@@ -611,14 +641,16 @@ export const ResourceMonitor = Object.freeze({
611
641
 
612
642
  history.push(snapshot);
613
643
 
614
- // Keep only last 1000 snapshots
615
- if (history.length > 1000) {
616
- history.shift();
644
+ // Trim resource history to prevent memory leaks
645
+ if (history.length > MAX_HISTORY_SIZE) {
646
+ resourceHistory.set(workerName, history.slice(-MAX_HISTORY_SIZE));
617
647
  }
618
648
 
619
649
  // Check thresholds
620
650
  const alerts = checkThresholds(workerName, snapshot, cost);
621
- alerts.forEach(storeAlert);
651
+ alerts.forEach((element) => {
652
+ storeAlert(element);
653
+ });
622
654
 
623
655
  return {
624
656
  workerName,