@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.
- package/README.md +861 -0
- package/dist/AnomalyDetection.d.ts +102 -0
- package/dist/AnomalyDetection.js +321 -0
- package/dist/AutoScaler.d.ts +127 -0
- package/dist/AutoScaler.js +425 -0
- package/dist/BroadcastWorker.d.ts +21 -0
- package/dist/BroadcastWorker.js +24 -0
- package/dist/CanaryController.d.ts +103 -0
- package/dist/CanaryController.js +380 -0
- package/dist/ChaosEngineering.d.ts +79 -0
- package/dist/ChaosEngineering.js +216 -0
- package/dist/CircuitBreaker.d.ts +106 -0
- package/dist/CircuitBreaker.js +374 -0
- package/dist/ClusterLock.d.ts +90 -0
- package/dist/ClusterLock.js +385 -0
- package/dist/ComplianceManager.d.ts +177 -0
- package/dist/ComplianceManager.js +556 -0
- package/dist/DatacenterOrchestrator.d.ts +133 -0
- package/dist/DatacenterOrchestrator.js +404 -0
- package/dist/DeadLetterQueue.d.ts +122 -0
- package/dist/DeadLetterQueue.js +539 -0
- package/dist/HealthMonitor.d.ts +42 -0
- package/dist/HealthMonitor.js +301 -0
- package/dist/MultiQueueWorker.d.ts +89 -0
- package/dist/MultiQueueWorker.js +277 -0
- package/dist/NotificationWorker.d.ts +21 -0
- package/dist/NotificationWorker.js +23 -0
- package/dist/Observability.d.ts +153 -0
- package/dist/Observability.js +530 -0
- package/dist/PluginManager.d.ts +123 -0
- package/dist/PluginManager.js +392 -0
- package/dist/PriorityQueue.d.ts +117 -0
- package/dist/PriorityQueue.js +244 -0
- package/dist/ResourceMonitor.d.ts +164 -0
- package/dist/ResourceMonitor.js +605 -0
- package/dist/SLAMonitor.d.ts +110 -0
- package/dist/SLAMonitor.js +274 -0
- package/dist/WorkerFactory.d.ts +193 -0
- package/dist/WorkerFactory.js +1507 -0
- package/dist/WorkerInit.d.ts +85 -0
- package/dist/WorkerInit.js +223 -0
- package/dist/WorkerMetrics.d.ts +114 -0
- package/dist/WorkerMetrics.js +509 -0
- package/dist/WorkerRegistry.d.ts +145 -0
- package/dist/WorkerRegistry.js +319 -0
- package/dist/WorkerShutdown.d.ts +61 -0
- package/dist/WorkerShutdown.js +159 -0
- package/dist/WorkerVersioning.d.ts +107 -0
- package/dist/WorkerVersioning.js +300 -0
- package/dist/build-manifest.json +462 -0
- package/dist/config/workerConfig.d.ts +3 -0
- package/dist/config/workerConfig.js +19 -0
- package/dist/createQueueWorker.d.ts +23 -0
- package/dist/createQueueWorker.js +113 -0
- package/dist/dashboard/index.d.ts +1 -0
- package/dist/dashboard/index.js +1 -0
- package/dist/dashboard/types.d.ts +117 -0
- package/dist/dashboard/types.js +1 -0
- package/dist/dashboard/workers-api.d.ts +4 -0
- package/dist/dashboard/workers-api.js +638 -0
- package/dist/dashboard/workers-dashboard-ui.d.ts +3 -0
- package/dist/dashboard/workers-dashboard-ui.js +1026 -0
- package/dist/dashboard/workers-dashboard.d.ts +4 -0
- package/dist/dashboard/workers-dashboard.js +904 -0
- package/dist/helper/index.d.ts +5 -0
- package/dist/helper/index.js +10 -0
- package/dist/http/WorkerApiController.d.ts +38 -0
- package/dist/http/WorkerApiController.js +312 -0
- package/dist/http/WorkerController.d.ts +374 -0
- package/dist/http/WorkerController.js +1351 -0
- package/dist/http/middleware/CustomValidation.d.ts +92 -0
- package/dist/http/middleware/CustomValidation.js +270 -0
- package/dist/http/middleware/DatacenterValidator.d.ts +3 -0
- package/dist/http/middleware/DatacenterValidator.js +94 -0
- package/dist/http/middleware/EditWorkerValidation.d.ts +7 -0
- package/dist/http/middleware/EditWorkerValidation.js +55 -0
- package/dist/http/middleware/FeaturesValidator.d.ts +3 -0
- package/dist/http/middleware/FeaturesValidator.js +60 -0
- package/dist/http/middleware/InfrastructureValidator.d.ts +31 -0
- package/dist/http/middleware/InfrastructureValidator.js +226 -0
- package/dist/http/middleware/OptionsValidator.d.ts +3 -0
- package/dist/http/middleware/OptionsValidator.js +112 -0
- package/dist/http/middleware/PayloadSanitizer.d.ts +7 -0
- package/dist/http/middleware/PayloadSanitizer.js +42 -0
- package/dist/http/middleware/ProcessorPathSanitizer.d.ts +3 -0
- package/dist/http/middleware/ProcessorPathSanitizer.js +74 -0
- package/dist/http/middleware/QueueNameSanitizer.d.ts +3 -0
- package/dist/http/middleware/QueueNameSanitizer.js +45 -0
- package/dist/http/middleware/ValidateDriver.d.ts +7 -0
- package/dist/http/middleware/ValidateDriver.js +20 -0
- package/dist/http/middleware/VersionSanitizer.d.ts +3 -0
- package/dist/http/middleware/VersionSanitizer.js +25 -0
- package/dist/http/middleware/WorkerNameSanitizer.d.ts +3 -0
- package/dist/http/middleware/WorkerNameSanitizer.js +46 -0
- package/dist/http/middleware/WorkerValidationChain.d.ts +27 -0
- package/dist/http/middleware/WorkerValidationChain.js +185 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +48 -0
- package/dist/routes/workers.d.ts +12 -0
- package/dist/routes/workers.js +81 -0
- package/dist/storage/WorkerStore.d.ts +45 -0
- package/dist/storage/WorkerStore.js +195 -0
- package/dist/type.d.ts +76 -0
- package/dist/type.js +1 -0
- package/dist/ui/router/ui.d.ts +3 -0
- package/dist/ui/router/ui.js +83 -0
- package/dist/ui/types/worker-ui.d.ts +229 -0
- package/dist/ui/types/worker-ui.js +5 -0
- package/package.json +53 -0
- package/src/AnomalyDetection.ts +434 -0
- package/src/AutoScaler.ts +654 -0
- package/src/BroadcastWorker.ts +34 -0
- package/src/CanaryController.ts +531 -0
- package/src/ChaosEngineering.ts +301 -0
- package/src/CircuitBreaker.ts +495 -0
- package/src/ClusterLock.ts +499 -0
- package/src/ComplianceManager.ts +815 -0
- package/src/DatacenterOrchestrator.ts +561 -0
- package/src/DeadLetterQueue.ts +733 -0
- package/src/HealthMonitor.ts +390 -0
- package/src/MultiQueueWorker.ts +431 -0
- package/src/NotificationWorker.ts +33 -0
- package/src/Observability.ts +696 -0
- package/src/PluginManager.ts +551 -0
- package/src/PriorityQueue.ts +351 -0
- package/src/ResourceMonitor.ts +769 -0
- package/src/SLAMonitor.ts +408 -0
- package/src/WorkerFactory.ts +2108 -0
- package/src/WorkerInit.ts +313 -0
- package/src/WorkerMetrics.ts +709 -0
- package/src/WorkerRegistry.ts +443 -0
- package/src/WorkerShutdown.ts +210 -0
- package/src/WorkerVersioning.ts +422 -0
- package/src/config/workerConfig.ts +25 -0
- package/src/createQueueWorker.ts +174 -0
- package/src/dashboard/index.ts +6 -0
- package/src/dashboard/types.ts +141 -0
- package/src/dashboard/workers-api.ts +785 -0
- package/src/dashboard/zintrust.svg +30 -0
- package/src/helper/index.ts +11 -0
- package/src/http/WorkerApiController.ts +369 -0
- package/src/http/WorkerController.ts +1512 -0
- package/src/http/middleware/CustomValidation.ts +360 -0
- package/src/http/middleware/DatacenterValidator.ts +124 -0
- package/src/http/middleware/EditWorkerValidation.ts +74 -0
- package/src/http/middleware/FeaturesValidator.ts +82 -0
- package/src/http/middleware/InfrastructureValidator.ts +295 -0
- package/src/http/middleware/OptionsValidator.ts +144 -0
- package/src/http/middleware/PayloadSanitizer.ts +52 -0
- package/src/http/middleware/ProcessorPathSanitizer.ts +86 -0
- package/src/http/middleware/QueueNameSanitizer.ts +55 -0
- package/src/http/middleware/ValidateDriver.ts +29 -0
- package/src/http/middleware/VersionSanitizer.ts +30 -0
- package/src/http/middleware/WorkerNameSanitizer.ts +56 -0
- package/src/http/middleware/WorkerValidationChain.ts +230 -0
- package/src/index.ts +98 -0
- package/src/routes/workers.ts +154 -0
- package/src/storage/WorkerStore.ts +240 -0
- package/src/type.ts +89 -0
- package/src/types/queue-monitor.d.ts +38 -0
- package/src/types/queue-redis.d.ts +38 -0
- package/src/ui/README.md +13 -0
- package/src/ui/components/JsonEditor.js +670 -0
- package/src/ui/components/JsonViewer.js +387 -0
- package/src/ui/components/WorkerCard.js +178 -0
- package/src/ui/components/WorkerExpandPanel.js +257 -0
- package/src/ui/components/fetcher.js +42 -0
- package/src/ui/components/sla-scorecard.js +32 -0
- package/src/ui/components/styles.css +30 -0
- package/src/ui/components/table-expander.js +34 -0
- package/src/ui/integration/worker-ui-integration.js +565 -0
- package/src/ui/router/ui.ts +99 -0
- package/src/ui/services/workerApi.js +240 -0
- package/src/ui/types/worker-ui.ts +283 -0
- package/src/ui/utils/jsonValidator.js +444 -0
- package/src/ui/workers/index.html +202 -0
- package/src/ui/workers/main.js +1781 -0
- package/src/ui/workers/styles.css +1350 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Management System Initializer
|
|
3
|
+
*
|
|
4
|
+
* Handles initialization and lifecycle management of the worker management system.
|
|
5
|
+
* - Registers shutdown handlers
|
|
6
|
+
* - Initializes monitoring and resource management
|
|
7
|
+
* - Sets up auto-scaling and health checks
|
|
8
|
+
* - Ensures graceful startup and shutdown
|
|
9
|
+
*/
|
|
10
|
+
export interface IWorkerInitOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Whether to start resource monitoring on initialization
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
enableResourceMonitoring?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Whether to start health monitoring on initialization
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
enableHealthMonitoring?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Whether to start auto-scaling on initialization
|
|
23
|
+
* @default false - must be explicitly enabled
|
|
24
|
+
*/
|
|
25
|
+
enableAutoScaling?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Whether to register graceful shutdown handlers
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
registerShutdownHandlers?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Resource monitoring interval in milliseconds
|
|
33
|
+
* @default 60000 (1 minute)
|
|
34
|
+
*/
|
|
35
|
+
resourceMonitoringInterval?: number;
|
|
36
|
+
}
|
|
37
|
+
interface IInitState {
|
|
38
|
+
initialized: boolean;
|
|
39
|
+
initializedAt: Date | null;
|
|
40
|
+
resourceMonitoring: boolean;
|
|
41
|
+
healthMonitoring: boolean;
|
|
42
|
+
autoScaling: boolean;
|
|
43
|
+
shutdownHandlersRegistered: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Initialize the worker management system
|
|
47
|
+
*/
|
|
48
|
+
declare function initialize(options?: IWorkerInitOptions): Promise<void>;
|
|
49
|
+
declare function autoStartPersistedWorkers(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Check if worker management system is initialized
|
|
52
|
+
*/
|
|
53
|
+
declare function isInitialized(): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Get initialization state
|
|
56
|
+
*/
|
|
57
|
+
declare function getInitState(): Readonly<IInitState>;
|
|
58
|
+
/**
|
|
59
|
+
* Graceful shutdown of worker management system
|
|
60
|
+
* (convenience method that delegates to WorkerShutdown)
|
|
61
|
+
*/
|
|
62
|
+
declare function shutdown(): Promise<void>;
|
|
63
|
+
export declare const WorkerInit: Readonly<{
|
|
64
|
+
/**
|
|
65
|
+
* Initialize the worker management system
|
|
66
|
+
*/
|
|
67
|
+
initialize: typeof initialize;
|
|
68
|
+
/**
|
|
69
|
+
* Check if worker management system is initialized
|
|
70
|
+
*/
|
|
71
|
+
isInitialized: typeof isInitialized;
|
|
72
|
+
/**
|
|
73
|
+
* Get initialization state
|
|
74
|
+
*/
|
|
75
|
+
getInitState: typeof getInitState;
|
|
76
|
+
/**
|
|
77
|
+
* Graceful shutdown of worker management system
|
|
78
|
+
*/
|
|
79
|
+
shutdown: typeof shutdown;
|
|
80
|
+
/**
|
|
81
|
+
* Start persisted workers after boot completes
|
|
82
|
+
*/
|
|
83
|
+
autoStartPersistedWorkers: typeof autoStartPersistedWorkers;
|
|
84
|
+
}>;
|
|
85
|
+
export {};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Management System Initializer
|
|
3
|
+
*
|
|
4
|
+
* Handles initialization and lifecycle management of the worker management system.
|
|
5
|
+
* - Registers shutdown handlers
|
|
6
|
+
* - Initializes monitoring and resource management
|
|
7
|
+
* - Sets up auto-scaling and health checks
|
|
8
|
+
* - Ensures graceful startup and shutdown
|
|
9
|
+
*/
|
|
10
|
+
import { Env, Logger, workersConfig } from '@zintrust/core';
|
|
11
|
+
import { ResourceMonitor } from './ResourceMonitor';
|
|
12
|
+
import { WorkerFactory } from './WorkerFactory';
|
|
13
|
+
import { WorkerShutdown } from './WorkerShutdown';
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// State
|
|
16
|
+
// ============================================================================
|
|
17
|
+
const state = {
|
|
18
|
+
initialized: false,
|
|
19
|
+
initializedAt: null,
|
|
20
|
+
resourceMonitoring: false,
|
|
21
|
+
healthMonitoring: false,
|
|
22
|
+
autoScaling: false,
|
|
23
|
+
shutdownHandlersRegistered: false,
|
|
24
|
+
};
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Implementation
|
|
27
|
+
// ============================================================================
|
|
28
|
+
/**
|
|
29
|
+
* Initialize resource monitoring based on environment and worker settings
|
|
30
|
+
*/
|
|
31
|
+
function initializeResourceMonitoring(enableResourceMonitoring, resourceMonitoringInterval) {
|
|
32
|
+
// Check global environment gate first
|
|
33
|
+
const globalResourceMonitoring = Env.getBool('WORKER_RESOURCE_MONITORING', false);
|
|
34
|
+
if (enableResourceMonitoring && globalResourceMonitoring) {
|
|
35
|
+
// Check if any workers have resourceMonitoring enabled
|
|
36
|
+
const shouldStart = shouldStartResourceMonitoring();
|
|
37
|
+
if (shouldStart) {
|
|
38
|
+
if (ResourceMonitor.isRunning() === false) {
|
|
39
|
+
ResourceMonitor.start(resourceMonitoringInterval / 1000);
|
|
40
|
+
}
|
|
41
|
+
Logger.debug('✓ Resource monitoring started (worker requested)');
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
Logger.debug('⏸️ Resource monitoring disabled (no workers requested it)');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else if (!globalResourceMonitoring) {
|
|
49
|
+
Logger.debug('⏸️ Resource monitoring disabled (WORKER_RESOURCE_MONITORING=false)');
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Check if any workers have resource monitoring enabled
|
|
55
|
+
*/
|
|
56
|
+
function shouldStartResourceMonitoring() {
|
|
57
|
+
try {
|
|
58
|
+
const workerNames = WorkerFactory.list();
|
|
59
|
+
return workerNames.some((name) => {
|
|
60
|
+
const worker = WorkerFactory.get(name);
|
|
61
|
+
return worker?.config?.features?.resourceMonitoring === true;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Initialize the worker management system
|
|
70
|
+
*/
|
|
71
|
+
async function initialize(options = {}) {
|
|
72
|
+
if (state.initialized) {
|
|
73
|
+
Logger.warn('Worker management system already initialized');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const { enableResourceMonitoring = true, enableHealthMonitoring = true, enableAutoScaling = false, registerShutdownHandlers = true, resourceMonitoringInterval = 60000, } = options;
|
|
77
|
+
Logger.info('🚀 Initializing worker management system', {
|
|
78
|
+
enableResourceMonitoring,
|
|
79
|
+
enableHealthMonitoring,
|
|
80
|
+
enableAutoScaling,
|
|
81
|
+
registerShutdownHandlers,
|
|
82
|
+
});
|
|
83
|
+
try {
|
|
84
|
+
// 1. Register shutdown handlers first (so they're ready for any failures)
|
|
85
|
+
if (registerShutdownHandlers) {
|
|
86
|
+
WorkerShutdown.registerShutdownHandlers();
|
|
87
|
+
state.shutdownHandlersRegistered = true;
|
|
88
|
+
Logger.debug('✓ Shutdown handlers registered');
|
|
89
|
+
}
|
|
90
|
+
// 2. Start resource monitoring (important for scaling decisions)
|
|
91
|
+
state.resourceMonitoring = initializeResourceMonitoring(enableResourceMonitoring, resourceMonitoringInterval);
|
|
92
|
+
// 3. Enable health monitoring (depends on workers being created)
|
|
93
|
+
if (enableHealthMonitoring) {
|
|
94
|
+
// Health checks will start automatically when workers are created
|
|
95
|
+
state.healthMonitoring = true;
|
|
96
|
+
Logger.debug('✓ Health monitoring enabled');
|
|
97
|
+
}
|
|
98
|
+
// 4. Start auto-scaling if explicitly enabled
|
|
99
|
+
if (enableAutoScaling) {
|
|
100
|
+
// Auto-scaling will evaluate workers when they are created
|
|
101
|
+
state.autoScaling = true;
|
|
102
|
+
Logger.debug('✓ Auto-scaling enabled');
|
|
103
|
+
}
|
|
104
|
+
state.initialized = true;
|
|
105
|
+
state.initializedAt = new Date();
|
|
106
|
+
Logger.info('✅ Worker management system initialized successfully', {
|
|
107
|
+
timestamp: state.initializedAt.toISOString(),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
Logger.error('❌ Failed to initialize worker management system', error);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function autoStartPersistedWorkers() {
|
|
116
|
+
// Check if auto-start is enabled globally via environment variable
|
|
117
|
+
Logger.debug('Auto-start check', {
|
|
118
|
+
envAutoStart: process.env['WORKER_AUTO_START'],
|
|
119
|
+
configAutoStart: workersConfig.defaultWorker?.autoStart,
|
|
120
|
+
});
|
|
121
|
+
if (workersConfig.defaultWorker?.autoStart !== true) {
|
|
122
|
+
Logger.debug('Auto-start disabled - WORKER_AUTO_START is not true');
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
const records = await WorkerFactory.listPersistedRecords();
|
|
127
|
+
Logger.debug('Found persisted records', {
|
|
128
|
+
count: records.length,
|
|
129
|
+
records: records.map((r) => ({ name: r.name, autoStart: r.autoStart })),
|
|
130
|
+
});
|
|
131
|
+
const candidates = records.filter((record) => {
|
|
132
|
+
// If autoStart is explicitly true, always include
|
|
133
|
+
if (record.autoStart === true) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
// If autoStart is null or undefined and global auto-start is enabled, include
|
|
137
|
+
if ((record.autoStart === null || record.autoStart === undefined) &&
|
|
138
|
+
workersConfig.defaultWorker?.autoStart === true) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
});
|
|
143
|
+
Logger.debug('Auto-start candidates', {
|
|
144
|
+
count: candidates.length,
|
|
145
|
+
candidates: candidates.map((c) => c.name),
|
|
146
|
+
});
|
|
147
|
+
const results = await Promise.all(candidates.map(async (record) => {
|
|
148
|
+
if (WorkerFactory.get(record.name)) {
|
|
149
|
+
return { name: record.name, started: false, skipped: true };
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
await WorkerFactory.startFromPersisted(record.name);
|
|
153
|
+
return { name: record.name, started: true, skipped: false };
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
Logger.warn(`Auto-start failed for worker ${record.name}`, error);
|
|
157
|
+
return { name: record.name, started: false, skipped: false };
|
|
158
|
+
}
|
|
159
|
+
}));
|
|
160
|
+
const startedCount = results.filter((item) => item.started).length;
|
|
161
|
+
const skippedCount = results.filter((item) => item.skipped).length;
|
|
162
|
+
Logger.info('Auto-started persisted workers', {
|
|
163
|
+
total: candidates.length,
|
|
164
|
+
started: startedCount,
|
|
165
|
+
skipped: skippedCount,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
Logger.warn('Auto-start persisted workers failed', error);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Check if worker management system is initialized
|
|
174
|
+
*/
|
|
175
|
+
function isInitialized() {
|
|
176
|
+
return state.initialized;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get initialization state
|
|
180
|
+
*/
|
|
181
|
+
function getInitState() {
|
|
182
|
+
return { ...state };
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Graceful shutdown of worker management system
|
|
186
|
+
* (convenience method that delegates to WorkerShutdown)
|
|
187
|
+
*/
|
|
188
|
+
async function shutdown() {
|
|
189
|
+
if (!state.initialized) {
|
|
190
|
+
Logger.warn('Worker management system not initialized, nothing to shutdown');
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
await WorkerShutdown.shutdown();
|
|
194
|
+
state.initialized = false;
|
|
195
|
+
state.resourceMonitoring = false;
|
|
196
|
+
state.healthMonitoring = false;
|
|
197
|
+
state.autoScaling = false;
|
|
198
|
+
}
|
|
199
|
+
// ============================================================================
|
|
200
|
+
// Public API (Sealed Namespace)
|
|
201
|
+
// ============================================================================
|
|
202
|
+
export const WorkerInit = Object.freeze({
|
|
203
|
+
/**
|
|
204
|
+
* Initialize the worker management system
|
|
205
|
+
*/
|
|
206
|
+
initialize,
|
|
207
|
+
/**
|
|
208
|
+
* Check if worker management system is initialized
|
|
209
|
+
*/
|
|
210
|
+
isInitialized,
|
|
211
|
+
/**
|
|
212
|
+
* Get initialization state
|
|
213
|
+
*/
|
|
214
|
+
getInitState,
|
|
215
|
+
/**
|
|
216
|
+
* Graceful shutdown of worker management system
|
|
217
|
+
*/
|
|
218
|
+
shutdown,
|
|
219
|
+
/**
|
|
220
|
+
* Start persisted workers after boot completes
|
|
221
|
+
*/
|
|
222
|
+
autoStartPersistedWorkers,
|
|
223
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Metrics Manager
|
|
3
|
+
* Time-series metrics persistence with Redis Sorted Sets
|
|
4
|
+
* Sealed namespace for immutability
|
|
5
|
+
*/
|
|
6
|
+
import { type RedisConfig } from '@zintrust/core';
|
|
7
|
+
export type MetricType = 'processed' | 'errors' | 'duration' | 'memory' | 'cpu' | 'queue-size' | 'active-jobs' | 'waiting-jobs' | 'delayed-jobs' | 'failed-jobs' | 'completed-jobs';
|
|
8
|
+
export type MetricGranularity = 'hourly' | 'daily' | 'monthly';
|
|
9
|
+
export type MetricPoint = {
|
|
10
|
+
timestamp: Date;
|
|
11
|
+
value: number;
|
|
12
|
+
metadata?: Record<string, unknown>;
|
|
13
|
+
};
|
|
14
|
+
export type MetricEntry = {
|
|
15
|
+
workerName: string;
|
|
16
|
+
metricType: MetricType;
|
|
17
|
+
granularity: MetricGranularity;
|
|
18
|
+
points: ReadonlyArray<MetricPoint>;
|
|
19
|
+
};
|
|
20
|
+
export type MetricQueryOptions = {
|
|
21
|
+
workerName: string;
|
|
22
|
+
metricType: MetricType;
|
|
23
|
+
granularity: MetricGranularity;
|
|
24
|
+
startDate?: Date;
|
|
25
|
+
endDate?: Date;
|
|
26
|
+
limit?: number;
|
|
27
|
+
};
|
|
28
|
+
export type AggregatedMetrics = {
|
|
29
|
+
workerName: string;
|
|
30
|
+
metricType: MetricType;
|
|
31
|
+
period: {
|
|
32
|
+
start: Date;
|
|
33
|
+
end: Date;
|
|
34
|
+
};
|
|
35
|
+
total: number;
|
|
36
|
+
average: number;
|
|
37
|
+
min: number;
|
|
38
|
+
max: number;
|
|
39
|
+
count: number;
|
|
40
|
+
};
|
|
41
|
+
export type WorkerHealthScore = {
|
|
42
|
+
workerName: string;
|
|
43
|
+
timestamp: Date;
|
|
44
|
+
score: number;
|
|
45
|
+
factors: {
|
|
46
|
+
errorRate: number;
|
|
47
|
+
throughput: number;
|
|
48
|
+
latency: number;
|
|
49
|
+
resourceUsage: number;
|
|
50
|
+
};
|
|
51
|
+
status: 'healthy' | 'degraded' | 'unhealthy';
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Worker Metrics Manager - Sealed namespace
|
|
55
|
+
*/
|
|
56
|
+
export declare const WorkerMetrics: Readonly<{
|
|
57
|
+
/**
|
|
58
|
+
* Initialize the metrics manager with Redis connection
|
|
59
|
+
*/
|
|
60
|
+
initialize(config: RedisConfig): void;
|
|
61
|
+
/**
|
|
62
|
+
* Record a metric point
|
|
63
|
+
*/
|
|
64
|
+
record(workerName: string, metricType: MetricType, value: number, metadata?: Record<string, unknown>): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Record multiple metrics at once (batch operation)
|
|
67
|
+
*/
|
|
68
|
+
recordBatch(workerName: string, metrics: Array<{
|
|
69
|
+
metricType: MetricType;
|
|
70
|
+
value: number;
|
|
71
|
+
metadata?: Record<string, unknown>;
|
|
72
|
+
}>): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Query metrics for a time range
|
|
75
|
+
*/
|
|
76
|
+
query(options: MetricQueryOptions): Promise<MetricEntry>;
|
|
77
|
+
/**
|
|
78
|
+
* Get aggregated metrics for a time range
|
|
79
|
+
*/
|
|
80
|
+
aggregate(options: MetricQueryOptions): Promise<AggregatedMetrics>;
|
|
81
|
+
aggregateBatch(optionsList: MetricQueryOptions[]): Promise<AggregatedMetrics[]>;
|
|
82
|
+
/**
|
|
83
|
+
* Calculate and store health score
|
|
84
|
+
*/
|
|
85
|
+
calculateHealth(workerName: string): Promise<WorkerHealthScore>;
|
|
86
|
+
/**
|
|
87
|
+
* Get recent health scores
|
|
88
|
+
*/
|
|
89
|
+
getHealthHistory(workerName: string, hours?: number): Promise<ReadonlyArray<WorkerHealthScore>>;
|
|
90
|
+
/**
|
|
91
|
+
* Get latest health score
|
|
92
|
+
*/
|
|
93
|
+
getLatestHealth(workerName: string): Promise<WorkerHealthScore | null>;
|
|
94
|
+
/**
|
|
95
|
+
* Get metrics summary for all workers
|
|
96
|
+
*/
|
|
97
|
+
getAllWorkersSummary(): Promise<ReadonlyArray<{
|
|
98
|
+
workerName: string;
|
|
99
|
+
health: WorkerHealthScore | null;
|
|
100
|
+
metrics: {
|
|
101
|
+
processed: number;
|
|
102
|
+
errors: number;
|
|
103
|
+
errorRate: number;
|
|
104
|
+
};
|
|
105
|
+
}>>;
|
|
106
|
+
/**
|
|
107
|
+
* Delete all metrics for a worker
|
|
108
|
+
*/
|
|
109
|
+
deleteWorkerMetrics(workerName: string): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Shutdown and disconnect
|
|
112
|
+
*/
|
|
113
|
+
shutdown(): Promise<void>;
|
|
114
|
+
}>;
|