@gravito/core 1.6.0 → 2.0.0
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 +100 -6
- package/README.zh-TW.md +101 -6
- package/dist/Application.d.ts +256 -0
- package/dist/CommandKernel.d.ts +33 -0
- package/dist/ConfigManager.d.ts +65 -0
- package/dist/Container/RequestScopeManager.d.ts +62 -0
- package/dist/Container/RequestScopeMetrics.d.ts +144 -0
- package/dist/Container.d.ts +153 -0
- package/dist/ErrorHandler.d.ts +66 -0
- package/dist/Event.d.ts +5 -0
- package/dist/EventManager.d.ts +123 -0
- package/dist/GlobalErrorHandlers.d.ts +47 -0
- package/dist/GravitoServer.d.ts +28 -0
- package/dist/HookManager.d.ts +435 -0
- package/dist/Listener.d.ts +4 -0
- package/dist/Logger.d.ts +20 -0
- package/dist/PlanetCore.d.ts +402 -0
- package/dist/RequestContext.d.ts +97 -0
- package/dist/Route.d.ts +36 -0
- package/dist/Router.d.ts +270 -0
- package/dist/ServiceProvider.d.ts +178 -0
- package/dist/adapters/GravitoEngineAdapter.d.ts +27 -0
- package/dist/adapters/bun/AdaptiveAdapter.d.ts +99 -0
- package/dist/adapters/bun/BunContext.d.ts +54 -0
- package/dist/adapters/bun/BunNativeAdapter.d.ts +66 -0
- package/dist/adapters/bun/BunRequest.d.ts +31 -0
- package/dist/adapters/bun/BunWebSocketHandler.d.ts +48 -0
- package/dist/adapters/bun/RadixNode.d.ts +19 -0
- package/dist/adapters/bun/RadixRouter.d.ts +32 -0
- package/dist/adapters/bun/index.d.ts +7 -0
- package/dist/adapters/bun/types.d.ts +20 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/types.d.ts +235 -0
- package/dist/binary/BinaryUtils.d.ts +105 -0
- package/dist/binary/index.d.ts +5 -0
- package/dist/cli/queue-commands.d.ts +6 -0
- package/dist/compat/async-local-storage.browser.d.ts +9 -0
- package/dist/compat/async-local-storage.d.ts +7 -0
- package/dist/compat/crypto.browser.d.ts +5 -0
- package/dist/compat/crypto.d.ts +6 -0
- package/dist/compat.cjs +42 -11
- package/dist/compat.cjs.map +9 -0
- package/dist/compat.d.ts +23 -1
- package/dist/compat.js +3 -0
- package/dist/compat.js.map +9 -0
- package/dist/engine/AOTRouter.d.ts +139 -0
- package/dist/engine/FastContext.d.ts +141 -0
- package/dist/engine/Gravito.d.ts +131 -0
- package/dist/engine/MinimalContext.d.ts +102 -0
- package/dist/engine/analyzer.d.ts +113 -0
- package/dist/engine/constants.d.ts +23 -0
- package/dist/engine/index.cjs +758 -532
- package/dist/engine/index.cjs.map +22 -0
- package/dist/engine/index.d.ts +14 -690
- package/dist/engine/index.js +758 -508
- package/dist/engine/index.js.map +22 -0
- package/dist/engine/path.d.ts +26 -0
- package/dist/engine/pool.d.ts +83 -0
- package/dist/engine/types.d.ts +149 -0
- package/dist/error-handling/RequestScopeErrorContext.d.ts +126 -0
- package/dist/events/BackpressureManager.d.ts +215 -0
- package/dist/events/CircuitBreaker.d.ts +229 -0
- package/dist/events/DeadLetterQueue.d.ts +219 -0
- package/dist/events/EventBackend.d.ts +12 -0
- package/dist/events/EventOptions.d.ts +204 -0
- package/dist/events/EventPriorityQueue.d.ts +63 -0
- package/dist/events/FlowControlStrategy.d.ts +109 -0
- package/dist/events/IdempotencyCache.d.ts +60 -0
- package/dist/events/MessageQueueBridge.d.ts +184 -0
- package/dist/events/PriorityEscalationManager.d.ts +82 -0
- package/dist/events/RetryScheduler.d.ts +104 -0
- package/dist/events/WorkerPool.d.ts +98 -0
- package/dist/events/WorkerPoolConfig.d.ts +153 -0
- package/dist/events/WorkerPoolMetrics.d.ts +65 -0
- package/dist/events/aggregation/AggregationWindow.d.ts +77 -0
- package/dist/events/aggregation/DeduplicationManager.d.ts +135 -0
- package/dist/events/aggregation/EventAggregationManager.d.ts +108 -0
- package/dist/events/aggregation/EventBatcher.d.ts +99 -0
- package/dist/events/aggregation/index.d.ts +10 -0
- package/dist/events/aggregation/types.d.ts +117 -0
- package/dist/events/index.d.ts +26 -0
- package/dist/events/observability/EventMetrics.d.ts +132 -0
- package/dist/events/observability/EventTracer.d.ts +68 -0
- package/dist/events/observability/EventTracing.d.ts +161 -0
- package/dist/events/observability/OTelEventMetrics.d.ts +332 -0
- package/dist/events/observability/ObservableHookManager.d.ts +108 -0
- package/dist/events/observability/StreamWorkerMetrics.d.ts +76 -0
- package/dist/events/observability/index.d.ts +24 -0
- package/dist/events/observability/metrics-types.d.ts +16 -0
- package/dist/events/queue-core.d.ts +77 -0
- package/dist/events/task-executor.d.ts +51 -0
- package/dist/events/types.d.ts +134 -0
- package/dist/exceptions/AuthenticationException.d.ts +8 -0
- package/dist/exceptions/AuthorizationException.d.ts +8 -0
- package/dist/exceptions/CircularDependencyException.d.ts +9 -0
- package/dist/exceptions/GravitoException.d.ts +23 -0
- package/dist/exceptions/HttpException.d.ts +9 -0
- package/dist/exceptions/ModelNotFoundException.d.ts +10 -0
- package/dist/exceptions/ValidationException.d.ts +22 -0
- package/dist/exceptions/index.d.ts +7 -0
- package/dist/ffi/NativeAccelerator.d.ts +69 -0
- package/dist/ffi/NativeHasher.d.ts +139 -0
- package/dist/ffi/cbor-fallback.d.ts +96 -0
- package/dist/ffi/hash-fallback.d.ts +33 -0
- package/dist/ffi/index.cjs +621 -0
- package/dist/ffi/index.cjs.map +14 -0
- package/dist/ffi/index.d.ts +10 -0
- package/dist/ffi/index.js +602 -0
- package/dist/ffi/index.js.map +14 -0
- package/dist/ffi/types.d.ts +135 -0
- package/dist/health/HealthProvider.d.ts +67 -0
- package/dist/helpers/Arr.d.ts +19 -0
- package/dist/helpers/Str.d.ts +38 -0
- package/dist/helpers/data.d.ts +25 -0
- package/dist/helpers/errors.d.ts +34 -0
- package/dist/helpers/response.d.ts +41 -0
- package/dist/helpers.d.ts +338 -0
- package/dist/hooks/ActionManager.d.ts +132 -0
- package/dist/hooks/AsyncDetector.d.ts +84 -0
- package/dist/hooks/FilterManager.d.ts +71 -0
- package/dist/hooks/MigrationWarner.d.ts +24 -0
- package/dist/hooks/dlq-operations.d.ts +60 -0
- package/dist/hooks/index.d.ts +11 -0
- package/dist/hooks/types.d.ts +107 -0
- package/dist/http/CookieJar.d.ts +51 -0
- package/dist/http/cookie.d.ts +29 -0
- package/dist/http/index.d.ts +12 -0
- package/dist/{compat-C4Src6NN.d.cts → http/types.d.ts} +48 -16
- package/dist/index.browser.d.ts +34 -0
- package/dist/index.cjs +12909 -9492
- package/dist/index.cjs.map +168 -0
- package/dist/index.d.ts +58 -8707
- package/dist/index.js +12906 -9381
- package/dist/index.js.map +168 -0
- package/dist/observability/QueueDashboard.d.ts +136 -0
- package/dist/observability/contracts.d.ts +137 -0
- package/dist/observability/index.d.ts +13 -0
- package/dist/reliability/DeadLetterQueueManager.d.ts +349 -0
- package/dist/reliability/RetryPolicy.d.ts +217 -0
- package/dist/reliability/index.d.ts +6 -0
- package/dist/router/ControllerDispatcher.d.ts +12 -0
- package/dist/router/RequestValidator.d.ts +20 -0
- package/dist/runtime/adapter-bun.d.ts +12 -0
- package/dist/runtime/adapter-deno.d.ts +12 -0
- package/dist/runtime/adapter-node.d.ts +12 -0
- package/dist/runtime/adapter-unknown.d.ts +13 -0
- package/dist/runtime/archive.d.ts +17 -0
- package/dist/runtime/compression.d.ts +21 -0
- package/dist/runtime/deep-equals.d.ts +56 -0
- package/dist/runtime/detection.d.ts +22 -0
- package/dist/runtime/escape.d.ts +34 -0
- package/dist/runtime/index.browser.d.ts +20 -0
- package/dist/runtime/index.d.ts +44 -0
- package/dist/runtime/markdown.d.ts +44 -0
- package/dist/runtime/types.d.ts +436 -0
- package/dist/runtime-helpers.d.ts +67 -0
- package/dist/runtime.d.ts +11 -0
- package/dist/security/Encrypter.d.ts +33 -0
- package/dist/security/Hasher.d.ts +29 -0
- package/dist/testing/HttpTester.d.ts +39 -0
- package/dist/testing/TestResponse.d.ts +78 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/transpiler-utils.d.ts +170 -0
- package/dist/types/events.d.ts +94 -0
- package/dist/types.d.ts +13 -0
- package/package.json +23 -53
- package/src/ffi/native/cbor.c +1148 -0
- package/dist/compat-C4Src6NN.d.ts +0 -357
- package/dist/compat.d.cts +0 -1
- package/dist/engine/index.d.cts +0 -702
- package/dist/index.d.cts +0 -8734
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Message Queue Bridge
|
|
3
|
+
*
|
|
4
|
+
* 橋接層,連接 HookManager 與 Bull Queue (via StreamEventBackend)
|
|
5
|
+
* 支持分佈式事件流:HookManager → Bull Queue → Worker → 完成/DLQ
|
|
6
|
+
*/
|
|
7
|
+
import type { HookManager } from '../HookManager';
|
|
8
|
+
import type { DeadLetterQueueManager } from '../reliability/DeadLetterQueueManager';
|
|
9
|
+
import type { EventBackend } from './EventBackend';
|
|
10
|
+
import type { EventTask } from './types';
|
|
11
|
+
import type { WorkerPool } from './WorkerPool';
|
|
12
|
+
/**
|
|
13
|
+
* 事件執行狀態
|
|
14
|
+
*/
|
|
15
|
+
export interface EventStatus {
|
|
16
|
+
eventId: string;
|
|
17
|
+
status: 'pending' | 'processing' | 'completed' | 'failed' | 'in_dlq';
|
|
18
|
+
attempts: number;
|
|
19
|
+
lastError?: string;
|
|
20
|
+
createdAt: number;
|
|
21
|
+
processedAt?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* MessageQueueBridge 配置
|
|
25
|
+
*/
|
|
26
|
+
export interface MessageQueueBridgeConfig {
|
|
27
|
+
/**
|
|
28
|
+
* HookManager 實例
|
|
29
|
+
*/
|
|
30
|
+
hookManager: HookManager;
|
|
31
|
+
/**
|
|
32
|
+
* Worker 線程池(可選)
|
|
33
|
+
*/
|
|
34
|
+
workerPool?: WorkerPool;
|
|
35
|
+
/**
|
|
36
|
+
* 分佈式事件後端(Bull Queue)
|
|
37
|
+
*/
|
|
38
|
+
eventBackend: EventBackend;
|
|
39
|
+
/**
|
|
40
|
+
* Dead Letter Queue 管理器
|
|
41
|
+
*/
|
|
42
|
+
dlqManager: DeadLetterQueueManager;
|
|
43
|
+
/**
|
|
44
|
+
* 是否啟用 CircuitBreaker 檢查
|
|
45
|
+
* @default false
|
|
46
|
+
*/
|
|
47
|
+
enableCircuitBreaker?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* MessageQueueBridge - 連接 HookManager 與 Bull Queue 的橋接層
|
|
51
|
+
*
|
|
52
|
+
* 功能:
|
|
53
|
+
* 1. 通過 Bull Queue 分發事件(dispatchWithQueue)
|
|
54
|
+
* 2. Worker 中處理隊列事件(processQueuedEvent)
|
|
55
|
+
* 3. 失敗任務轉移至 DLQ(handleJobFailure)
|
|
56
|
+
* 4. 查詢事件執行狀態(getEventStatus)
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const bridge = new MessageQueueBridge({
|
|
61
|
+
* hookManager,
|
|
62
|
+
* eventBackend: streamEventBackend,
|
|
63
|
+
* dlqManager,
|
|
64
|
+
* enableCircuitBreaker: true
|
|
65
|
+
* })
|
|
66
|
+
*
|
|
67
|
+
* // 分發事件至 Bull Queue
|
|
68
|
+
* const jobId = await bridge.dispatchWithQueue('order:created', { orderId: 123 })
|
|
69
|
+
*
|
|
70
|
+
* // Worker 處理事件
|
|
71
|
+
* await bridge.processQueuedEvent(jobId, task)
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare class MessageQueueBridge {
|
|
75
|
+
private config;
|
|
76
|
+
constructor(config: MessageQueueBridgeConfig);
|
|
77
|
+
/**
|
|
78
|
+
* 通過 Bull Queue 分發事件(分佈式異步處理)
|
|
79
|
+
*
|
|
80
|
+
* 流程:
|
|
81
|
+
* 1. 驗證事件 listeners 存在
|
|
82
|
+
* 2. 檢查 CircuitBreaker 狀態(如果啟用)
|
|
83
|
+
* 3. 構建 EventTask
|
|
84
|
+
* 4. 入隊到 eventBackend (Bull Queue)
|
|
85
|
+
*
|
|
86
|
+
* @param eventName - 事件名稱
|
|
87
|
+
* @param args - 事件參數
|
|
88
|
+
* @param options - 事件選項(可選)
|
|
89
|
+
* @returns Job ID
|
|
90
|
+
* @throws 如果沒有 listeners 或 CircuitBreaker 打開
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const jobId = await bridge.dispatchWithQueue('order:created', {
|
|
95
|
+
* orderId: 'ORD-123',
|
|
96
|
+
* amount: 999.99
|
|
97
|
+
* })
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
dispatchWithQueue<TArgs = unknown>(eventName: string, args: TArgs, options?: any): Promise<string>;
|
|
101
|
+
/**
|
|
102
|
+
* 處理隊列事件(由 Bull Queue Worker 調用)
|
|
103
|
+
*
|
|
104
|
+
* 重要:使用 doActionSync() 避免遞迴
|
|
105
|
+
* - 不能使用 doActionAsync()(會導致事件再次入隊)
|
|
106
|
+
* - Worker 中直接同步執行 callbacks
|
|
107
|
+
*
|
|
108
|
+
* 流程:
|
|
109
|
+
* 1. 直接執行 callbacks(不使用 doActionSync,避免吞掉錯誤)
|
|
110
|
+
* 2. 記錄成功或失敗
|
|
111
|
+
* 3. 拋出錯誤讓 Bull Queue 處理重試
|
|
112
|
+
*
|
|
113
|
+
* @param jobId - Bull Queue Job ID
|
|
114
|
+
* @param task - 事件任務
|
|
115
|
+
* @throws 如果 callback 執行失敗,讓 Bull Queue 處理重試
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // 在 Bull Queue Worker 中:
|
|
120
|
+
* await bridge.processQueuedEvent(jobId, task)
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
processQueuedEvent(jobId: string, task: EventTask): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
* 處理 Bull Queue 任務失敗(由 onFailed callback 調用)
|
|
126
|
+
*
|
|
127
|
+
* 流程:
|
|
128
|
+
* 1. 記錄警告日誌
|
|
129
|
+
* 2. 移至 persistent DLQ(DeadLetterQueueManager)
|
|
130
|
+
* 3. 持久化到 event_dlq 表,支持延遲重試
|
|
131
|
+
*
|
|
132
|
+
* @param jobId - Bull Queue Job ID
|
|
133
|
+
* @param task - 事件任務
|
|
134
|
+
* @param error - 導致失敗的錯誤
|
|
135
|
+
* @param retryCount - 已重試次數(可選)
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* // 在 Bull Queue onFailed callback 中:
|
|
140
|
+
* await bridge.handleJobFailure(jobId, task, error, 3)
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
handleJobFailure(jobId: string, task: EventTask, error: Error, retryCount?: number): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* 查詢事件執行狀態
|
|
146
|
+
*
|
|
147
|
+
* 支持查詢:
|
|
148
|
+
* 1. 待處理事件
|
|
149
|
+
* 2. 正在處理的事件
|
|
150
|
+
* 3. 已完成事件
|
|
151
|
+
* 4. 失敗事件(在 DLQ 中)
|
|
152
|
+
*
|
|
153
|
+
* @param eventId - 事件 ID(task.id 或 jobId)
|
|
154
|
+
* @returns 事件狀態
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const status = await bridge.getEventStatus('queue-1707000000000-abc123')
|
|
159
|
+
* console.log(status) // { eventId, status, attempts, ... }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
getEventStatus(eventId: string): Promise<EventStatus>;
|
|
163
|
+
/**
|
|
164
|
+
* 獲取 EventBackend 實例(用於高級操作)
|
|
165
|
+
*
|
|
166
|
+
* @returns EventBackend 實例
|
|
167
|
+
* @internal
|
|
168
|
+
*/
|
|
169
|
+
getEventBackend(): EventBackend;
|
|
170
|
+
/**
|
|
171
|
+
* 獲取 DLQ 管理器實例(用於高級操作)
|
|
172
|
+
*
|
|
173
|
+
* @returns DeadLetterQueueManager 實例
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
getDLQManager(): DeadLetterQueueManager;
|
|
177
|
+
/**
|
|
178
|
+
* 獲取 HookManager 實例(用於高級操作)
|
|
179
|
+
*
|
|
180
|
+
* @returns HookManager 實例
|
|
181
|
+
* @internal
|
|
182
|
+
*/
|
|
183
|
+
getHookManager(): HookManager;
|
|
184
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { EventOptions } from './EventOptions';
|
|
2
|
+
import type { EventTask } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* 優先級升級管理器
|
|
5
|
+
*
|
|
6
|
+
* 根據事件等待時間動態調整優先級,實施優先級提升機制。
|
|
7
|
+
* 確保關鍵事件不會因為隊列深度而長時間等待。
|
|
8
|
+
*
|
|
9
|
+
* 升級規則(可配置):
|
|
10
|
+
* - LOW 等待 > 200ms → NORMAL
|
|
11
|
+
* - NORMAL 等待 > 100ms → HIGH
|
|
12
|
+
* - HIGH 等待 > 50ms → CRITICAL
|
|
13
|
+
* - 超過 maxWaitTimeMs → 強制 CRITICAL
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare class PriorityEscalationManager {
|
|
18
|
+
/**
|
|
19
|
+
* 計算事件當前應該的優先級
|
|
20
|
+
* 根據等待時間動態調整
|
|
21
|
+
*/
|
|
22
|
+
static calculateCurrentPriority(task: EventTask, config?: EventOptions['escalation']): 'critical' | 'high' | 'normal' | 'low';
|
|
23
|
+
/**
|
|
24
|
+
* 比較兩個事件的優先級
|
|
25
|
+
* @returns 負數表示 task1 優先級更高,正數表示 task2 優先級更高
|
|
26
|
+
*/
|
|
27
|
+
static comparePriority(task1: EventTask, task2: EventTask): number;
|
|
28
|
+
/**
|
|
29
|
+
* 判斷事件是否應該立即處理(跳過隊列)
|
|
30
|
+
*/
|
|
31
|
+
static shouldProcessImmediately(task: EventTask, config?: EventOptions['escalation']): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* 獲取事件當前的超期時間(毫秒)
|
|
34
|
+
* 正數表示已超期,0 表示未超期
|
|
35
|
+
*/
|
|
36
|
+
static calculateOverdueTime(task: EventTask, config?: EventOptions['escalation']): number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 優先級統計器
|
|
40
|
+
* 追蹤優先級分布和升級事件
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export declare class PriorityStatistics {
|
|
45
|
+
private eventCounts;
|
|
46
|
+
private escalationCounts;
|
|
47
|
+
private totalEscalations;
|
|
48
|
+
/**
|
|
49
|
+
* 記錄一個優先級的事件
|
|
50
|
+
*/
|
|
51
|
+
recordEvent(priority: 'critical' | 'high' | 'normal' | 'low'): void;
|
|
52
|
+
/**
|
|
53
|
+
* 記錄優先級升級
|
|
54
|
+
*/
|
|
55
|
+
recordEscalation(fromPriority: 'critical' | 'high' | 'normal' | 'low', toPriority: 'critical' | 'high' | 'normal' | 'low'): void;
|
|
56
|
+
/**
|
|
57
|
+
* 獲取優先級分布
|
|
58
|
+
*/
|
|
59
|
+
getDistribution(): Record<'critical' | 'high' | 'normal' | 'low', string>;
|
|
60
|
+
/**
|
|
61
|
+
* 獲取升級統計
|
|
62
|
+
*/
|
|
63
|
+
getEscalationStats(): {
|
|
64
|
+
total: number;
|
|
65
|
+
byTransition: Record<string, number>;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* 重置統計信息
|
|
69
|
+
*/
|
|
70
|
+
reset(): void;
|
|
71
|
+
/**
|
|
72
|
+
* 獲取所有統計信息
|
|
73
|
+
*/
|
|
74
|
+
getStats(): {
|
|
75
|
+
eventCounts: Record<'critical' | 'high' | 'normal' | 'low', number>;
|
|
76
|
+
escalationStats: {
|
|
77
|
+
total: number;
|
|
78
|
+
byTransition: Record<string, number>;
|
|
79
|
+
};
|
|
80
|
+
distribution: Record<'critical' | 'high' | 'normal' | 'low', string>;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/core - Retry Scheduler
|
|
3
|
+
*
|
|
4
|
+
* 分佈式重試排程器,使用 Bull Queue 進行異步延遲重試。
|
|
5
|
+
* 支援指數回退、Redis 持久化、重試失敗回呼。
|
|
6
|
+
*
|
|
7
|
+
* Distributed retry scheduler using Bull Queue for async delayed retries.
|
|
8
|
+
* Supports exponential backoff, Redis persistence, and failure callbacks.
|
|
9
|
+
*/
|
|
10
|
+
import type { EventOptions } from './EventOptions';
|
|
11
|
+
/**
|
|
12
|
+
* 重試排程器配置介面
|
|
13
|
+
*/
|
|
14
|
+
export interface RetrySchedulerConfig {
|
|
15
|
+
/** 是否啟用 Bull Queue 重試(預設 true) */
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
/** Redis 連接配置(可選,使用全域 Redis) */
|
|
18
|
+
redisUrl?: string;
|
|
19
|
+
/** 預設最大重試次數(預設 5) */
|
|
20
|
+
maxRetries?: number;
|
|
21
|
+
/** 初始延遲時間(ms,預設 1000) */
|
|
22
|
+
initialDelayMs?: number;
|
|
23
|
+
/** 指數回退倍數(預設 2.0) */
|
|
24
|
+
backoffMultiplier?: number;
|
|
25
|
+
/** 最大延遲時間(ms,預設 1h) */
|
|
26
|
+
maxDelayMs?: number;
|
|
27
|
+
/** 重試失敗回呼 */
|
|
28
|
+
onRetryFailed?: (eventName: string, error: Error, retryCount: number) => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 隊列統計資訊
|
|
32
|
+
*/
|
|
33
|
+
export interface QueueStats {
|
|
34
|
+
name: string;
|
|
35
|
+
jobCounts: {
|
|
36
|
+
waiting: number;
|
|
37
|
+
active: number;
|
|
38
|
+
delayed: number;
|
|
39
|
+
failed: number;
|
|
40
|
+
};
|
|
41
|
+
completedCount: number;
|
|
42
|
+
failedCount: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 重試排程器
|
|
46
|
+
*
|
|
47
|
+
* 提供異步分佈式重試機制,利用 Bull Queue 進行延遲重試。
|
|
48
|
+
* 支援指數回退、Redis 持久化、隊列統計。
|
|
49
|
+
*/
|
|
50
|
+
export declare class RetryScheduler {
|
|
51
|
+
private enabled;
|
|
52
|
+
private config;
|
|
53
|
+
private queues;
|
|
54
|
+
private bullmqModule;
|
|
55
|
+
private bullmqLoadError;
|
|
56
|
+
constructor(config?: RetrySchedulerConfig);
|
|
57
|
+
/**
|
|
58
|
+
* 動態加載 bullmq 模組
|
|
59
|
+
*/
|
|
60
|
+
private loadBullmq;
|
|
61
|
+
/**
|
|
62
|
+
* 檢查排程器是否啟用
|
|
63
|
+
*/
|
|
64
|
+
isEnabled(): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* 計算指數回退延遲時間
|
|
67
|
+
*
|
|
68
|
+
* @param retryCount 當前重試次數(從 0 開始)
|
|
69
|
+
* @returns 延遲時間(毫秒)
|
|
70
|
+
*/
|
|
71
|
+
private calculateDelay;
|
|
72
|
+
/**
|
|
73
|
+
* 獲取或創建隊列
|
|
74
|
+
*
|
|
75
|
+
* @param eventName 事件名稱
|
|
76
|
+
* @returns Queue 實例
|
|
77
|
+
*/
|
|
78
|
+
private getOrCreateQueue;
|
|
79
|
+
/**
|
|
80
|
+
* 排程重試任務
|
|
81
|
+
*
|
|
82
|
+
* @param eventName 事件名稱
|
|
83
|
+
* @param payload 事件負載
|
|
84
|
+
* @param options 事件選項
|
|
85
|
+
* @param error 原始錯誤
|
|
86
|
+
* @param retryCount 當前重試次數
|
|
87
|
+
*/
|
|
88
|
+
scheduleRetry(eventName: string, payload: unknown, _options: EventOptions, error: Error, retryCount: number): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* 獲取特定事件的隊列
|
|
91
|
+
*
|
|
92
|
+
* @param eventName 事件名稱
|
|
93
|
+
* @returns Queue 實例或 undefined
|
|
94
|
+
*/
|
|
95
|
+
getQueue(eventName: string): unknown;
|
|
96
|
+
/**
|
|
97
|
+
* 取得所有隊列統計
|
|
98
|
+
*/
|
|
99
|
+
getStats(): Map<string, QueueStats>;
|
|
100
|
+
/**
|
|
101
|
+
* 關閉所有隊列並清理資源
|
|
102
|
+
*/
|
|
103
|
+
shutdown(): Promise<void>;
|
|
104
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Pool for concurrent event task processing.
|
|
3
|
+
*
|
|
4
|
+
* Manages a pool of workers for executing event tasks concurrently.
|
|
5
|
+
* Supports auto-scaling, health checks, and OpenTelemetry metrics integration.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
import type { Meter } from '@opentelemetry/api';
|
|
10
|
+
import type { EventTask } from './types';
|
|
11
|
+
import type { WorkerPoolConfig, WorkerPoolStats, WorkerStats } from './WorkerPoolConfig';
|
|
12
|
+
/**
|
|
13
|
+
* Worker Pool for managing concurrent task execution
|
|
14
|
+
*/
|
|
15
|
+
export declare class WorkerPool {
|
|
16
|
+
private workers;
|
|
17
|
+
private taskQueue;
|
|
18
|
+
private config;
|
|
19
|
+
private metrics?;
|
|
20
|
+
private healthCheckTimer?;
|
|
21
|
+
private metricsTimer?;
|
|
22
|
+
private isRunning;
|
|
23
|
+
private scaleDownCounter;
|
|
24
|
+
constructor(config?: WorkerPoolConfig, meter?: Meter);
|
|
25
|
+
/**
|
|
26
|
+
* Start the worker pool
|
|
27
|
+
*/
|
|
28
|
+
start(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Stop the worker pool
|
|
31
|
+
*/
|
|
32
|
+
stop(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Submit a task to the worker pool
|
|
35
|
+
*/
|
|
36
|
+
submitTask(task: EventTask): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Process next task in queue
|
|
39
|
+
*/
|
|
40
|
+
private processQueue;
|
|
41
|
+
/**
|
|
42
|
+
* Execute task on a worker
|
|
43
|
+
*/
|
|
44
|
+
private executeTask;
|
|
45
|
+
/**
|
|
46
|
+
* Execute task callbacks
|
|
47
|
+
*/
|
|
48
|
+
private executeTaskCallbacks;
|
|
49
|
+
/**
|
|
50
|
+
* Create a timeout promise
|
|
51
|
+
*/
|
|
52
|
+
private createTimeout;
|
|
53
|
+
/**
|
|
54
|
+
* Create a new worker
|
|
55
|
+
*/
|
|
56
|
+
private createWorker;
|
|
57
|
+
/**
|
|
58
|
+
* Remove a worker
|
|
59
|
+
*/
|
|
60
|
+
private removeWorker;
|
|
61
|
+
/**
|
|
62
|
+
* Perform health check and cleanup
|
|
63
|
+
*/
|
|
64
|
+
private performHealthCheck;
|
|
65
|
+
/**
|
|
66
|
+
* Perform auto-scaling based on load
|
|
67
|
+
*/
|
|
68
|
+
private performAutoScaling;
|
|
69
|
+
/**
|
|
70
|
+
* Collect metrics
|
|
71
|
+
*/
|
|
72
|
+
private collectMetrics;
|
|
73
|
+
/**
|
|
74
|
+
* Get number of active workers (not terminated)
|
|
75
|
+
*/
|
|
76
|
+
private getActiveWorkerCount;
|
|
77
|
+
/**
|
|
78
|
+
* Get worker utilization (0-1)
|
|
79
|
+
*/
|
|
80
|
+
private getUtilization;
|
|
81
|
+
/**
|
|
82
|
+
* Get queue depth
|
|
83
|
+
*/
|
|
84
|
+
getQueueDepth(): number;
|
|
85
|
+
/**
|
|
86
|
+
* Get worker utilization
|
|
87
|
+
*/
|
|
88
|
+
getWorkerUtilization(): number;
|
|
89
|
+
/**
|
|
90
|
+
* Get worker statistics
|
|
91
|
+
*/
|
|
92
|
+
getWorkerStats(): WorkerStats[];
|
|
93
|
+
/**
|
|
94
|
+
* Get pool statistics
|
|
95
|
+
*/
|
|
96
|
+
getPoolStats(): WorkerPoolStats;
|
|
97
|
+
}
|
|
98
|
+
export type { WorkerPoolConfig, WorkerStats, WorkerPoolStats };
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Pool Configuration and Types
|
|
3
|
+
*
|
|
4
|
+
* Defines configuration interfaces and data types for worker pool management.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
import type { EventTask } from './types';
|
|
9
|
+
/**
|
|
10
|
+
* Task source interface for fetching and acknowledging tasks.
|
|
11
|
+
* Supports integration with external task systems like Bull Queue.
|
|
12
|
+
*/
|
|
13
|
+
export interface TaskSource {
|
|
14
|
+
/**
|
|
15
|
+
* Fetch the next task from the source.
|
|
16
|
+
* Returns null if no tasks available.
|
|
17
|
+
*/
|
|
18
|
+
fetchTask(): Promise<EventTask | null>;
|
|
19
|
+
/**
|
|
20
|
+
* Acknowledge successful task completion.
|
|
21
|
+
*/
|
|
22
|
+
acknowledgeCompletion(taskId: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Acknowledge task failure.
|
|
25
|
+
*/
|
|
26
|
+
acknowledgeFailure(taskId: string, error: Error): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Worker Pool Configuration
|
|
30
|
+
*/
|
|
31
|
+
export interface WorkerPoolConfig {
|
|
32
|
+
/**
|
|
33
|
+
* Maximum number of concurrent tasks being processed.
|
|
34
|
+
* @default 4
|
|
35
|
+
*/
|
|
36
|
+
concurrency?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Number of worker threads to maintain.
|
|
39
|
+
* @default Number of CPU cores
|
|
40
|
+
*/
|
|
41
|
+
workerThreads?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Task execution timeout in milliseconds.
|
|
44
|
+
* @default 30000
|
|
45
|
+
*/
|
|
46
|
+
taskTimeout?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Maximum number of retries for failed tasks.
|
|
49
|
+
* @default 3
|
|
50
|
+
*/
|
|
51
|
+
maxRetries?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Enable automatic scaling based on load.
|
|
54
|
+
* @default true
|
|
55
|
+
*/
|
|
56
|
+
enableAutoScaling?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Minimum number of workers for auto-scaling.
|
|
59
|
+
* @default 1
|
|
60
|
+
*/
|
|
61
|
+
minWorkers?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Maximum number of workers for auto-scaling.
|
|
64
|
+
* @default 2 * CPU cores
|
|
65
|
+
*/
|
|
66
|
+
maxWorkers?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Worker utilization threshold for scaling up (0-1).
|
|
69
|
+
* @default 0.8
|
|
70
|
+
*/
|
|
71
|
+
scaleUpThreshold?: number;
|
|
72
|
+
/**
|
|
73
|
+
* Worker utilization threshold for scaling down (0-1).
|
|
74
|
+
* @default 0.2
|
|
75
|
+
*/
|
|
76
|
+
scaleDownThreshold?: number;
|
|
77
|
+
/**
|
|
78
|
+
* Interval for collecting metrics in milliseconds.
|
|
79
|
+
* @default 5000
|
|
80
|
+
*/
|
|
81
|
+
metricsInterval?: number;
|
|
82
|
+
/**
|
|
83
|
+
* External task source (e.g., Bull Queue).
|
|
84
|
+
* When provided, workers fetch tasks from this source.
|
|
85
|
+
*/
|
|
86
|
+
taskSource?: TaskSource;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Worker State
|
|
90
|
+
*/
|
|
91
|
+
export type WorkerState = 'idle' | 'busy' | 'terminated';
|
|
92
|
+
/**
|
|
93
|
+
* Worker Statistics
|
|
94
|
+
*/
|
|
95
|
+
export interface WorkerStats {
|
|
96
|
+
/**
|
|
97
|
+
* Unique worker identifier.
|
|
98
|
+
*/
|
|
99
|
+
id: string;
|
|
100
|
+
/**
|
|
101
|
+
* Current worker state.
|
|
102
|
+
*/
|
|
103
|
+
state: WorkerState;
|
|
104
|
+
/**
|
|
105
|
+
* Total tasks processed by this worker.
|
|
106
|
+
*/
|
|
107
|
+
tasksProcessed: number;
|
|
108
|
+
/**
|
|
109
|
+
* Total successful tasks.
|
|
110
|
+
*/
|
|
111
|
+
tasksSuccess: number;
|
|
112
|
+
/**
|
|
113
|
+
* Total failed tasks.
|
|
114
|
+
*/
|
|
115
|
+
tasksFailed: number;
|
|
116
|
+
/**
|
|
117
|
+
* Average task execution duration in milliseconds.
|
|
118
|
+
*/
|
|
119
|
+
avgDurationMs: number;
|
|
120
|
+
/**
|
|
121
|
+
* Worker uptime in milliseconds.
|
|
122
|
+
*/
|
|
123
|
+
uptime: number;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Worker Pool Statistics
|
|
127
|
+
*/
|
|
128
|
+
export interface WorkerPoolStats {
|
|
129
|
+
/**
|
|
130
|
+
* Total tasks in queue.
|
|
131
|
+
*/
|
|
132
|
+
queueDepth: number;
|
|
133
|
+
/**
|
|
134
|
+
* Worker utilization (0-1).
|
|
135
|
+
*/
|
|
136
|
+
utilization: number;
|
|
137
|
+
/**
|
|
138
|
+
* Current number of active workers.
|
|
139
|
+
*/
|
|
140
|
+
activeWorkers: number;
|
|
141
|
+
/**
|
|
142
|
+
* Total tasks processed.
|
|
143
|
+
*/
|
|
144
|
+
totalTasksProcessed: number;
|
|
145
|
+
/**
|
|
146
|
+
* Total successful tasks.
|
|
147
|
+
*/
|
|
148
|
+
totalSuccess: number;
|
|
149
|
+
/**
|
|
150
|
+
* Total failed tasks.
|
|
151
|
+
*/
|
|
152
|
+
totalFailures: number;
|
|
153
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Pool Metrics for OpenTelemetry integration.
|
|
3
|
+
*
|
|
4
|
+
* Tracks worker pool statistics and emits metrics using OpenTelemetry.
|
|
5
|
+
* Follows the Provider Callback pattern for dynamic metric collection.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
import type { Meter } from '@opentelemetry/api';
|
|
10
|
+
/**
|
|
11
|
+
* Worker Pool Metrics Recorder
|
|
12
|
+
*/
|
|
13
|
+
export declare class WorkerPoolMetrics {
|
|
14
|
+
private meter;
|
|
15
|
+
private taskDurationHistogram?;
|
|
16
|
+
private taskCounter?;
|
|
17
|
+
private autoScalingCounter?;
|
|
18
|
+
private poolSizeCallback?;
|
|
19
|
+
private utilizationCallback?;
|
|
20
|
+
private queueDepthCallback?;
|
|
21
|
+
constructor(meter: Meter);
|
|
22
|
+
/**
|
|
23
|
+
* Initialize OpenTelemetry metrics
|
|
24
|
+
*/
|
|
25
|
+
private initializeMetrics;
|
|
26
|
+
/**
|
|
27
|
+
* Set provider callback for pool size metric
|
|
28
|
+
*/
|
|
29
|
+
setPoolSizeProvider(callback: () => number): void;
|
|
30
|
+
/**
|
|
31
|
+
* Set provider callback for utilization metric
|
|
32
|
+
*/
|
|
33
|
+
setUtilizationProvider(callback: () => number): void;
|
|
34
|
+
/**
|
|
35
|
+
* Set provider callback for queue depth metric
|
|
36
|
+
*/
|
|
37
|
+
setQueueDepthProvider(callback: () => number): void;
|
|
38
|
+
/**
|
|
39
|
+
* Record task execution
|
|
40
|
+
*
|
|
41
|
+
* @param durationMs - Task execution duration in milliseconds
|
|
42
|
+
* @param priority - Task priority level
|
|
43
|
+
* @param success - Whether task succeeded
|
|
44
|
+
*/
|
|
45
|
+
recordTaskExecution(durationMs: number, priority: string, success: boolean): void;
|
|
46
|
+
/**
|
|
47
|
+
* Record auto-scaling event
|
|
48
|
+
*
|
|
49
|
+
* @param action - 'scale-up' or 'scale-down'
|
|
50
|
+
* @param reason - Reason for scaling
|
|
51
|
+
*/
|
|
52
|
+
recordAutoScaling(action: 'scale-up' | 'scale-down', reason: string): void;
|
|
53
|
+
/**
|
|
54
|
+
* Get the current pool size (for provider callbacks)
|
|
55
|
+
*/
|
|
56
|
+
getPoolSize(): number;
|
|
57
|
+
/**
|
|
58
|
+
* Get the current utilization (for provider callbacks)
|
|
59
|
+
*/
|
|
60
|
+
getUtilization(): number;
|
|
61
|
+
/**
|
|
62
|
+
* Get the current queue depth (for provider callbacks)
|
|
63
|
+
*/
|
|
64
|
+
getQueueDepth(): number;
|
|
65
|
+
}
|