@gravito/stream 2.0.1 → 2.1.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 +127 -285
- package/README.zh-TW.md +146 -13
- package/dist/BatchConsumer.d.ts +81 -0
- package/dist/Consumer.d.ts +215 -0
- package/dist/DashboardProvider.d.ts +20 -0
- package/dist/Job.d.ts +183 -0
- package/dist/OrbitStream.d.ts +151 -0
- package/dist/QueueManager.d.ts +319 -0
- package/dist/Queueable.d.ts +91 -0
- package/dist/Scheduler.d.ts +214 -0
- package/dist/StreamEventBackend.d.ts +114 -0
- package/dist/SystemEventJob.d.ts +33 -0
- package/dist/Worker.d.ts +139 -0
- package/dist/benchmarks/PerformanceReporter.d.ts +99 -0
- package/dist/consumer/ConcurrencyGate.d.ts +55 -0
- package/dist/consumer/ConsumerStrategy.d.ts +41 -0
- package/dist/consumer/GroupSequencer.d.ts +57 -0
- package/dist/consumer/HeartbeatManager.d.ts +65 -0
- package/dist/consumer/JobExecutor.d.ts +61 -0
- package/dist/consumer/JobSourceGenerator.d.ts +31 -0
- package/dist/consumer/PollingStrategy.d.ts +42 -0
- package/dist/consumer/ReactiveStrategy.d.ts +41 -0
- package/dist/consumer/StreamingConsumer.d.ts +88 -0
- package/dist/consumer/index.d.ts +13 -0
- package/dist/consumer/types.d.ts +102 -0
- package/dist/drivers/BinaryJobFrame.d.ts +78 -0
- package/dist/drivers/BullMQDriver.d.ts +186 -0
- package/dist/drivers/DatabaseDriver.d.ts +131 -0
- package/dist/drivers/GrpcDriver.d.ts +16 -0
- package/dist/drivers/KafkaDriver.d.ts +148 -0
- package/dist/drivers/MemoryDriver.d.ts +108 -0
- package/dist/drivers/QueueDriver.d.ts +250 -0
- package/dist/drivers/RabbitMQDriver.d.ts +102 -0
- package/dist/drivers/RedisDriver.d.ts +294 -0
- package/dist/drivers/SQSDriver.d.ts +111 -0
- package/dist/drivers/kafka/BackpressureController.d.ts +60 -0
- package/dist/drivers/kafka/BatchProcessor.d.ts +50 -0
- package/dist/drivers/kafka/ConsumerLifecycleManager.d.ts +80 -0
- package/dist/drivers/kafka/ErrorCategorizer.d.ts +39 -0
- package/dist/drivers/kafka/ErrorRecoveryManager.d.ts +100 -0
- package/dist/drivers/kafka/HeartbeatManager.d.ts +57 -0
- package/dist/drivers/kafka/KafkaDriver.d.ts +138 -0
- package/dist/drivers/kafka/KafkaMetrics.d.ts +88 -0
- package/dist/drivers/kafka/KafkaNotifier.d.ts +54 -0
- package/dist/drivers/kafka/MessageBuffer.d.ts +71 -0
- package/dist/drivers/kafka/OffsetTracker.d.ts +63 -0
- package/dist/drivers/kafka/PerformanceMonitor.d.ts +88 -0
- package/dist/drivers/kafka/RateLimiter.d.ts +52 -0
- package/dist/drivers/kafka/RebalanceHandler.d.ts +104 -0
- package/dist/drivers/kafka/RingBuffer.d.ts +63 -0
- package/dist/drivers/kafka/index.d.ts +22 -0
- package/dist/drivers/kafka/types.d.ts +553 -0
- package/dist/drivers/prepareJobForTransport.d.ts +10 -0
- package/dist/index.cjs +6274 -3777
- package/dist/index.cjs.map +71 -0
- package/dist/index.d.ts +60 -2233
- package/dist/index.js +6955 -4446
- package/dist/index.js.map +71 -0
- package/dist/locks/DistributedLock.d.ts +175 -0
- package/dist/persistence/BufferedPersistence.d.ts +130 -0
- package/dist/persistence/BunBufferedPersistence.d.ts +173 -0
- package/dist/persistence/MySQLPersistence.d.ts +134 -0
- package/dist/persistence/SQLitePersistence.d.ts +133 -0
- package/dist/serializers/BinarySerializer.d.ts +42 -0
- package/dist/serializers/CachedSerializer.d.ts +38 -0
- package/dist/serializers/CborNativeSerializer.d.ts +56 -0
- package/dist/serializers/ClassNameSerializer.d.ts +58 -0
- package/dist/serializers/JobSerializer.d.ts +33 -0
- package/dist/serializers/JsonSerializer.d.ts +28 -0
- package/dist/serializers/JsonlSerializer.d.ts +90 -0
- package/dist/serializers/MessagePackSerializer.d.ts +29 -0
- package/dist/types.d.ts +653 -0
- package/dist/workers/BinaryWorkerProtocol.d.ts +77 -0
- package/dist/workers/BunWorker.d.ts +179 -0
- package/dist/workers/SandboxedWorker.d.ts +132 -0
- package/dist/workers/WorkerFactory.d.ts +128 -0
- package/dist/workers/WorkerPool.d.ts +186 -0
- package/dist/workers/bun-job-executor.d.ts +14 -0
- package/dist/workers/index.d.ts +13 -0
- package/dist/workers/job-executor.d.ts +9 -0
- package/package.json +13 -6
- package/proto/queue.proto +101 -0
- package/dist/index.d.cts +0 -2242
package/dist/index.d.ts
CHANGED
|
@@ -1,2242 +1,69 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
import { GravitoOrbit, PlanetCore } from '@gravito/core';
|
|
3
|
-
import { ConnectionContract } from '@gravito/atlas';
|
|
4
|
-
|
|
5
1
|
/**
|
|
6
|
-
*
|
|
7
|
-
* @public
|
|
8
|
-
*/
|
|
9
|
-
interface SerializedJob {
|
|
10
|
-
/** Unique job identifier */
|
|
11
|
-
id: string;
|
|
12
|
-
/** Serializer type: 'json' for plain objects or 'class' for instances */
|
|
13
|
-
type: 'json' | 'class' | 'msgpack';
|
|
14
|
-
/** Serialized data string */
|
|
15
|
-
data: string;
|
|
16
|
-
/** Fully qualified class name (only used for 'class' type) */
|
|
17
|
-
className?: string;
|
|
18
|
-
/** Timestamp when the job was created */
|
|
19
|
-
createdAt: number;
|
|
20
|
-
/** Optional delay in seconds before the job becomes available for processing */
|
|
21
|
-
delaySeconds?: number;
|
|
22
|
-
/** Number of times the job has been attempted */
|
|
23
|
-
attempts?: number;
|
|
24
|
-
/** Maximum number of retry attempts before the job is marked as failed */
|
|
25
|
-
maxAttempts?: number;
|
|
26
|
-
/** Group ID for FIFO (strictly sequential) processing */
|
|
27
|
-
groupId?: string;
|
|
28
|
-
/** Initial delay in seconds before first retry attempt */
|
|
29
|
-
retryAfterSeconds?: number;
|
|
30
|
-
/** Multiplier for exponential backoff on retries */
|
|
31
|
-
retryMultiplier?: number;
|
|
32
|
-
/** Last error message if the job failed */
|
|
33
|
-
error?: string;
|
|
34
|
-
/** Timestamp when the job finally failed after max attempts */
|
|
35
|
-
failedAt?: number;
|
|
36
|
-
/** Optional priority for the job (string or numeric) */
|
|
37
|
-
priority?: string | number;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Statistics for a single queue.
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
43
|
-
interface QueueStats {
|
|
44
|
-
/** Queue name */
|
|
45
|
-
queue: string;
|
|
46
|
-
/** Number of pending jobs */
|
|
47
|
-
size: number;
|
|
48
|
-
/** Number of delayed jobs (if supported) */
|
|
49
|
-
delayed?: number;
|
|
50
|
-
/** Number of reserved/in-flight jobs (if supported) */
|
|
51
|
-
reserved?: number;
|
|
52
|
-
/** Number of failed jobs in DLQ (if supported) */
|
|
53
|
-
failed?: number;
|
|
54
|
-
/** Additional custom metrics */
|
|
55
|
-
metrics?: Record<string, number>;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Advanced topic options for distributed queues (e.g., Kafka).
|
|
59
|
-
* @public
|
|
60
|
-
*/
|
|
61
|
-
interface TopicOptions {
|
|
62
|
-
/** Number of partitions for the topic */
|
|
63
|
-
partitions?: number;
|
|
64
|
-
/** Number of replicas for each partition */
|
|
65
|
-
replicationFactor?: number;
|
|
66
|
-
/** Additional driver-specific configurations */
|
|
67
|
-
config?: Record<string, string>;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Database driver configuration.
|
|
71
|
-
* @public
|
|
72
|
-
*/
|
|
73
|
-
interface DatabaseDriverConfig$1 {
|
|
74
|
-
driver: 'database';
|
|
75
|
-
/** Database service implementation for executing queries */
|
|
76
|
-
dbService: any;
|
|
77
|
-
/** Optional table name for job storage */
|
|
78
|
-
table?: string;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Redis driver configuration.
|
|
82
|
-
* @public
|
|
83
|
-
*/
|
|
84
|
-
interface RedisDriverConfig$1 {
|
|
85
|
-
driver: 'redis';
|
|
86
|
-
/** Redis client instance (ioredis or node-redis compatible) */
|
|
87
|
-
client: any;
|
|
88
|
-
/** Optional prefix for all Redis keys */
|
|
89
|
-
prefix?: string;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Kafka driver configuration.
|
|
93
|
-
*/
|
|
94
|
-
interface KafkaDriverConfig$1 {
|
|
95
|
-
driver: 'kafka';
|
|
96
|
-
client: any;
|
|
97
|
-
consumerGroupId?: string;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* SQS driver configuration.
|
|
101
|
-
* @public
|
|
102
|
-
*/
|
|
103
|
-
interface SQSDriverConfig$1 {
|
|
104
|
-
driver: 'sqs';
|
|
105
|
-
/** Amazon SQS client instance */
|
|
106
|
-
client: any;
|
|
107
|
-
/** Optional prefix for queue URLs */
|
|
108
|
-
queueUrlPrefix?: string;
|
|
109
|
-
/** The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request. */
|
|
110
|
-
visibilityTimeout?: number;
|
|
111
|
-
/** The duration (in seconds) for which the call waits for a message to arrive in the queue before returning. */
|
|
112
|
-
waitTimeSeconds?: number;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* RabbitMQ driver configuration.
|
|
116
|
-
*/
|
|
117
|
-
interface RabbitMQDriverConfig$1 {
|
|
118
|
-
driver: 'rabbitmq';
|
|
119
|
-
client: any;
|
|
120
|
-
exchange?: string;
|
|
121
|
-
exchangeType?: string;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Configuration for a specific queue connection.
|
|
125
|
-
* @public
|
|
126
|
-
*/
|
|
127
|
-
type QueueConnectionConfig = {
|
|
128
|
-
driver: 'memory';
|
|
129
|
-
} | DatabaseDriverConfig$1 | RedisDriverConfig$1 | KafkaDriverConfig$1 | SQSDriverConfig$1 | RabbitMQDriverConfig$1 | {
|
|
130
|
-
driver: 'nats';
|
|
131
|
-
[key: string]: unknown;
|
|
132
|
-
} | {
|
|
133
|
-
driver: string;
|
|
134
|
-
[key: string]: unknown;
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Queue manager config.
|
|
138
|
-
*/
|
|
139
|
-
interface QueueConfig {
|
|
140
|
-
/**
|
|
141
|
-
* Default connection name.
|
|
142
|
-
*/
|
|
143
|
-
default?: string;
|
|
144
|
-
/**
|
|
145
|
-
* Connection configs.
|
|
146
|
-
*/
|
|
147
|
-
connections?: Record<string, QueueConnectionConfig>;
|
|
148
|
-
/**
|
|
149
|
-
* Default serializer type.
|
|
150
|
-
*/
|
|
151
|
-
defaultSerializer?: 'json' | 'class' | 'msgpack';
|
|
152
|
-
/**
|
|
153
|
-
* Whether to enable serialization caching.
|
|
154
|
-
* If true, re-queuing the same Job instance will use the cached serialized data.
|
|
155
|
-
* @default false
|
|
156
|
-
*/
|
|
157
|
-
useSerializationCache?: boolean;
|
|
158
|
-
/**
|
|
159
|
-
* Enable verbose debug logging for QueueManager and Consumer.
|
|
160
|
-
* @default false
|
|
161
|
-
*/
|
|
162
|
-
debug?: boolean;
|
|
163
|
-
/**
|
|
164
|
-
* Persistence configuration (SQL Archive).
|
|
165
|
-
*/
|
|
166
|
-
persistence?: {
|
|
167
|
-
/**
|
|
168
|
-
* Persistence adapter instance or config.
|
|
169
|
-
*/
|
|
170
|
-
adapter: PersistenceAdapter;
|
|
171
|
-
/**
|
|
172
|
-
* Whether to automatically archive completed jobs.
|
|
173
|
-
*/
|
|
174
|
-
archiveCompleted?: boolean;
|
|
175
|
-
/**
|
|
176
|
-
* Whether to automatically archive failed jobs.
|
|
177
|
-
*/
|
|
178
|
-
archiveFailed?: boolean;
|
|
179
|
-
/**
|
|
180
|
-
* Whether to archive jobs immediately upon enqueue (Audit Mode).
|
|
181
|
-
* @default false
|
|
182
|
-
*/
|
|
183
|
-
archiveEnqueued?: boolean;
|
|
184
|
-
/**
|
|
185
|
-
* Buffer size for batched writes.
|
|
186
|
-
* If set, wraps the adapter in BufferedPersistence.
|
|
187
|
-
*/
|
|
188
|
-
bufferSize?: number;
|
|
189
|
-
/**
|
|
190
|
-
* Flush interval in ms for batched writes.
|
|
191
|
-
* If set, wraps the adapter in BufferedPersistence.
|
|
192
|
-
*/
|
|
193
|
-
flushInterval?: number;
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Persistence Adapter Interface
|
|
198
|
-
* Used for long-term archiving of jobs in a SQL database.
|
|
199
|
-
*/
|
|
200
|
-
interface PersistenceAdapter {
|
|
201
|
-
/**
|
|
202
|
-
* Archive a job.
|
|
203
|
-
*/
|
|
204
|
-
archive(queue: string, job: SerializedJob, status: 'completed' | 'failed' | 'waiting' | string): Promise<void>;
|
|
205
|
-
/**
|
|
206
|
-
* Find a job in the archive.
|
|
207
|
-
*/
|
|
208
|
-
find(queue: string, id: string): Promise<SerializedJob | null>;
|
|
209
|
-
/**
|
|
210
|
-
* List jobs from the archive.
|
|
211
|
-
*/
|
|
212
|
-
list(queue: string, options?: {
|
|
213
|
-
limit?: number;
|
|
214
|
-
offset?: number;
|
|
215
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
216
|
-
jobId?: string;
|
|
217
|
-
startTime?: Date;
|
|
218
|
-
endTime?: Date;
|
|
219
|
-
}): Promise<SerializedJob[]>;
|
|
220
|
-
/**
|
|
221
|
-
* Archive multiple jobs (batch write).
|
|
222
|
-
*/
|
|
223
|
-
archiveMany?(jobs: Array<{
|
|
224
|
-
queue: string;
|
|
225
|
-
job: SerializedJob;
|
|
226
|
-
status: 'completed' | 'failed' | 'waiting' | string;
|
|
227
|
-
}>): Promise<void>;
|
|
228
|
-
/**
|
|
229
|
-
* Remove old data from the archive.
|
|
230
|
-
*/
|
|
231
|
-
cleanup(days: number): Promise<number>;
|
|
232
|
-
/**
|
|
233
|
-
* Flush any buffered data.
|
|
234
|
-
*/
|
|
235
|
-
flush?(): Promise<void>;
|
|
236
|
-
/**
|
|
237
|
-
* Count jobs in the archive.
|
|
238
|
-
*/
|
|
239
|
-
count(queue: string, options?: {
|
|
240
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
241
|
-
jobId?: string;
|
|
242
|
-
startTime?: Date;
|
|
243
|
-
endTime?: Date;
|
|
244
|
-
}): Promise<number>;
|
|
245
|
-
/**
|
|
246
|
-
* Archive a system log message.
|
|
247
|
-
*/
|
|
248
|
-
archiveLog(log: {
|
|
249
|
-
level: string;
|
|
250
|
-
message: string;
|
|
251
|
-
workerId: string;
|
|
252
|
-
queue?: string;
|
|
253
|
-
timestamp: Date;
|
|
254
|
-
}): Promise<void>;
|
|
255
|
-
/**
|
|
256
|
-
* Archive multiple log messages (batch write).
|
|
257
|
-
*/
|
|
258
|
-
archiveLogMany?(logs: Array<{
|
|
259
|
-
level: string;
|
|
260
|
-
message: string;
|
|
261
|
-
workerId: string;
|
|
262
|
-
queue?: string;
|
|
263
|
-
timestamp: Date;
|
|
264
|
-
}>): Promise<void>;
|
|
265
|
-
/**
|
|
266
|
-
* List system logs from the archive.
|
|
267
|
-
*/
|
|
268
|
-
listLogs(options?: {
|
|
269
|
-
limit?: number;
|
|
270
|
-
offset?: number;
|
|
271
|
-
level?: string;
|
|
272
|
-
workerId?: string;
|
|
273
|
-
queue?: string;
|
|
274
|
-
search?: string;
|
|
275
|
-
startTime?: Date;
|
|
276
|
-
endTime?: Date;
|
|
277
|
-
}): Promise<any[]>;
|
|
278
|
-
/**
|
|
279
|
-
* Count system logs in the archive.
|
|
280
|
-
*/
|
|
281
|
-
countLogs(options?: {
|
|
282
|
-
level?: string;
|
|
283
|
-
workerId?: string;
|
|
284
|
-
queue?: string;
|
|
285
|
-
search?: string;
|
|
286
|
-
startTime?: Date;
|
|
287
|
-
endTime?: Date;
|
|
288
|
-
}): Promise<number>;
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Options when pushing a job.
|
|
292
|
-
*/
|
|
293
|
-
interface JobPushOptions {
|
|
294
|
-
/**
|
|
295
|
-
* Group ID for FIFO ordering (e.g. userId).
|
|
296
|
-
* If set, jobs with the same groupId will be processed strictly sequentially.
|
|
297
|
-
*/
|
|
298
|
-
groupId?: string;
|
|
299
|
-
/**
|
|
300
|
-
* Job priority.
|
|
301
|
-
* Higher priority jobs are processed first (if supported by driver).
|
|
302
|
-
* Example: 'high', 'low', 'critical'
|
|
303
|
-
*/
|
|
304
|
-
priority?: string | number;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Queue driver interface.
|
|
309
|
-
*
|
|
310
|
-
* All queue drivers must implement this interface.
|
|
311
|
-
* Defines basic queue operations plus optional enterprise-grade capabilities.
|
|
312
|
-
*
|
|
313
|
-
* @example
|
|
314
|
-
* ```typescript
|
|
315
|
-
* class MyDriver implements QueueDriver {
|
|
316
|
-
* async push(queue: string, job: SerializedJob, options?: JobPushOptions): Promise<void> {
|
|
317
|
-
* // push a job
|
|
318
|
-
* }
|
|
319
|
-
*
|
|
320
|
-
* async pop(queue: string): Promise<SerializedJob | null> {
|
|
321
|
-
* // pop a job
|
|
322
|
-
* }
|
|
323
|
-
*
|
|
324
|
-
* async complete(queue: string, job: SerializedJob): Promise<void> {
|
|
325
|
-
* // job completed (for FIFO handling)
|
|
326
|
-
* }
|
|
327
|
-
*
|
|
328
|
-
* async size(queue: string): Promise<number> {
|
|
329
|
-
* // queue size
|
|
330
|
-
* }
|
|
331
|
-
*
|
|
332
|
-
* async clear(queue: string): Promise<void> {
|
|
333
|
-
* // clear queue
|
|
334
|
-
* }
|
|
335
|
-
* }
|
|
336
|
-
* ```
|
|
337
|
-
*/
|
|
338
|
-
interface QueueDriver {
|
|
339
|
-
/**
|
|
340
|
-
* Push a job to a queue.
|
|
341
|
-
* @param queue - Queue name
|
|
342
|
-
* @param job - Serialized job
|
|
343
|
-
* @param options - Push options (e.g. groupId)
|
|
344
|
-
*/
|
|
345
|
-
push(queue: string, job: SerializedJob, options?: JobPushOptions): Promise<void>;
|
|
346
|
-
/**
|
|
347
|
-
* Pop a job from a queue (non-blocking).
|
|
348
|
-
* @param queue - Queue name
|
|
349
|
-
* @returns Serialized job, or `null` if the queue is empty
|
|
350
|
-
*/
|
|
351
|
-
pop(queue: string): Promise<SerializedJob | null>;
|
|
352
|
-
/**
|
|
353
|
-
* Pop a job from a queue (blocking).
|
|
354
|
-
* @param queues - Queue name or array of queue names
|
|
355
|
-
* @param timeout - Timeout in seconds
|
|
356
|
-
* @returns Serialized job, or `null` if timeout reached
|
|
357
|
-
*/
|
|
358
|
-
popBlocking?(queues: string | string[], timeout: number): Promise<SerializedJob | null>;
|
|
359
|
-
/**
|
|
360
|
-
* Mark a job as completed (used for FIFO/Group handling).
|
|
361
|
-
* @param queue - Queue name
|
|
362
|
-
* @param job - Serialized job
|
|
363
|
-
*/
|
|
364
|
-
complete?(queue: string, job: SerializedJob): Promise<void>;
|
|
365
|
-
/**
|
|
366
|
-
* Get queue size.
|
|
367
|
-
* @param queue - Queue name
|
|
368
|
-
* @returns Number of jobs in the queue
|
|
369
|
-
*/
|
|
370
|
-
size(queue: string): Promise<number>;
|
|
371
|
-
/**
|
|
372
|
-
* Clear a queue.
|
|
373
|
-
* @param queue - Queue name
|
|
374
|
-
*/
|
|
375
|
-
clear(queue: string): Promise<void>;
|
|
376
|
-
/**
|
|
377
|
-
* Mark a job as permanently failed (move to DLQ).
|
|
378
|
-
* @param queue - Queue name
|
|
379
|
-
* @param job - Serialized job with error info
|
|
380
|
-
*/
|
|
381
|
-
fail?(queue: string, job: SerializedJob): Promise<void>;
|
|
382
|
-
/**
|
|
383
|
-
* Get queue statistics including pending, delayed, and failed job counts.
|
|
384
|
-
* @param queue - Queue name
|
|
385
|
-
*/
|
|
386
|
-
stats?(queue: string): Promise<QueueStats>;
|
|
387
|
-
/**
|
|
388
|
-
* Push multiple jobs (optional, higher throughput).
|
|
389
|
-
* @param queue - Queue name
|
|
390
|
-
* @param jobs - Serialized job array
|
|
391
|
-
*/
|
|
392
|
-
pushMany?(queue: string, jobs: SerializedJob[]): Promise<void>;
|
|
393
|
-
/**
|
|
394
|
-
* Pop multiple jobs (optional, higher throughput).
|
|
395
|
-
* @param queue - Queue name
|
|
396
|
-
* @param count - Max number of jobs to pop
|
|
397
|
-
* @returns Serialized job array
|
|
398
|
-
*/
|
|
399
|
-
popMany?(queue: string, count: number): Promise<SerializedJob[]>;
|
|
400
|
-
/**
|
|
401
|
-
* Acknowledge a message (enterprise capability, e.g. Kafka/SQS).
|
|
402
|
-
* @param messageId - Message ID
|
|
403
|
-
*/
|
|
404
|
-
acknowledge?(messageId: string): Promise<void>;
|
|
405
|
-
/**
|
|
406
|
-
* Subscribe to a queue (push-based model, e.g. Kafka/SQS).
|
|
407
|
-
* @param queue - Queue name
|
|
408
|
-
* @param callback - Callback to process jobs
|
|
409
|
-
*/
|
|
410
|
-
subscribe?(queue: string, callback: (job: SerializedJob) => Promise<void>): Promise<void>;
|
|
411
|
-
/**
|
|
412
|
-
* Create a topic (Kafka, etc.).
|
|
413
|
-
* @param topic - Topic name
|
|
414
|
-
* @param options - Topic options
|
|
415
|
-
*/
|
|
416
|
-
createTopic?(topic: string, options?: TopicOptions): Promise<void>;
|
|
417
|
-
/**
|
|
418
|
-
* Delete a topic (Kafka, etc.).
|
|
419
|
-
* @param topic - Topic name
|
|
420
|
-
*/
|
|
421
|
-
deleteTopic?(topic: string): Promise<void>;
|
|
422
|
-
/**
|
|
423
|
-
* Report worker heartbeat for monitoring.
|
|
424
|
-
* @param workerInfo - Worker information
|
|
425
|
-
* @param prefix - Optional prefix for monitoring keys
|
|
426
|
-
*/
|
|
427
|
-
reportHeartbeat?(workerInfo: {
|
|
428
|
-
id: string;
|
|
429
|
-
status: string;
|
|
430
|
-
hostname: string;
|
|
431
|
-
pid: number;
|
|
432
|
-
uptime: number;
|
|
433
|
-
last_ping: string;
|
|
434
|
-
queues: string[];
|
|
435
|
-
metrics?: Record<string, any>;
|
|
436
|
-
[key: string]: any;
|
|
437
|
-
}, prefix?: string): Promise<void>;
|
|
438
|
-
/**
|
|
439
|
-
* Publish a log message for monitoring.
|
|
440
|
-
* @param logPayload - Log payload
|
|
441
|
-
* @param prefix - Optional prefix for monitoring channels/keys
|
|
442
|
-
*/
|
|
443
|
-
publishLog?(logPayload: {
|
|
444
|
-
level: string;
|
|
445
|
-
message: string;
|
|
446
|
-
workerId: string;
|
|
447
|
-
jobId?: string;
|
|
448
|
-
timestamp: string;
|
|
449
|
-
[key: string]: any;
|
|
450
|
-
}, prefix?: string): Promise<void>;
|
|
451
|
-
/**
|
|
452
|
-
* Check if a queue is rate limited.
|
|
453
|
-
* @param queue - Queue name
|
|
454
|
-
* @param config - Rate limit configuration
|
|
455
|
-
* @returns true if allowed, false if limited
|
|
456
|
-
*/
|
|
457
|
-
checkRateLimit?(queue: string, config: {
|
|
458
|
-
max: number;
|
|
459
|
-
duration: number;
|
|
460
|
-
}): Promise<boolean>;
|
|
461
|
-
/**
|
|
462
|
-
* Retry failed jobs from DLQ.
|
|
463
|
-
* @param queue - Queue name
|
|
464
|
-
* @param count - Optional count (default: all)
|
|
465
|
-
* @returns Number of jobs retried
|
|
466
|
-
*/
|
|
467
|
-
retryFailed?(queue: string, count?: number): Promise<number>;
|
|
468
|
-
/**
|
|
469
|
-
* Get failed jobs from DLQ.
|
|
470
|
-
* @param queue - Queue name
|
|
471
|
-
* @param start - Start index
|
|
472
|
-
* @param end - End index
|
|
473
|
-
* @returns Array of failed jobs
|
|
474
|
-
*/
|
|
475
|
-
getFailed?(queue: string, start?: number, end?: number): Promise<SerializedJob[]>;
|
|
476
|
-
/**
|
|
477
|
-
* Clear failed jobs from DLQ.
|
|
478
|
-
* @param queue - Queue name
|
|
479
|
-
*/
|
|
480
|
-
clearFailed?(queue: string): Promise<void>;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Queueable interface.
|
|
485
|
-
*
|
|
486
|
-
* Classes implementing this interface can be pushed to a queue for execution.
|
|
487
|
-
* Provides a fluent API for queue/connection/delay configuration.
|
|
488
|
-
*
|
|
489
|
-
* @example
|
|
490
|
-
* ```typescript
|
|
491
|
-
* class MyJob implements Queueable {
|
|
492
|
-
* queueName?: string
|
|
493
|
-
* connectionName?: string
|
|
494
|
-
* delaySeconds?: number
|
|
495
|
-
*
|
|
496
|
-
* onQueue(queue: string): this {
|
|
497
|
-
* this.queueName = queue
|
|
498
|
-
* return this
|
|
499
|
-
* }
|
|
500
|
-
*
|
|
501
|
-
* onConnection(connection: string): this {
|
|
502
|
-
* this.connectionName = connection
|
|
503
|
-
* return this
|
|
504
|
-
* }
|
|
505
|
-
*
|
|
506
|
-
* delay(seconds: number): this {
|
|
507
|
-
* this.delaySeconds = seconds
|
|
508
|
-
* return this
|
|
509
|
-
* }
|
|
510
|
-
* }
|
|
511
|
-
* ```
|
|
512
|
-
*/
|
|
513
|
-
interface Queueable {
|
|
514
|
-
/**
|
|
515
|
-
* Queue name where the job should be pushed.
|
|
516
|
-
*/
|
|
517
|
-
queueName?: string;
|
|
518
|
-
/**
|
|
519
|
-
* Connection name the job should use.
|
|
520
|
-
*/
|
|
521
|
-
connectionName?: string;
|
|
522
|
-
/**
|
|
523
|
-
* Delay before execution (seconds).
|
|
524
|
-
*/
|
|
525
|
-
delaySeconds?: number;
|
|
526
|
-
/**
|
|
527
|
-
* Job priority.
|
|
528
|
-
*/
|
|
529
|
-
priority?: string | number;
|
|
530
|
-
/**
|
|
531
|
-
* Set target queue.
|
|
532
|
-
* @param queue - Queue name
|
|
533
|
-
* @returns Self for fluent chaining
|
|
534
|
-
*/
|
|
535
|
-
onQueue(queue: string): this;
|
|
536
|
-
/**
|
|
537
|
-
* Set target connection.
|
|
538
|
-
* @param connection - Connection name
|
|
539
|
-
* @returns Self for fluent chaining
|
|
540
|
-
*/
|
|
541
|
-
onConnection(connection: string): this;
|
|
542
|
-
/**
|
|
543
|
-
* Set job priority.
|
|
544
|
-
* @param priority - Priority level
|
|
545
|
-
* @returns Self for fluent chaining
|
|
546
|
-
*/
|
|
547
|
-
withPriority(priority: string | number): this;
|
|
548
|
-
/**
|
|
549
|
-
* Set delay (seconds).
|
|
550
|
-
* @param delay - Delay seconds
|
|
551
|
-
* @returns Self for fluent chaining
|
|
552
|
-
*/
|
|
553
|
-
delay(delay: number): this;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* Base Job.
|
|
2
|
+
* @gravito/stream
|
|
558
3
|
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
4
|
+
* Lightweight, high-performance queue system inspired by Laravel while keeping Gravito's core values.
|
|
5
|
+
* Supports multiple storage drivers, embedded/standalone consumer modes, and multiple job serialization strategies.
|
|
561
6
|
*
|
|
562
7
|
* @example
|
|
563
8
|
* ```typescript
|
|
564
|
-
*
|
|
565
|
-
* constructor(private userId: string) {
|
|
566
|
-
* super()
|
|
567
|
-
* }
|
|
9
|
+
* import { OrbitStream, Job } from '@gravito/stream'
|
|
568
10
|
*
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
11
|
+
* // Create a Job
|
|
12
|
+
* class SendEmail extends Job {
|
|
13
|
+
* async handle() {
|
|
14
|
+
* // handle logic
|
|
572
15
|
* }
|
|
573
16
|
* }
|
|
574
17
|
*
|
|
575
|
-
* //
|
|
576
|
-
* await queue.push(new
|
|
577
|
-
*
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
/**
|
|
628
|
-
* Set target connection.
|
|
629
|
-
*/
|
|
630
|
-
onConnection(connection: string): this;
|
|
631
|
-
/**
|
|
632
|
-
* Set job priority.
|
|
633
|
-
* @param priority - 'high', 'low', or number
|
|
634
|
-
*/
|
|
635
|
-
withPriority(priority: string | number): this;
|
|
636
|
-
/**
|
|
637
|
-
* Set delay (seconds).
|
|
638
|
-
*/
|
|
639
|
-
delay(delay: number): this;
|
|
640
|
-
/**
|
|
641
|
-
* Set retry backoff strategy.
|
|
642
|
-
* @param seconds - Initial delay in seconds
|
|
643
|
-
* @param multiplier - Multiplier for each subsequent attempt (default: 2)
|
|
644
|
-
*/
|
|
645
|
-
backoff(seconds: number, multiplier?: number): this;
|
|
646
|
-
/**
|
|
647
|
-
* Calculate retry delay for the next attempt.
|
|
648
|
-
* @param attempt - Current attempt number (1-based)
|
|
649
|
-
* @returns Delay in milliseconds
|
|
650
|
-
*/
|
|
651
|
-
getRetryDelay(attempt: number): number;
|
|
652
|
-
/**
|
|
653
|
-
* Job handler logic.
|
|
654
|
-
*
|
|
655
|
-
* Subclasses must implement this method.
|
|
656
|
-
*/
|
|
657
|
-
abstract handle(): Promise<void>;
|
|
658
|
-
/**
|
|
659
|
-
* Failure handler (optional).
|
|
660
|
-
*
|
|
661
|
-
* Called when the job fails and reaches the maximum number of attempts.
|
|
662
|
-
* Subclasses can override to implement custom failure handling.
|
|
663
|
-
*
|
|
664
|
-
* @param error - Error instance
|
|
665
|
-
*/
|
|
666
|
-
failed(_error: Error): Promise<void>;
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
/**
|
|
670
|
-
* Configuration for a recurring scheduled job.
|
|
671
|
-
*
|
|
672
|
-
* @public
|
|
673
|
-
* @since 3.0.0
|
|
674
|
-
*/
|
|
675
|
-
interface ScheduledJobConfig {
|
|
676
|
-
/** Unique identifier for the scheduled task. */
|
|
677
|
-
id: string;
|
|
678
|
-
/** Cron expression defining the schedule (e.g., '* * * * *'). */
|
|
679
|
-
cron: string;
|
|
680
|
-
/** The target queue name where the job should be pushed. */
|
|
681
|
-
queue: string;
|
|
682
|
-
/** The serialized job data. */
|
|
683
|
-
job: SerializedJob;
|
|
684
|
-
/** Timestamp of the last successful execution in milliseconds. */
|
|
685
|
-
lastRun?: number;
|
|
686
|
-
/** Timestamp of the next scheduled execution in milliseconds. */
|
|
687
|
-
nextRun?: number;
|
|
688
|
-
/** Whether the scheduled job is active. */
|
|
689
|
-
enabled: boolean;
|
|
690
|
-
}
|
|
691
|
-
/**
|
|
692
|
-
* Scheduler manages recurring (cron) jobs in Gravito.
|
|
693
|
-
*
|
|
694
|
-
* It uses Redis to store schedule metadata and coordinates distributed
|
|
695
|
-
* execution using locks to ensure jobs are triggered exactly once per interval.
|
|
696
|
-
*
|
|
697
|
-
* @example
|
|
698
|
-
* ```typescript
|
|
699
|
-
* const scheduler = new Scheduler(queueManager);
|
|
700
|
-
* await scheduler.register({
|
|
701
|
-
* id: 'daily-cleanup',
|
|
702
|
-
* cron: '0 0 * * *',
|
|
703
|
-
* queue: 'default',
|
|
704
|
-
* job: myJob.serialize()
|
|
705
|
-
* });
|
|
706
|
-
* ```
|
|
707
|
-
*
|
|
708
|
-
* @public
|
|
709
|
-
* @since 3.0.0
|
|
710
|
-
*/
|
|
711
|
-
declare class Scheduler {
|
|
712
|
-
private manager;
|
|
713
|
-
private prefix;
|
|
714
|
-
constructor(manager: QueueManager, options?: {
|
|
715
|
-
prefix?: string;
|
|
716
|
-
});
|
|
717
|
-
private get client();
|
|
718
|
-
/**
|
|
719
|
-
* Register a scheduled job.
|
|
720
|
-
*/
|
|
721
|
-
register(config: Omit<ScheduledJobConfig, 'nextRun' | 'enabled'>): Promise<void>;
|
|
722
|
-
/**
|
|
723
|
-
* Remove a scheduled job.
|
|
724
|
-
*/
|
|
725
|
-
remove(id: string): Promise<void>;
|
|
726
|
-
/**
|
|
727
|
-
* List all scheduled jobs.
|
|
728
|
-
*/
|
|
729
|
-
list(): Promise<ScheduledJobConfig[]>;
|
|
730
|
-
/**
|
|
731
|
-
* Run a scheduled job immediately (out of schedule).
|
|
732
|
-
*/
|
|
733
|
-
runNow(id: string): Promise<void>;
|
|
734
|
-
/**
|
|
735
|
-
* Process due tasks (TICK).
|
|
736
|
-
* This should be called periodically (e.g. every minute).
|
|
737
|
-
*/
|
|
738
|
-
tick(): Promise<number>;
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
/**
|
|
742
|
-
* Job serializer interface.
|
|
743
|
-
*
|
|
744
|
-
* Responsible for serializing and deserializing jobs.
|
|
745
|
-
* Supports multiple strategies (JSON, class-name, etc.).
|
|
746
|
-
*
|
|
747
|
-
* @example
|
|
748
|
-
* ```typescript
|
|
749
|
-
* class MySerializer implements JobSerializer {
|
|
750
|
-
* serialize(job: Job): SerializedJob {
|
|
751
|
-
* // serialization logic
|
|
752
|
-
* }
|
|
753
|
-
*
|
|
754
|
-
* deserialize(serialized: SerializedJob): Job {
|
|
755
|
-
* // deserialization logic
|
|
756
|
-
* }
|
|
757
|
-
* }
|
|
758
|
-
* ```
|
|
759
|
-
*/
|
|
760
|
-
interface JobSerializer {
|
|
761
|
-
/**
|
|
762
|
-
* Serialize a job.
|
|
763
|
-
* @param job - Job instance
|
|
764
|
-
* @returns Serialized job payload
|
|
765
|
-
*/
|
|
766
|
-
serialize(job: Job): SerializedJob;
|
|
767
|
-
/**
|
|
768
|
-
* Deserialize a job.
|
|
769
|
-
* @param serialized - Serialized job payload
|
|
770
|
-
* @returns Job instance
|
|
771
|
-
*/
|
|
772
|
-
deserialize(serialized: SerializedJob): Job;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
/**
|
|
776
|
-
* Queue Manager
|
|
777
|
-
*
|
|
778
|
-
* Manages multiple queue connections and drivers, exposing a unified API for pushing and consuming jobs.
|
|
779
|
-
* Supports lazy-loading drivers to keep the core lightweight.
|
|
780
|
-
*
|
|
781
|
-
* @example
|
|
782
|
-
* ```typescript
|
|
783
|
-
* const manager = new QueueManager({
|
|
784
|
-
* default: 'database',
|
|
785
|
-
* connections: {
|
|
786
|
-
* database: { driver: 'database', table: 'jobs' },
|
|
787
|
-
* redis: { driver: 'redis', url: 'redis://...' }
|
|
788
|
-
* }
|
|
789
|
-
* })
|
|
790
|
-
*
|
|
791
|
-
* await manager.push(new SendEmail('user@example.com'))
|
|
792
|
-
* ```
|
|
793
|
-
*/
|
|
794
|
-
declare class QueueManager {
|
|
795
|
-
private drivers;
|
|
796
|
-
private serializers;
|
|
797
|
-
private defaultConnection;
|
|
798
|
-
private defaultSerializer;
|
|
799
|
-
private persistence?;
|
|
800
|
-
private scheduler?;
|
|
801
|
-
private debug;
|
|
802
|
-
constructor(config?: QueueConfig);
|
|
803
|
-
/**
|
|
804
|
-
* Log debug message.
|
|
805
|
-
*/
|
|
806
|
-
private log;
|
|
807
|
-
/**
|
|
808
|
-
* Register a connection.
|
|
809
|
-
* @param name - Connection name
|
|
810
|
-
* @param config - Connection config
|
|
811
|
-
*/
|
|
812
|
-
registerConnection(name: string, config: QueueConnectionConfig): void;
|
|
813
|
-
/**
|
|
814
|
-
* Get a driver for a connection.
|
|
815
|
-
* @param connection - Connection name
|
|
816
|
-
* @returns Driver instance
|
|
817
|
-
*/
|
|
818
|
-
getDriver(connection: string): QueueDriver;
|
|
819
|
-
/**
|
|
820
|
-
* Get the default connection name.
|
|
821
|
-
* @returns Default connection name
|
|
822
|
-
*/
|
|
823
|
-
getDefaultConnection(): string;
|
|
824
|
-
/**
|
|
825
|
-
* Get a serializer.
|
|
826
|
-
* @param type - Serializer type
|
|
827
|
-
* @returns Serializer instance
|
|
828
|
-
*/
|
|
829
|
-
getSerializer(type?: string): JobSerializer;
|
|
830
|
-
/**
|
|
831
|
-
* Register Job classes (used by ClassNameSerializer).
|
|
832
|
-
* @param jobClasses - Job class array
|
|
833
|
-
*/
|
|
834
|
-
registerJobClasses(jobClasses: Array<new (...args: unknown[]) => Job>): void;
|
|
835
|
-
/**
|
|
836
|
-
* Push a Job to the queue.
|
|
837
|
-
*
|
|
838
|
-
* @template T - The type of the job.
|
|
839
|
-
* @param job - Job instance to push.
|
|
840
|
-
* @param options - Push options.
|
|
841
|
-
* @returns The same job instance (for fluent chaining).
|
|
842
|
-
*
|
|
843
|
-
* @example
|
|
844
|
-
* ```typescript
|
|
845
|
-
* await manager.push(new SendEmailJob('user@example.com'));
|
|
846
|
-
* ```
|
|
847
|
-
*/
|
|
848
|
-
push<T extends Job & Queueable>(job: T, options?: JobPushOptions): Promise<T>;
|
|
849
|
-
/**
|
|
850
|
-
* Push multiple jobs to the queue.
|
|
851
|
-
*
|
|
852
|
-
* @template T - The type of the jobs.
|
|
853
|
-
* @param jobs - Array of job instances.
|
|
854
|
-
* @param options - Bulk push options.
|
|
855
|
-
*
|
|
856
|
-
* @example
|
|
857
|
-
* ```typescript
|
|
858
|
-
* await manager.pushMany(jobs, { batchSize: 500, concurrency: 2 });
|
|
859
|
-
* ```
|
|
860
|
-
*/
|
|
861
|
-
pushMany<T extends Job & Queueable>(jobs: T[], options?: {
|
|
862
|
-
batchSize?: number;
|
|
863
|
-
concurrency?: number;
|
|
864
|
-
}): Promise<void>;
|
|
865
|
-
/**
|
|
866
|
-
* Pop a job from the queue.
|
|
867
|
-
*
|
|
868
|
-
* @param queue - Queue name (default: 'default').
|
|
869
|
-
* @param connection - Connection name (optional).
|
|
870
|
-
* @returns Job instance or null if queue is empty.
|
|
871
|
-
*
|
|
872
|
-
* @example
|
|
873
|
-
* ```typescript
|
|
874
|
-
* const job = await manager.pop('emails');
|
|
875
|
-
* if (job) await job.handle();
|
|
876
|
-
* ```
|
|
877
|
-
*/
|
|
878
|
-
pop(queue?: string, connection?: string): Promise<Job | null>;
|
|
879
|
-
/**
|
|
880
|
-
* Pop multiple jobs from the queue.
|
|
881
|
-
*
|
|
882
|
-
* @param queue - Queue name (default: 'default').
|
|
883
|
-
* @param count - Number of jobs to pop (default: 10).
|
|
884
|
-
* @param connection - Connection name (optional).
|
|
885
|
-
* @returns Array of Job instances.
|
|
886
|
-
*/
|
|
887
|
-
popMany(queue?: string, count?: number, connection?: string): Promise<Job[]>;
|
|
888
|
-
/**
|
|
889
|
-
* Get queue size.
|
|
890
|
-
*
|
|
891
|
-
* @param queue - Queue name (default: 'default').
|
|
892
|
-
* @param connection - Connection name (optional).
|
|
893
|
-
* @returns Number of jobs in the queue.
|
|
894
|
-
*/
|
|
895
|
-
size(queue?: string, connection?: string): Promise<number>;
|
|
896
|
-
/**
|
|
897
|
-
* Pop a job from the queue (blocking).
|
|
898
|
-
*
|
|
899
|
-
* @param queue - Queue name (default: 'default').
|
|
900
|
-
* @param timeout - Timeout in seconds (default: 0, wait forever).
|
|
901
|
-
* @param connection - Connection name (optional).
|
|
902
|
-
*/
|
|
903
|
-
popBlocking(queues?: string | string[], timeout?: number, connection?: string): Promise<Job | null>;
|
|
904
|
-
/**
|
|
905
|
-
* Clear all jobs from a queue.
|
|
906
|
-
*
|
|
907
|
-
* @param queue - Queue name (default: 'default').
|
|
908
|
-
* @param connection - Connection name (optional).
|
|
909
|
-
*/
|
|
910
|
-
clear(queue?: string, connection?: string): Promise<void>;
|
|
911
|
-
/**
|
|
912
|
-
* Get queue statistics including size, delayed, and failed job counts.
|
|
913
|
-
*
|
|
914
|
-
* @param queue - Queue name (default: 'default').
|
|
915
|
-
* @param connection - Connection name (optional).
|
|
916
|
-
* @returns Queue statistics object.
|
|
917
|
-
*/
|
|
918
|
-
stats(queue?: string, connection?: string): Promise<QueueStats>;
|
|
919
|
-
/**
|
|
920
|
-
* Mark a job as completed.
|
|
921
|
-
* @param job - Job instance
|
|
922
|
-
*/
|
|
923
|
-
complete<T extends Job & Queueable>(job: T): Promise<void>;
|
|
924
|
-
/**
|
|
925
|
-
* Mark a job as permanently failed.
|
|
926
|
-
* @param job - Job instance
|
|
927
|
-
* @param error - Error object
|
|
928
|
-
*/
|
|
929
|
-
fail<T extends Job & Queueable>(job: T, error: Error): Promise<void>;
|
|
930
|
-
/**
|
|
931
|
-
* Get the persistence adapter if configured.
|
|
932
|
-
*/
|
|
933
|
-
getPersistence(): PersistenceAdapter | undefined;
|
|
934
|
-
/**
|
|
935
|
-
* Get the scheduler if configured.
|
|
936
|
-
*/
|
|
937
|
-
getScheduler(): Scheduler;
|
|
938
|
-
/**
|
|
939
|
-
* Get failed jobs from DLQ (if driver supports it).
|
|
940
|
-
*/
|
|
941
|
-
getFailed(queue: string, start?: number, end?: number, connection?: string): Promise<SerializedJob[]>;
|
|
942
|
-
/**
|
|
943
|
-
* Retry failed jobs from DLQ (if driver supports it).
|
|
944
|
-
*/
|
|
945
|
-
retryFailed(queue: string, count?: number, connection?: string): Promise<number>;
|
|
946
|
-
/**
|
|
947
|
-
* Clear failed jobs from DLQ (if driver supports it).
|
|
948
|
-
*/
|
|
949
|
-
clearFailed(queue: string, connection?: string): Promise<void>;
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
/**
|
|
953
|
-
* Worker options.
|
|
954
|
-
*/
|
|
955
|
-
interface WorkerOptions {
|
|
956
|
-
/**
|
|
957
|
-
* Maximum retry attempts.
|
|
958
|
-
*/
|
|
959
|
-
maxAttempts?: number;
|
|
960
|
-
/**
|
|
961
|
-
* Job timeout (seconds).
|
|
962
|
-
*/
|
|
963
|
-
timeout?: number;
|
|
964
|
-
/**
|
|
965
|
-
* Failure callback.
|
|
966
|
-
*/
|
|
967
|
-
onFailed?: (job: Job, error: Error) => Promise<void>;
|
|
968
|
-
}
|
|
969
|
-
/**
|
|
970
|
-
* Base Worker.
|
|
971
|
-
*
|
|
972
|
-
* Responsible for executing `Job` instances.
|
|
973
|
-
* Provides error handling, retry logic, and timeout support.
|
|
974
|
-
*
|
|
975
|
-
* @example
|
|
976
|
-
* ```typescript
|
|
977
|
-
* const worker = new Worker({
|
|
978
|
-
* maxAttempts: 3,
|
|
979
|
-
* timeout: 60
|
|
980
|
-
* })
|
|
981
|
-
*
|
|
982
|
-
* await worker.process(job)
|
|
983
|
-
* ```
|
|
984
|
-
*/
|
|
985
|
-
declare class Worker {
|
|
986
|
-
private options;
|
|
987
|
-
constructor(options?: WorkerOptions);
|
|
988
|
-
/**
|
|
989
|
-
* Process a Job.
|
|
990
|
-
* @param job - Job instance
|
|
991
|
-
*/
|
|
992
|
-
process(job: Job): Promise<void>;
|
|
993
|
-
/**
|
|
994
|
-
* Handle failure.
|
|
995
|
-
*/
|
|
996
|
-
private handleFailure;
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
/**
|
|
1000
|
-
* Consumer options.
|
|
1001
|
-
*/
|
|
1002
|
-
interface ConsumerOptions {
|
|
1003
|
-
/**
|
|
1004
|
-
* Queues to listen on.
|
|
1005
|
-
*/
|
|
1006
|
-
queues: string[];
|
|
1007
|
-
/**
|
|
1008
|
-
* Connection name.
|
|
1009
|
-
*/
|
|
1010
|
-
connection?: string;
|
|
1011
|
-
/**
|
|
1012
|
-
* Worker options.
|
|
1013
|
-
*/
|
|
1014
|
-
workerOptions?: WorkerOptions;
|
|
1015
|
-
/**
|
|
1016
|
-
* Polling interval (milliseconds).
|
|
1017
|
-
*/
|
|
1018
|
-
pollInterval?: number;
|
|
1019
|
-
/**
|
|
1020
|
-
* Whether to keep polling when queues are empty.
|
|
1021
|
-
*/
|
|
1022
|
-
keepAlive?: boolean;
|
|
1023
|
-
/**
|
|
1024
|
-
* Monitoring options.
|
|
1025
|
-
*/
|
|
1026
|
-
monitor?: boolean | {
|
|
1027
|
-
/**
|
|
1028
|
-
* Heartbeat interval (milliseconds). Default: 5000.
|
|
1029
|
-
*/
|
|
1030
|
-
interval?: number;
|
|
1031
|
-
/**
|
|
1032
|
-
* Extra info to report with heartbeat.
|
|
1033
|
-
*/
|
|
1034
|
-
extraInfo?: Record<string, unknown>;
|
|
1035
|
-
/**
|
|
1036
|
-
* Prefix for monitoring keys/channels.
|
|
1037
|
-
*/
|
|
1038
|
-
prefix?: string;
|
|
1039
|
-
};
|
|
1040
|
-
/**
|
|
1041
|
-
* Rate limits per queue.
|
|
1042
|
-
* Example: { 'emails': { max: 10, duration: 1000 } }
|
|
1043
|
-
*/
|
|
1044
|
-
rateLimits?: Record<string, {
|
|
1045
|
-
max: number;
|
|
1046
|
-
duration: number;
|
|
1047
|
-
}>;
|
|
1048
|
-
/**
|
|
1049
|
-
* Max concurrent jobs to process. Default: 1.
|
|
1050
|
-
*/
|
|
1051
|
-
concurrency?: number;
|
|
1052
|
-
/**
|
|
1053
|
-
* Whether to process jobs with the same groupId sequentially.
|
|
1054
|
-
* If true, jobs with the same groupId will never run concurrently,
|
|
1055
|
-
* regardless of the global concurrency setting.
|
|
1056
|
-
* @default true
|
|
1057
|
-
*/
|
|
1058
|
-
groupJobsSequential?: boolean;
|
|
1059
|
-
/**
|
|
1060
|
-
* Minimum polling interval in ms (for adaptive polling).
|
|
1061
|
-
* @default 100
|
|
1062
|
-
*/
|
|
1063
|
-
minPollInterval?: number;
|
|
1064
|
-
/**
|
|
1065
|
-
* Maximum polling interval in ms (for adaptive polling).
|
|
1066
|
-
* @default 5000
|
|
1067
|
-
*/
|
|
1068
|
-
maxPollInterval?: number;
|
|
1069
|
-
/**
|
|
1070
|
-
* Backoff multiplier for adaptive polling.
|
|
1071
|
-
* @default 1.5
|
|
1072
|
-
*/
|
|
1073
|
-
backoffMultiplier?: number;
|
|
1074
|
-
/**
|
|
1075
|
-
* Batch size for consuming jobs.
|
|
1076
|
-
* If > 1, tries to fetch multiple jobs at once.
|
|
1077
|
-
* @default 1
|
|
1078
|
-
*/
|
|
1079
|
-
batchSize?: number;
|
|
1080
|
-
/**
|
|
1081
|
-
* Whether to use blocking pop (BLPOP/long-polling) if supported by driver.
|
|
1082
|
-
* Only applies when batchSize is 1.
|
|
1083
|
-
* @default true
|
|
1084
|
-
*/
|
|
1085
|
-
useBlocking?: boolean;
|
|
1086
|
-
/**
|
|
1087
|
-
* Timeout in seconds for blocking pop.
|
|
1088
|
-
* @default 5
|
|
1089
|
-
*/
|
|
1090
|
-
blockingTimeout?: number;
|
|
1091
|
-
/**
|
|
1092
|
-
* Enable verbose debug logging.
|
|
1093
|
-
* @default false
|
|
1094
|
-
*/
|
|
1095
|
-
debug?: boolean;
|
|
1096
|
-
}
|
|
1097
|
-
/**
|
|
1098
|
-
* Consumer
|
|
1099
|
-
*
|
|
1100
|
-
* Consumes and executes jobs from queues.
|
|
1101
|
-
* Supports embedded mode (inside the main app) and standalone mode (as a worker service).
|
|
1102
|
-
*
|
|
1103
|
-
* @example
|
|
1104
|
-
* ```typescript
|
|
1105
|
-
* // Embedded mode
|
|
1106
|
-
* const consumer = new Consumer(queueManager, {
|
|
1107
|
-
* queues: ['default', 'emails'],
|
|
1108
|
-
* pollInterval: 1000
|
|
1109
|
-
* })
|
|
1110
|
-
*
|
|
1111
|
-
* consumer.start()
|
|
1112
|
-
*
|
|
1113
|
-
* // Standalone mode (CLI)
|
|
1114
|
-
* // Start via CLI tooling with graceful shutdown
|
|
1115
|
-
* ```
|
|
1116
|
-
*
|
|
1117
|
-
* @emits job:started - When a job begins processing. Payload: { job: Job, queue: string }
|
|
1118
|
-
* @emits job:processed - When a job completes successfully. Payload: { job: Job, duration: number, queue: string }
|
|
1119
|
-
* @emits job:failed - When a job fails an attempt. Payload: { job: Job, error: Error, duration: number, queue: string }
|
|
1120
|
-
* @emits job:retried - When a job is scheduled for a retry. Payload: { job: Job, attempt: number, delay: number }
|
|
1121
|
-
* @emits job:failed_permanently - When a job fails all attempts and is moved to DLQ. Payload: { job: Job, error: Error }
|
|
1122
|
-
*/
|
|
1123
|
-
declare class Consumer extends EventEmitter {
|
|
1124
|
-
private queueManager;
|
|
1125
|
-
private options;
|
|
1126
|
-
private running;
|
|
1127
|
-
private stopRequested;
|
|
1128
|
-
private workerId;
|
|
1129
|
-
private heartbeatTimer;
|
|
1130
|
-
private groupLimiters;
|
|
1131
|
-
private stats;
|
|
1132
|
-
constructor(queueManager: QueueManager, options: ConsumerOptions);
|
|
1133
|
-
private get connectionName();
|
|
1134
|
-
/**
|
|
1135
|
-
* Log debug message.
|
|
1136
|
-
*/
|
|
1137
|
-
private log;
|
|
1138
|
-
/**
|
|
1139
|
-
* Start the consumer loop.
|
|
1140
|
-
*/
|
|
1141
|
-
start(): Promise<void>;
|
|
1142
|
-
/**
|
|
1143
|
-
* Run a job with concurrency controls.
|
|
1144
|
-
*/
|
|
1145
|
-
private runJob;
|
|
1146
|
-
/**
|
|
1147
|
-
* Handle a single job.
|
|
1148
|
-
*/
|
|
1149
|
-
private handleJob;
|
|
1150
|
-
private startHeartbeat;
|
|
1151
|
-
private stopHeartbeat;
|
|
1152
|
-
private publishLog;
|
|
1153
|
-
/**
|
|
1154
|
-
* Stop the consumer loop (graceful shutdown).
|
|
1155
|
-
*/
|
|
1156
|
-
stop(): Promise<void>;
|
|
1157
|
-
/**
|
|
1158
|
-
* Check whether the consumer is running.
|
|
1159
|
-
*/
|
|
1160
|
-
isRunning(): boolean;
|
|
1161
|
-
/**
|
|
1162
|
-
* Get current consumer statistics.
|
|
1163
|
-
*/
|
|
1164
|
-
getStats(): {
|
|
1165
|
-
processed: number;
|
|
1166
|
-
failed: number;
|
|
1167
|
-
retried: number;
|
|
1168
|
-
active: number;
|
|
1169
|
-
};
|
|
1170
|
-
/**
|
|
1171
|
-
* Reset statistics counters.
|
|
1172
|
-
*/
|
|
1173
|
-
resetStats(): void;
|
|
1174
|
-
}
|
|
1175
|
-
|
|
1176
|
-
/**
|
|
1177
|
-
* Generic database service interface.
|
|
1178
|
-
* Users should implement this interface with their preferred ORM/database client.
|
|
1179
|
-
*/
|
|
1180
|
-
interface DatabaseService {
|
|
1181
|
-
/**
|
|
1182
|
-
* Execute a raw SQL query.
|
|
1183
|
-
* @param sql - The SQL query string with placeholders ($1, $2, etc.)
|
|
1184
|
-
* @param bindings - The values to bind to placeholders
|
|
1185
|
-
*/
|
|
1186
|
-
execute<T = unknown>(sql: string, bindings?: unknown[]): Promise<T[] | T>;
|
|
1187
|
-
/**
|
|
1188
|
-
* Execute multiple queries within a transaction.
|
|
1189
|
-
* @param callback - The callback to execute within the transaction
|
|
1190
|
-
*/
|
|
1191
|
-
transaction<T>(callback: (tx: DatabaseService) => Promise<T>): Promise<T>;
|
|
1192
|
-
}
|
|
1193
|
-
/**
|
|
1194
|
-
* Database driver configuration.
|
|
1195
|
-
*/
|
|
1196
|
-
interface DatabaseDriverConfig {
|
|
1197
|
-
/**
|
|
1198
|
-
* Table name (default: `jobs`).
|
|
1199
|
-
*/
|
|
1200
|
-
table?: string;
|
|
1201
|
-
/**
|
|
1202
|
-
* Database service instance that implements DatabaseService interface.
|
|
1203
|
-
*/
|
|
1204
|
-
dbService?: DatabaseService;
|
|
1205
|
-
}
|
|
1206
|
-
/**
|
|
1207
|
-
* Database Driver
|
|
1208
|
-
*
|
|
1209
|
-
* Uses a database as the queue backend.
|
|
1210
|
-
* Works with any database service that implements the DatabaseService interface.
|
|
1211
|
-
*
|
|
1212
|
-
* @example
|
|
1213
|
-
* ```typescript
|
|
1214
|
-
* // Create a database service adapter
|
|
1215
|
-
* const dbService = {
|
|
1216
|
-
* execute: async (sql, bindings) => yourDbClient.query(sql, bindings),
|
|
1217
|
-
* transaction: async (callback) => yourDbClient.transaction(callback),
|
|
1218
|
-
* }
|
|
1219
|
-
*
|
|
1220
|
-
* const driver = new DatabaseDriver({ dbService, table: 'jobs' })
|
|
1221
|
-
* await driver.push('default', serializedJob)
|
|
1222
|
-
* ```
|
|
1223
|
-
*/
|
|
1224
|
-
declare class DatabaseDriver implements QueueDriver {
|
|
1225
|
-
private tableName;
|
|
1226
|
-
private dbService;
|
|
1227
|
-
constructor(config: DatabaseDriverConfig);
|
|
1228
|
-
/**
|
|
1229
|
-
* Push a job to a queue.
|
|
1230
|
-
*/
|
|
1231
|
-
push(queue: string, job: SerializedJob): Promise<void>;
|
|
1232
|
-
/**
|
|
1233
|
-
* Pop a job from the queue (FIFO, with delay support).
|
|
1234
|
-
*/
|
|
1235
|
-
pop(queue: string): Promise<SerializedJob | null>;
|
|
1236
|
-
/**
|
|
1237
|
-
* Pop multiple jobs from the queue.
|
|
1238
|
-
*/
|
|
1239
|
-
popMany(queue: string, count: number): Promise<SerializedJob[]>;
|
|
1240
|
-
/**
|
|
1241
|
-
* Get queue statistics.
|
|
1242
|
-
*/
|
|
1243
|
-
stats(queue: string): Promise<QueueStats>;
|
|
1244
|
-
/**
|
|
1245
|
-
* Get queue size.
|
|
1246
|
-
*/
|
|
1247
|
-
size(queue: string): Promise<number>;
|
|
1248
|
-
/**
|
|
1249
|
-
* Clear a queue.
|
|
1250
|
-
*/
|
|
1251
|
-
clear(queue: string): Promise<void>;
|
|
1252
|
-
/**
|
|
1253
|
-
* Pop a job from the queue (blocking).
|
|
1254
|
-
* Simple polling fallback for databases.
|
|
1255
|
-
*/
|
|
1256
|
-
popBlocking(queue: string, timeout: number): Promise<SerializedJob | null>;
|
|
1257
|
-
/**
|
|
1258
|
-
* Push multiple jobs.
|
|
1259
|
-
* Optimizes by using a single multi-row insert if possible.
|
|
1260
|
-
*/
|
|
1261
|
-
pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
|
|
1262
|
-
/**
|
|
1263
|
-
* Mark a job as failed (DLQ).
|
|
1264
|
-
*/
|
|
1265
|
-
fail(queue: string, job: SerializedJob): Promise<void>;
|
|
1266
|
-
/**
|
|
1267
|
-
* Acknowledge/Complete a job.
|
|
1268
|
-
*/
|
|
1269
|
-
complete(_queue: string, job: SerializedJob): Promise<void>;
|
|
1270
|
-
}
|
|
1271
|
-
|
|
1272
|
-
/**
|
|
1273
|
-
* Kafka driver configuration.
|
|
1274
|
-
*/
|
|
1275
|
-
interface KafkaDriverConfig {
|
|
1276
|
-
/**
|
|
1277
|
-
* Kafka client instance (kafkajs).
|
|
1278
|
-
*/
|
|
1279
|
-
client: {
|
|
1280
|
-
producer: () => {
|
|
1281
|
-
connect: () => Promise<void>;
|
|
1282
|
-
send: (args: {
|
|
1283
|
-
topic: string;
|
|
1284
|
-
messages: Array<{
|
|
1285
|
-
key?: string;
|
|
1286
|
-
value: string;
|
|
1287
|
-
}>;
|
|
1288
|
-
}) => Promise<void>;
|
|
1289
|
-
disconnect: () => Promise<void>;
|
|
1290
|
-
};
|
|
1291
|
-
admin: () => {
|
|
1292
|
-
connect: () => Promise<void>;
|
|
1293
|
-
createTopics: (args: {
|
|
1294
|
-
topics: Array<{
|
|
1295
|
-
topic: string;
|
|
1296
|
-
numPartitions?: number;
|
|
1297
|
-
replicationFactor?: number;
|
|
1298
|
-
}>;
|
|
1299
|
-
}) => Promise<void>;
|
|
1300
|
-
deleteTopics: (args: {
|
|
1301
|
-
topics: string[];
|
|
1302
|
-
}) => Promise<void>;
|
|
1303
|
-
disconnect: () => Promise<void>;
|
|
1304
|
-
};
|
|
1305
|
-
consumer: (args: {
|
|
1306
|
-
groupId: string;
|
|
1307
|
-
}) => {
|
|
1308
|
-
connect: () => Promise<void>;
|
|
1309
|
-
subscribe: (args: {
|
|
1310
|
-
topics: string[];
|
|
1311
|
-
}) => Promise<void>;
|
|
1312
|
-
run: (args: {
|
|
1313
|
-
eachMessage: (args: {
|
|
1314
|
-
topic: string;
|
|
1315
|
-
partition: number;
|
|
1316
|
-
message: {
|
|
1317
|
-
key?: Buffer;
|
|
1318
|
-
value: Buffer;
|
|
1319
|
-
offset: string;
|
|
1320
|
-
};
|
|
1321
|
-
}) => Promise<void>;
|
|
1322
|
-
}) => Promise<void>;
|
|
1323
|
-
disconnect: () => Promise<void>;
|
|
1324
|
-
};
|
|
1325
|
-
};
|
|
1326
|
-
/**
|
|
1327
|
-
* Consumer group ID (for consuming messages).
|
|
1328
|
-
*/
|
|
1329
|
-
consumerGroupId?: string;
|
|
1330
|
-
}
|
|
1331
|
-
/**
|
|
1332
|
-
* Kafka Driver
|
|
1333
|
-
*
|
|
1334
|
-
* Uses Apache Kafka as the queue backend.
|
|
1335
|
-
* Supports topic management, consumer groups, and batch operations.
|
|
1336
|
-
*
|
|
1337
|
-
* Requires `kafkajs`.
|
|
1338
|
-
*
|
|
1339
|
-
* @example
|
|
1340
|
-
* ```typescript
|
|
1341
|
-
* import { Kafka } from 'kafkajs'
|
|
1342
|
-
*
|
|
1343
|
-
* const kafka = new Kafka({
|
|
1344
|
-
* brokers: ['localhost:9092'],
|
|
1345
|
-
* clientId: 'gravito-app'
|
|
1346
|
-
* })
|
|
1347
|
-
*
|
|
1348
|
-
* const driver = new KafkaDriver({ client: kafka, consumerGroupId: 'workers' })
|
|
1349
|
-
* await driver.push('default', serializedJob)
|
|
1350
|
-
* ```
|
|
1351
|
-
*/
|
|
1352
|
-
declare class KafkaDriver implements QueueDriver {
|
|
1353
|
-
private client;
|
|
1354
|
-
private consumerGroupId;
|
|
1355
|
-
private producer?;
|
|
1356
|
-
private admin?;
|
|
1357
|
-
constructor(config: KafkaDriverConfig);
|
|
1358
|
-
/**
|
|
1359
|
-
* Ensure the producer is connected.
|
|
1360
|
-
*/
|
|
1361
|
-
private ensureProducer;
|
|
1362
|
-
/**
|
|
1363
|
-
* Ensure the admin client is connected.
|
|
1364
|
-
*/
|
|
1365
|
-
private ensureAdmin;
|
|
1366
|
-
/**
|
|
1367
|
-
* Push a job to a topic.
|
|
1368
|
-
*/
|
|
1369
|
-
push(queue: string, job: SerializedJob): Promise<void>;
|
|
1370
|
-
/**
|
|
1371
|
-
* Pop is not supported for Kafka.
|
|
1372
|
-
*
|
|
1373
|
-
* Note: Kafka uses a push-based model, so you should use `subscribe()`.
|
|
1374
|
-
*/
|
|
1375
|
-
pop(_queue: string): Promise<SerializedJob | null>;
|
|
1376
|
-
/**
|
|
1377
|
-
* Kafka does not provide a direct queue size.
|
|
1378
|
-
*
|
|
1379
|
-
* Returns 0; use Kafka tooling/metrics for lag/size insights.
|
|
1380
|
-
*/
|
|
1381
|
-
size(_queue: string): Promise<number>;
|
|
1382
|
-
/**
|
|
1383
|
-
* Clear a queue by deleting the topic.
|
|
1384
|
-
*/
|
|
1385
|
-
clear(queue: string): Promise<void>;
|
|
1386
|
-
/**
|
|
1387
|
-
* Push multiple jobs.
|
|
1388
|
-
*/
|
|
1389
|
-
pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
|
|
1390
|
-
/**
|
|
1391
|
-
* Create a topic.
|
|
1392
|
-
*/
|
|
1393
|
-
createTopic(topic: string, options?: TopicOptions): Promise<void>;
|
|
1394
|
-
/**
|
|
1395
|
-
* Delete a topic.
|
|
1396
|
-
*/
|
|
1397
|
-
deleteTopic(topic: string): Promise<void>;
|
|
1398
|
-
/**
|
|
1399
|
-
* Subscribe to a topic (push-based model).
|
|
1400
|
-
*/
|
|
1401
|
-
subscribe(queue: string, callback: (job: SerializedJob) => Promise<void>): Promise<void>;
|
|
1402
|
-
}
|
|
1403
|
-
|
|
1404
|
-
interface MemoryDriverConfig {
|
|
1405
|
-
/**
|
|
1406
|
-
* Maximum number of jobs per queue.
|
|
1407
|
-
* @default Infinity
|
|
1408
|
-
*/
|
|
1409
|
-
maxSize?: number;
|
|
1410
|
-
}
|
|
1411
|
-
/**
|
|
1412
|
-
* Memory Driver
|
|
1413
|
-
*
|
|
1414
|
-
* In-memory driver for development and testing.
|
|
1415
|
-
* All data is stored in memory and will be lost when the process restarts.
|
|
1416
|
-
*
|
|
1417
|
-
* Zero-config: works out of the box.
|
|
1418
|
-
*
|
|
1419
|
-
* @example
|
|
1420
|
-
* ```typescript
|
|
1421
|
-
* const driver = new MemoryDriver({ maxSize: 1000 })
|
|
1422
|
-
* await driver.push('default', serializedJob)
|
|
1423
|
-
* const job = await driver.pop('default')
|
|
1424
|
-
* ```
|
|
1425
|
-
*/
|
|
1426
|
-
declare class MemoryDriver implements QueueDriver {
|
|
1427
|
-
private queues;
|
|
1428
|
-
private maxSize;
|
|
1429
|
-
constructor(config?: MemoryDriverConfig);
|
|
1430
|
-
/**
|
|
1431
|
-
* Push a job to a queue.
|
|
1432
|
-
*/
|
|
1433
|
-
push(queue: string, job: SerializedJob): Promise<void>;
|
|
1434
|
-
/**
|
|
1435
|
-
* Pop a job from a queue (FIFO).
|
|
1436
|
-
*/
|
|
1437
|
-
pop(queue: string): Promise<SerializedJob | null>;
|
|
1438
|
-
/**
|
|
1439
|
-
* Get queue size.
|
|
1440
|
-
*/
|
|
1441
|
-
size(queue: string): Promise<number>;
|
|
1442
|
-
/**
|
|
1443
|
-
* Clear a queue.
|
|
1444
|
-
*/
|
|
1445
|
-
clear(queue: string): Promise<void>;
|
|
1446
|
-
/**
|
|
1447
|
-
* Mark a job as permanently failed.
|
|
1448
|
-
*/
|
|
1449
|
-
fail(queue: string, job: SerializedJob): Promise<void>;
|
|
1450
|
-
/**
|
|
1451
|
-
* Get queue statistics.
|
|
1452
|
-
*/
|
|
1453
|
-
stats(queue: string): Promise<QueueStats>;
|
|
1454
|
-
/**
|
|
1455
|
-
* Push multiple jobs.
|
|
1456
|
-
*/
|
|
1457
|
-
pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
|
|
1458
|
-
/**
|
|
1459
|
-
* Pop multiple jobs.
|
|
1460
|
-
*/
|
|
1461
|
-
popMany(queue: string, count: number): Promise<SerializedJob[]>;
|
|
1462
|
-
}
|
|
1463
|
-
|
|
1464
|
-
/**
|
|
1465
|
-
* RabbitMQ driver configuration.
|
|
1466
|
-
*/
|
|
1467
|
-
interface RabbitMQDriverConfig {
|
|
1468
|
-
/**
|
|
1469
|
-
* RabbitMQ client (amqplib) Connection or Channel.
|
|
1470
|
-
* If a Connection is provided, the driver will create and manage a Channel.
|
|
1471
|
-
*/
|
|
1472
|
-
client: any;
|
|
1473
|
-
/**
|
|
1474
|
-
* Exchange name (optional).
|
|
1475
|
-
*/
|
|
1476
|
-
exchange?: string;
|
|
1477
|
-
/**
|
|
1478
|
-
* Exchange type (default: 'fanout').
|
|
1479
|
-
*/
|
|
1480
|
-
exchangeType?: 'direct' | 'topic' | 'headers' | 'fanout' | 'match';
|
|
1481
|
-
}
|
|
1482
|
-
/**
|
|
1483
|
-
* RabbitMQ Driver
|
|
1484
|
-
*
|
|
1485
|
-
* Uses RabbitMQ as the queue backend.
|
|
1486
|
-
* Implements FIFO via RabbitMQ Queues.
|
|
1487
|
-
*
|
|
1488
|
-
* Requires `amqplib`.
|
|
1489
|
-
*
|
|
1490
|
-
* @example
|
|
1491
|
-
* ```typescript
|
|
1492
|
-
* import amqp from 'amqplib'
|
|
1493
|
-
*
|
|
1494
|
-
* const connection = await amqp.connect('amqp://localhost')
|
|
1495
|
-
* const driver = new RabbitMQDriver({ client: connection })
|
|
1496
|
-
*
|
|
1497
|
-
* await driver.push('default', serializedJob)
|
|
1498
|
-
* ```
|
|
1499
|
-
*/
|
|
1500
|
-
declare class RabbitMQDriver implements QueueDriver {
|
|
1501
|
-
private connection;
|
|
1502
|
-
private channel;
|
|
1503
|
-
private exchange?;
|
|
1504
|
-
private exchangeType;
|
|
1505
|
-
constructor(config: RabbitMQDriverConfig);
|
|
1506
|
-
/**
|
|
1507
|
-
* Ensure channel is created.
|
|
1508
|
-
*/
|
|
1509
|
-
ensureChannel(): Promise<any>;
|
|
1510
|
-
/**
|
|
1511
|
-
* Get the underlying connection.
|
|
1512
|
-
*/
|
|
1513
|
-
getRawConnection(): any;
|
|
1514
|
-
/**
|
|
1515
|
-
* Push a job (sendToQueue / publish).
|
|
1516
|
-
*/
|
|
1517
|
-
push(queue: string, job: SerializedJob): Promise<void>;
|
|
1518
|
-
/**
|
|
1519
|
-
* Pop a job (get).
|
|
1520
|
-
*/
|
|
1521
|
-
pop(queue: string): Promise<SerializedJob | null>;
|
|
1522
|
-
/**
|
|
1523
|
-
* Pop multiple jobs.
|
|
1524
|
-
* Uses channel.get() in a loop (no native batch get in AMQP).
|
|
1525
|
-
*/
|
|
1526
|
-
popMany(queue: string, count: number): Promise<SerializedJob[]>;
|
|
1527
|
-
/**
|
|
1528
|
-
* Acknowledge a message.
|
|
1529
|
-
*/
|
|
1530
|
-
acknowledge(messageId: string): Promise<void>;
|
|
1531
|
-
/**
|
|
1532
|
-
* Negative acknowledge a message.
|
|
1533
|
-
*/
|
|
1534
|
-
nack(message: any, requeue?: boolean): Promise<void>;
|
|
1535
|
-
/**
|
|
1536
|
-
* Reject a message.
|
|
1537
|
-
*/
|
|
1538
|
-
reject(message: any, requeue?: boolean): Promise<void>;
|
|
1539
|
-
/**
|
|
1540
|
-
* Subscribe to a queue.
|
|
1541
|
-
*/
|
|
1542
|
-
subscribe(queue: string, callback: (job: SerializedJob) => Promise<void>, options?: {
|
|
1543
|
-
autoAck?: boolean;
|
|
1544
|
-
prefetch?: number;
|
|
1545
|
-
}): Promise<void>;
|
|
1546
|
-
/**
|
|
1547
|
-
* Get queue size.
|
|
1548
|
-
*/
|
|
1549
|
-
size(queue: string): Promise<number>;
|
|
1550
|
-
/**
|
|
1551
|
-
* Clear a queue.
|
|
1552
|
-
*/
|
|
1553
|
-
clear(queue: string): Promise<void>;
|
|
1554
|
-
}
|
|
1555
|
-
|
|
1556
|
-
/**
|
|
1557
|
-
* Interface for Redis clients (compatible with ioredis and node-redis).
|
|
1558
|
-
*/
|
|
1559
|
-
interface RedisClient {
|
|
1560
|
-
lpush(key: string, ...values: string[]): Promise<number>;
|
|
1561
|
-
rpop(key: string, count?: number): Promise<string | string[] | null>;
|
|
1562
|
-
llen(key: string): Promise<number>;
|
|
1563
|
-
del(key: string, ...keys: string[]): Promise<number>;
|
|
1564
|
-
lpushx?(key: string, ...values: string[]): Promise<number>;
|
|
1565
|
-
rpoplpush?(src: string, dst: string): Promise<string | null>;
|
|
1566
|
-
zadd?(key: string, score: number, member: string): Promise<number>;
|
|
1567
|
-
zrange?(key: string, start: number, end: number, ...args: string[]): Promise<string[]>;
|
|
1568
|
-
zrem?(key: string, ...members: string[]): Promise<number>;
|
|
1569
|
-
get?(key: string): Promise<string | null>;
|
|
1570
|
-
set?(key: string, value: string, ...args: any[]): Promise<'OK' | null>;
|
|
1571
|
-
ltrim?(key: string, start: number, stop: number): Promise<'OK'>;
|
|
1572
|
-
lrange?(key: string, start: number, stop: number): Promise<string[]>;
|
|
1573
|
-
publish?(channel: string, message: string): Promise<number>;
|
|
1574
|
-
pipeline?(): any;
|
|
1575
|
-
defineCommand?(name: string, options: {
|
|
1576
|
-
numberOfKeys: number;
|
|
1577
|
-
lua: string;
|
|
1578
|
-
}): void;
|
|
1579
|
-
incr?(key: string): Promise<number>;
|
|
1580
|
-
expire?(key: string, seconds: number): Promise<number>;
|
|
1581
|
-
eval(script: string, numKeys: number, ...args: (string | number)[]): Promise<any>;
|
|
1582
|
-
[key: string]: any;
|
|
1583
|
-
}
|
|
1584
|
-
/**
|
|
1585
|
-
* Redis driver configuration.
|
|
1586
|
-
*/
|
|
1587
|
-
interface RedisDriverConfig {
|
|
1588
|
-
/**
|
|
1589
|
-
* Redis client instance (ioredis or node-redis).
|
|
1590
|
-
*/
|
|
1591
|
-
client: RedisClient;
|
|
1592
|
-
/**
|
|
1593
|
-
* Key prefix (default: `queue:`).
|
|
1594
|
-
*/
|
|
1595
|
-
prefix?: string;
|
|
1596
|
-
}
|
|
1597
|
-
/**
|
|
1598
|
-
* Redis Driver
|
|
1599
|
-
*
|
|
1600
|
-
* Uses Redis as the queue backend.
|
|
1601
|
-
* Implements FIFO via Redis Lists (LPUSH/RPOP).
|
|
1602
|
-
*
|
|
1603
|
-
* Requires `ioredis` or `redis`.
|
|
1604
|
-
*
|
|
1605
|
-
* @example
|
|
1606
|
-
* ```typescript
|
|
1607
|
-
* import Redis from 'ioredis'
|
|
1608
|
-
*
|
|
1609
|
-
* const redis = new Redis('redis://localhost:6379')
|
|
1610
|
-
* const redis = new Redis('ioredis://localhost:6379')
|
|
1611
|
-
* const driver = new RedisDriver({ client: redis })
|
|
1612
|
-
*
|
|
1613
|
-
* await driver.push('default', serializedJob)
|
|
1614
|
-
* ```
|
|
1615
|
-
*/
|
|
1616
|
-
declare class RedisDriver implements QueueDriver {
|
|
1617
|
-
private prefix;
|
|
1618
|
-
private client;
|
|
1619
|
-
private static PUSH_SCRIPT;
|
|
1620
|
-
private static COMPLETE_SCRIPT;
|
|
1621
|
-
private static POP_MANY_SCRIPT;
|
|
1622
|
-
constructor(config: RedisDriverConfig);
|
|
1623
|
-
/**
|
|
1624
|
-
* Get full Redis key for a queue.
|
|
1625
|
-
*/
|
|
1626
|
-
private getKey;
|
|
1627
|
-
/**
|
|
1628
|
-
* Push a job (LPUSH).
|
|
1629
|
-
*/
|
|
1630
|
-
push(queue: string, job: SerializedJob, options?: JobPushOptions): Promise<void>;
|
|
1631
|
-
/**
|
|
1632
|
-
* Complete a job (handle Group FIFO).
|
|
1633
|
-
*/
|
|
1634
|
-
complete(queue: string, job: SerializedJob): Promise<void>;
|
|
1635
|
-
/**
|
|
1636
|
-
* Pop a job from a queue (non-blocking).
|
|
1637
|
-
* Optimized with Lua script for atomic priority polling.
|
|
1638
|
-
*/
|
|
1639
|
-
pop(queue: string): Promise<SerializedJob | null>;
|
|
1640
|
-
/**
|
|
1641
|
-
* Manual fallback for pop if Lua fails.
|
|
1642
|
-
*/
|
|
1643
|
-
private popManualFallback;
|
|
1644
|
-
/**
|
|
1645
|
-
* Pop a job from the queue (blocking).
|
|
1646
|
-
* Uses BRPOP for efficiency. Supports multiple queues and priorities.
|
|
1647
|
-
*/
|
|
1648
|
-
popBlocking(queues: string | string[], timeout: number): Promise<SerializedJob | null>;
|
|
1649
|
-
/**
|
|
1650
|
-
* Parse Redis payload.
|
|
1651
|
-
*/
|
|
1652
|
-
private parsePayload;
|
|
1653
|
-
/**
|
|
1654
|
-
* Get queue size.
|
|
1655
|
-
*/
|
|
1656
|
-
size(queue: string): Promise<number>;
|
|
1657
|
-
/**
|
|
1658
|
-
* Mark a job as permanently failed (DLQ).
|
|
1659
|
-
*/
|
|
1660
|
-
fail(queue: string, job: SerializedJob): Promise<void>;
|
|
1661
|
-
/**
|
|
1662
|
-
* Clear a queue.
|
|
1663
|
-
*/
|
|
1664
|
-
clear(queue: string): Promise<void>;
|
|
1665
|
-
/**
|
|
1666
|
-
* Get queue statistics.
|
|
1667
|
-
* Optimized with Redis Pipeline to fetch all priorities and DLQ stats in one trip.
|
|
1668
|
-
*/
|
|
1669
|
-
stats(queue: string): Promise<QueueStats>;
|
|
1670
|
-
/**
|
|
1671
|
-
* Push multiple jobs.
|
|
1672
|
-
*/
|
|
1673
|
-
pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
|
|
1674
|
-
/**
|
|
1675
|
-
* Pop multiple jobs.
|
|
1676
|
-
* Atomic operation across multiple priority levels.
|
|
1677
|
-
*/
|
|
1678
|
-
popMany(queue: string, count: number): Promise<SerializedJob[]>;
|
|
1679
|
-
/**
|
|
1680
|
-
* Report worker heartbeat for monitoring.
|
|
1681
|
-
*/
|
|
1682
|
-
reportHeartbeat(workerInfo: any, prefix?: string): Promise<void>;
|
|
1683
|
-
/**
|
|
1684
|
-
* Publish a log message for monitoring.
|
|
1685
|
-
*/
|
|
1686
|
-
publishLog(logPayload: any, prefix?: string): Promise<void>;
|
|
1687
|
-
/**
|
|
1688
|
-
* Check if a queue is rate limited.
|
|
1689
|
-
* Uses a fixed window counter.
|
|
1690
|
-
*/
|
|
1691
|
-
checkRateLimit(queue: string, config: {
|
|
1692
|
-
max: number;
|
|
1693
|
-
duration: number;
|
|
1694
|
-
}): Promise<boolean>;
|
|
1695
|
-
/**
|
|
1696
|
-
* Get failed jobs from DLQ.
|
|
1697
|
-
*/
|
|
1698
|
-
getFailed(queue: string, start?: number, end?: number): Promise<SerializedJob[]>;
|
|
1699
|
-
/**
|
|
1700
|
-
* Retry failed jobs from DLQ.
|
|
1701
|
-
* Moves jobs from failed list back to the main queue.
|
|
1702
|
-
*/
|
|
1703
|
-
retryFailed(queue: string, count?: number): Promise<number>;
|
|
1704
|
-
/**
|
|
1705
|
-
* Clear failed jobs from DLQ.
|
|
1706
|
-
*/
|
|
1707
|
-
clearFailed(queue: string): Promise<void>;
|
|
1708
|
-
}
|
|
1709
|
-
|
|
1710
|
-
/**
|
|
1711
|
-
* SQS driver configuration.
|
|
1712
|
-
*/
|
|
1713
|
-
interface SQSDriverConfig {
|
|
1714
|
-
/**
|
|
1715
|
-
* SQS client instance (`@aws-sdk/client-sqs`).
|
|
1716
|
-
*/
|
|
1717
|
-
client: {
|
|
1718
|
-
send: (command: unknown) => Promise<{
|
|
1719
|
-
MessageId?: string;
|
|
1720
|
-
Messages?: Array<{
|
|
1721
|
-
MessageId?: string;
|
|
1722
|
-
ReceiptHandle?: string;
|
|
1723
|
-
Body?: string;
|
|
1724
|
-
}>;
|
|
1725
|
-
}>;
|
|
1726
|
-
};
|
|
1727
|
-
/**
|
|
1728
|
-
* Queue URL prefix (used to build full queue URLs).
|
|
1729
|
-
*/
|
|
1730
|
-
queueUrlPrefix?: string;
|
|
1731
|
-
/**
|
|
1732
|
-
* Visibility timeout (seconds, default: 30).
|
|
1733
|
-
*/
|
|
1734
|
-
visibilityTimeout?: number;
|
|
1735
|
-
/**
|
|
1736
|
-
* Long-polling duration (seconds, default: 20).
|
|
1737
|
-
*/
|
|
1738
|
-
waitTimeSeconds?: number;
|
|
1739
|
-
}
|
|
1740
|
-
/**
|
|
1741
|
-
* SQS Driver
|
|
1742
|
-
*
|
|
1743
|
-
* Uses AWS SQS as the queue backend.
|
|
1744
|
-
* Supports standard/FIFO queues, long polling, DLQ setups, etc.
|
|
1745
|
-
*
|
|
1746
|
-
* Requires `@aws-sdk/client-sqs`.
|
|
1747
|
-
*
|
|
1748
|
-
* @example
|
|
1749
|
-
* ```typescript
|
|
1750
|
-
* import { SQSClient } from '@aws-sdk/client-sqs'
|
|
1751
|
-
*
|
|
1752
|
-
* const sqs = new SQSClient({
|
|
1753
|
-
* region: 'us-east-1',
|
|
1754
|
-
* credentials: {
|
|
1755
|
-
* accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
|
1756
|
-
* secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
|
|
1757
|
-
* }
|
|
1758
|
-
* })
|
|
1759
|
-
*
|
|
1760
|
-
* const driver = new SQSDriver({ client: sqs })
|
|
1761
|
-
* await driver.push('default', serializedJob)
|
|
1762
|
-
* ```
|
|
1763
|
-
*/
|
|
1764
|
-
declare class SQSDriver implements QueueDriver {
|
|
1765
|
-
private client;
|
|
1766
|
-
private queueUrlPrefix;
|
|
1767
|
-
private visibilityTimeout;
|
|
1768
|
-
private waitTimeSeconds;
|
|
1769
|
-
private queueUrls;
|
|
1770
|
-
constructor(config: SQSDriverConfig);
|
|
1771
|
-
/**
|
|
1772
|
-
* Resolve the full queue URL.
|
|
1773
|
-
*/
|
|
1774
|
-
private getQueueUrl;
|
|
1775
|
-
/**
|
|
1776
|
-
* Push a job to SQS.
|
|
1777
|
-
*/
|
|
1778
|
-
push(queue: string, job: SerializedJob): Promise<void>;
|
|
1779
|
-
/**
|
|
1780
|
-
* Pop a job (long polling).
|
|
1781
|
-
*/
|
|
1782
|
-
pop(queue: string): Promise<SerializedJob | null>;
|
|
1783
|
-
/**
|
|
1784
|
-
* Pop multiple jobs.
|
|
1785
|
-
* Leverages SQS MaxNumberOfMessages (up to 10).
|
|
1786
|
-
*/
|
|
1787
|
-
popMany(queue: string, count: number): Promise<SerializedJob[]>;
|
|
1788
|
-
/**
|
|
1789
|
-
* Get queue size (approximate).
|
|
1790
|
-
*/
|
|
1791
|
-
size(queue: string): Promise<number>;
|
|
1792
|
-
/**
|
|
1793
|
-
* Clear a queue by receiving and deleting messages.
|
|
1794
|
-
*
|
|
1795
|
-
* Note: SQS does not provide a direct "purge" API via this wrapper. This method will
|
|
1796
|
-
* keep receiving and deleting messages until the queue is empty.
|
|
1797
|
-
*/
|
|
1798
|
-
clear(queue: string): Promise<void>;
|
|
1799
|
-
/**
|
|
1800
|
-
* Push multiple jobs.
|
|
1801
|
-
*/
|
|
1802
|
-
pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
|
|
1803
|
-
/**
|
|
1804
|
-
* Acknowledge is not supported via messageId.
|
|
1805
|
-
*/
|
|
1806
|
-
acknowledge(_messageId: string): Promise<void>;
|
|
1807
|
-
/**
|
|
1808
|
-
* Delete a message (acknowledge processing completion).
|
|
1809
|
-
*/
|
|
1810
|
-
deleteMessage(queue: string, receiptHandle: string): Promise<void>;
|
|
1811
|
-
}
|
|
1812
|
-
|
|
1813
|
-
/**
|
|
1814
|
-
* Options for configuring OrbitStream (Queue Orbit).
|
|
1815
|
-
* @public
|
|
1816
|
-
*/
|
|
1817
|
-
interface OrbitStreamOptions extends QueueConfig {
|
|
1818
|
-
/**
|
|
1819
|
-
* Whether to automatically start an embedded worker in development mode.
|
|
1820
|
-
* Useful for simple local testing without running a separate worker process.
|
|
1821
|
-
*/
|
|
1822
|
-
autoStartWorker?: boolean;
|
|
1823
|
-
/**
|
|
1824
|
-
* Configuration for the embedded worker/consumer.
|
|
1825
|
-
*/
|
|
1826
|
-
workerOptions?: ConsumerOptions;
|
|
1827
|
-
}
|
|
1828
|
-
/**
|
|
1829
|
-
* OrbitStream provides a powerful, multi-driver queue system for Gravito.
|
|
1830
|
-
* It integrates with various backends (Redis, Database, SQS, RabbitMQ)
|
|
1831
|
-
* and supports job serialization, delayed jobs, and FIFO processing.
|
|
1832
|
-
*
|
|
1833
|
-
* @example
|
|
1834
|
-
* ```typescript
|
|
1835
|
-
* const stream = new OrbitStream({
|
|
1836
|
-
* default: 'redis',
|
|
1837
|
-
* connections: {
|
|
1838
|
-
* redis: { driver: 'redis', host: 'localhost' }
|
|
1839
|
-
* }
|
|
1840
|
-
* });
|
|
1841
|
-
* core.addOrbit(stream);
|
|
1842
|
-
* ```
|
|
1843
|
-
* @public
|
|
1844
|
-
*/
|
|
1845
|
-
declare class OrbitStream implements GravitoOrbit {
|
|
1846
|
-
private options;
|
|
1847
|
-
private queueManager?;
|
|
1848
|
-
private consumer?;
|
|
1849
|
-
constructor(options?: OrbitStreamOptions);
|
|
1850
|
-
/**
|
|
1851
|
-
* Static configuration helper.
|
|
1852
|
-
*/
|
|
1853
|
-
static configure(options: OrbitStreamOptions): OrbitStream;
|
|
1854
|
-
/**
|
|
1855
|
-
* Install into PlanetCore.
|
|
1856
|
-
*/
|
|
1857
|
-
install(core: PlanetCore): void;
|
|
1858
|
-
/**
|
|
1859
|
-
* Start embedded worker.
|
|
1860
|
-
*/
|
|
1861
|
-
startWorker(options: ConsumerOptions): void;
|
|
1862
|
-
/**
|
|
1863
|
-
* Stop embedded worker.
|
|
1864
|
-
*/
|
|
1865
|
-
stopWorker(): Promise<void>;
|
|
1866
|
-
/**
|
|
1867
|
-
* Get QueueManager instance.
|
|
1868
|
-
*/
|
|
1869
|
-
getQueueManager(): QueueManager | undefined;
|
|
1870
|
-
}
|
|
1871
|
-
declare module '@gravito/core' {
|
|
1872
|
-
interface GravitoVariables {
|
|
1873
|
-
/** Queue manager for job processing */
|
|
1874
|
-
queue?: QueueManager;
|
|
1875
|
-
/** Database service (from orbit-db) */
|
|
1876
|
-
db?: unknown;
|
|
1877
|
-
}
|
|
1878
|
-
}
|
|
1879
|
-
|
|
1880
|
-
/**
|
|
1881
|
-
* Buffered Persistence Wrapper.
|
|
1882
|
-
* Wraps any PersistenceAdapter to add buffering and batch writing capabilities.
|
|
1883
|
-
*/
|
|
1884
|
-
declare class BufferedPersistence implements PersistenceAdapter {
|
|
1885
|
-
private adapter;
|
|
1886
|
-
private jobBuffer;
|
|
1887
|
-
private logBuffer;
|
|
1888
|
-
private flushTimer;
|
|
1889
|
-
private maxBufferSize;
|
|
1890
|
-
private flushInterval;
|
|
1891
|
-
constructor(adapter: PersistenceAdapter, options?: {
|
|
1892
|
-
maxBufferSize?: number;
|
|
1893
|
-
flushInterval?: number;
|
|
1894
|
-
});
|
|
1895
|
-
archive(queue: string, job: SerializedJob, status: 'completed' | 'failed' | 'waiting' | string): Promise<void>;
|
|
1896
|
-
find(queue: string, id: string): Promise<SerializedJob | null>;
|
|
1897
|
-
list(queue: string, options?: {
|
|
1898
|
-
limit?: number;
|
|
1899
|
-
offset?: number;
|
|
1900
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
1901
|
-
jobId?: string;
|
|
1902
|
-
startTime?: Date;
|
|
1903
|
-
endTime?: Date;
|
|
1904
|
-
}): Promise<SerializedJob[]>;
|
|
1905
|
-
archiveMany(jobs: Array<{
|
|
1906
|
-
queue: string;
|
|
1907
|
-
job: SerializedJob;
|
|
1908
|
-
status: 'completed' | 'failed' | 'waiting' | string;
|
|
1909
|
-
}>): Promise<void>;
|
|
1910
|
-
cleanup(days: number): Promise<number>;
|
|
1911
|
-
flush(): Promise<void>;
|
|
1912
|
-
count(queue: string, options?: {
|
|
1913
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
1914
|
-
jobId?: string;
|
|
1915
|
-
startTime?: Date;
|
|
1916
|
-
endTime?: Date;
|
|
1917
|
-
}): Promise<number>;
|
|
1918
|
-
archiveLog(log: {
|
|
1919
|
-
level: string;
|
|
1920
|
-
message: string;
|
|
1921
|
-
workerId: string;
|
|
1922
|
-
queue?: string;
|
|
1923
|
-
timestamp: Date;
|
|
1924
|
-
}): Promise<void>;
|
|
1925
|
-
archiveLogMany(logs: Array<{
|
|
1926
|
-
level: string;
|
|
1927
|
-
message: string;
|
|
1928
|
-
workerId: string;
|
|
1929
|
-
queue?: string;
|
|
1930
|
-
timestamp: Date;
|
|
1931
|
-
}>): Promise<void>;
|
|
1932
|
-
listLogs(options?: {
|
|
1933
|
-
limit?: number;
|
|
1934
|
-
offset?: number;
|
|
1935
|
-
level?: string;
|
|
1936
|
-
workerId?: string;
|
|
1937
|
-
queue?: string;
|
|
1938
|
-
search?: string;
|
|
1939
|
-
startTime?: Date;
|
|
1940
|
-
endTime?: Date;
|
|
1941
|
-
}): Promise<any[]>;
|
|
1942
|
-
countLogs(options?: {
|
|
1943
|
-
level?: string;
|
|
1944
|
-
workerId?: string;
|
|
1945
|
-
queue?: string;
|
|
1946
|
-
search?: string;
|
|
1947
|
-
startTime?: Date;
|
|
1948
|
-
endTime?: Date;
|
|
1949
|
-
}): Promise<number>;
|
|
1950
|
-
private ensureFlushTimer;
|
|
1951
|
-
}
|
|
1952
|
-
|
|
1953
|
-
/**
|
|
1954
|
-
* MySQL Persistence Adapter.
|
|
1955
|
-
* Archives jobs into a MySQL table for long-term auditing.
|
|
1956
|
-
*/
|
|
1957
|
-
declare class MySQLPersistence implements PersistenceAdapter {
|
|
1958
|
-
private db;
|
|
1959
|
-
private table;
|
|
1960
|
-
private logsTable;
|
|
1961
|
-
/**
|
|
1962
|
-
* @param db - An Atlas DB instance or compatible QueryBuilder.
|
|
1963
|
-
* @param table - The name of the table to store archived jobs.
|
|
1964
|
-
* @param logsTable - The name of the table to store system logs.
|
|
1965
|
-
* @param options - Buffering options (Deprecated: Use BufferedPersistence wrapper instead).
|
|
1966
|
-
*/
|
|
1967
|
-
constructor(db: ConnectionContract, table?: string, logsTable?: string, _options?: {
|
|
1968
|
-
maxBufferSize?: number;
|
|
1969
|
-
flushInterval?: number;
|
|
1970
|
-
});
|
|
1971
|
-
archive(queue: string, job: SerializedJob, status: 'completed' | 'failed' | 'waiting' | string): Promise<void>;
|
|
1972
|
-
archiveMany(jobs: Array<{
|
|
1973
|
-
queue: string;
|
|
1974
|
-
job: SerializedJob;
|
|
1975
|
-
status: 'completed' | 'failed' | 'waiting' | string;
|
|
1976
|
-
}>): Promise<void>;
|
|
1977
|
-
flush(): Promise<void>;
|
|
1978
|
-
find(queue: string, id: string): Promise<SerializedJob | null>;
|
|
1979
|
-
/**
|
|
1980
|
-
* List jobs from the archive.
|
|
1981
|
-
*/
|
|
1982
|
-
list(queue: string, options?: {
|
|
1983
|
-
limit?: number;
|
|
1984
|
-
offset?: number;
|
|
1985
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
1986
|
-
jobId?: string;
|
|
1987
|
-
startTime?: Date;
|
|
1988
|
-
endTime?: Date;
|
|
1989
|
-
}): Promise<SerializedJob[]>;
|
|
1990
|
-
/**
|
|
1991
|
-
* Search jobs from the archive.
|
|
1992
|
-
*/
|
|
1993
|
-
search(query: string, options?: {
|
|
1994
|
-
limit?: number;
|
|
1995
|
-
offset?: number;
|
|
1996
|
-
queue?: string;
|
|
1997
|
-
}): Promise<SerializedJob[]>;
|
|
1998
|
-
/**
|
|
1999
|
-
* Archive a system log message (buffered).
|
|
2000
|
-
*/
|
|
2001
|
-
archiveLog(log: {
|
|
2002
|
-
level: string;
|
|
2003
|
-
message: string;
|
|
2004
|
-
workerId: string;
|
|
2005
|
-
queue?: string;
|
|
2006
|
-
timestamp: Date;
|
|
2007
|
-
}): Promise<void>;
|
|
2008
|
-
/**
|
|
2009
|
-
* Archive multiple log messages (direct batch write).
|
|
2010
|
-
*/
|
|
2011
|
-
archiveLogMany(logs: Array<{
|
|
2012
|
-
level: string;
|
|
2013
|
-
message: string;
|
|
2014
|
-
workerId: string;
|
|
2015
|
-
queue?: string;
|
|
2016
|
-
timestamp: Date;
|
|
2017
|
-
}>): Promise<void>;
|
|
2018
|
-
/**
|
|
2019
|
-
* List system logs from the archive.
|
|
2020
|
-
*/
|
|
2021
|
-
listLogs(options?: {
|
|
2022
|
-
limit?: number;
|
|
2023
|
-
offset?: number;
|
|
2024
|
-
level?: string;
|
|
2025
|
-
workerId?: string;
|
|
2026
|
-
queue?: string;
|
|
2027
|
-
search?: string;
|
|
2028
|
-
startTime?: Date;
|
|
2029
|
-
endTime?: Date;
|
|
2030
|
-
}): Promise<any[]>;
|
|
2031
|
-
/**
|
|
2032
|
-
* Count system logs in the archive.
|
|
2033
|
-
*/
|
|
2034
|
-
countLogs(options?: {
|
|
2035
|
-
level?: string;
|
|
2036
|
-
workerId?: string;
|
|
2037
|
-
queue?: string;
|
|
2038
|
-
search?: string;
|
|
2039
|
-
startTime?: Date;
|
|
2040
|
-
endTime?: Date;
|
|
2041
|
-
}): Promise<number>;
|
|
2042
|
-
/**
|
|
2043
|
-
* Remove old records from the archive.
|
|
2044
|
-
*/
|
|
2045
|
-
cleanup(days: number): Promise<number>;
|
|
2046
|
-
/**
|
|
2047
|
-
* Count jobs in the archive.
|
|
2048
|
-
*/
|
|
2049
|
-
count(queue: string, options?: {
|
|
2050
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
2051
|
-
jobId?: string;
|
|
2052
|
-
startTime?: Date;
|
|
2053
|
-
endTime?: Date;
|
|
2054
|
-
}): Promise<number>;
|
|
2055
|
-
/**
|
|
2056
|
-
* Help script to create the necessary table.
|
|
2057
|
-
*/
|
|
2058
|
-
setupTable(): Promise<void>;
|
|
2059
|
-
private setupJobsTable;
|
|
2060
|
-
private setupLogsTable;
|
|
2061
|
-
}
|
|
2062
|
-
|
|
2063
|
-
/**
|
|
2064
|
-
* SQLite Persistence Adapter.
|
|
2065
|
-
* Archives jobs into a local SQLite database for zero-config persistence.
|
|
2066
|
-
*/
|
|
2067
|
-
declare class SQLitePersistence implements PersistenceAdapter {
|
|
2068
|
-
private db;
|
|
2069
|
-
private table;
|
|
2070
|
-
private logsTable;
|
|
2071
|
-
/**
|
|
2072
|
-
* @param db - An Atlas DB instance (SQLite driver).
|
|
2073
|
-
* @param table - The name of the table to store archived jobs.
|
|
2074
|
-
* @param logsTable - The name of the table to store system logs.
|
|
2075
|
-
* @param options - Buffering options (Deprecated: Use BufferedPersistence wrapper instead).
|
|
2076
|
-
*/
|
|
2077
|
-
constructor(db: ConnectionContract, table?: string, logsTable?: string, _options?: {
|
|
2078
|
-
maxBufferSize?: number;
|
|
2079
|
-
flushInterval?: number;
|
|
2080
|
-
});
|
|
2081
|
-
archive(queue: string, job: SerializedJob, status: 'completed' | 'failed' | 'waiting' | string): Promise<void>;
|
|
2082
|
-
archiveMany(jobs: Array<{
|
|
2083
|
-
queue: string;
|
|
2084
|
-
job: SerializedJob;
|
|
2085
|
-
status: 'completed' | 'failed' | 'waiting' | string;
|
|
2086
|
-
}>): Promise<void>;
|
|
2087
|
-
flush(): Promise<void>;
|
|
2088
|
-
find(queue: string, id: string): Promise<SerializedJob | null>;
|
|
2089
|
-
/**
|
|
2090
|
-
* List jobs from the archive.
|
|
2091
|
-
*/
|
|
2092
|
-
list(queue: string, options?: {
|
|
2093
|
-
limit?: number;
|
|
2094
|
-
offset?: number;
|
|
2095
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
2096
|
-
jobId?: string;
|
|
2097
|
-
startTime?: Date;
|
|
2098
|
-
endTime?: Date;
|
|
2099
|
-
}): Promise<SerializedJob[]>;
|
|
2100
|
-
/**
|
|
2101
|
-
* Search jobs from the archive.
|
|
2102
|
-
*/
|
|
2103
|
-
search(query: string, options?: {
|
|
2104
|
-
limit?: number;
|
|
2105
|
-
offset?: number;
|
|
2106
|
-
queue?: string;
|
|
2107
|
-
}): Promise<SerializedJob[]>;
|
|
2108
|
-
/**
|
|
2109
|
-
* Archive a system log message (buffered).
|
|
2110
|
-
*/
|
|
2111
|
-
archiveLog(log: {
|
|
2112
|
-
level: string;
|
|
2113
|
-
message: string;
|
|
2114
|
-
workerId: string;
|
|
2115
|
-
queue?: string;
|
|
2116
|
-
timestamp: Date;
|
|
2117
|
-
}): Promise<void>;
|
|
2118
|
-
/**
|
|
2119
|
-
* Archive multiple log messages (direct batch write).
|
|
2120
|
-
*/
|
|
2121
|
-
archiveLogMany(logs: Array<{
|
|
2122
|
-
level: string;
|
|
2123
|
-
message: string;
|
|
2124
|
-
workerId: string;
|
|
2125
|
-
queue?: string;
|
|
2126
|
-
timestamp: Date;
|
|
2127
|
-
}>): Promise<void>;
|
|
2128
|
-
/**
|
|
2129
|
-
* List system logs from the archive.
|
|
2130
|
-
*/
|
|
2131
|
-
listLogs(options?: {
|
|
2132
|
-
limit?: number;
|
|
2133
|
-
offset?: number;
|
|
2134
|
-
level?: string;
|
|
2135
|
-
workerId?: string;
|
|
2136
|
-
queue?: string;
|
|
2137
|
-
search?: string;
|
|
2138
|
-
startTime?: Date;
|
|
2139
|
-
endTime?: Date;
|
|
2140
|
-
}): Promise<any[]>;
|
|
2141
|
-
/**
|
|
2142
|
-
* Count system logs in the archive.
|
|
2143
|
-
*/
|
|
2144
|
-
countLogs(options?: {
|
|
2145
|
-
level?: string;
|
|
2146
|
-
workerId?: string;
|
|
2147
|
-
queue?: string;
|
|
2148
|
-
search?: string;
|
|
2149
|
-
startTime?: Date;
|
|
2150
|
-
endTime?: Date;
|
|
2151
|
-
}): Promise<number>;
|
|
2152
|
-
/**
|
|
2153
|
-
* Remove old records from the archive.
|
|
2154
|
-
*/
|
|
2155
|
-
cleanup(days: number): Promise<number>;
|
|
2156
|
-
/**
|
|
2157
|
-
* Count jobs in the archive.
|
|
2158
|
-
*/
|
|
2159
|
-
count(queue: string, options?: {
|
|
2160
|
-
status?: 'completed' | 'failed' | 'waiting' | string;
|
|
2161
|
-
jobId?: string;
|
|
2162
|
-
startTime?: Date;
|
|
2163
|
-
endTime?: Date;
|
|
2164
|
-
}): Promise<number>;
|
|
2165
|
-
/**
|
|
2166
|
-
* Setup table for SQLite.
|
|
2167
|
-
*/
|
|
2168
|
-
setupTable(): Promise<void>;
|
|
2169
|
-
private setupJobsTable;
|
|
2170
|
-
private setupLogsTable;
|
|
2171
|
-
}
|
|
2172
|
-
|
|
2173
|
-
/**
|
|
2174
|
-
* Class name serializer (Laravel-style).
|
|
2175
|
-
*
|
|
2176
|
-
* Stores the class name and properties, then recreates an instance at runtime.
|
|
2177
|
-
* This is the recommended serializer because it can restore class instances correctly.
|
|
2178
|
-
*
|
|
2179
|
-
* Requirement: Job classes must be dynamically loadable (by class name).
|
|
2180
|
-
*
|
|
2181
|
-
* @example
|
|
2182
|
-
* ```typescript
|
|
2183
|
-
* const serializer = new ClassNameSerializer()
|
|
2184
|
-
* const serialized = serializer.serialize(new SendEmail('user@example.com'))
|
|
2185
|
-
* // serialized.data contains class name and properties
|
|
2186
|
-
*
|
|
2187
|
-
* const job = serializer.deserialize(serialized)
|
|
2188
|
-
* // job is an instance of SendEmail
|
|
2189
|
-
* ```
|
|
2190
|
-
*/
|
|
2191
|
-
declare class ClassNameSerializer implements JobSerializer {
|
|
2192
|
-
/**
|
|
2193
|
-
* Job class registry (for resolving classes by name).
|
|
2194
|
-
*/
|
|
2195
|
-
private jobClasses;
|
|
2196
|
-
/**
|
|
2197
|
-
* Register a Job class.
|
|
2198
|
-
* @param jobClass - Job class
|
|
2199
|
-
*/
|
|
2200
|
-
register(jobClass: new (...args: unknown[]) => Job): void;
|
|
2201
|
-
/**
|
|
2202
|
-
* Register multiple Job classes.
|
|
2203
|
-
* @param jobClasses - Job class array
|
|
2204
|
-
*/
|
|
2205
|
-
registerMany(jobClasses: Array<new (...args: unknown[]) => Job>): void;
|
|
2206
|
-
/**
|
|
2207
|
-
* Serialize a Job.
|
|
2208
|
-
*/
|
|
2209
|
-
serialize(job: Job): SerializedJob;
|
|
2210
|
-
/**
|
|
2211
|
-
* Deserialize a Job.
|
|
2212
|
-
*/
|
|
2213
|
-
deserialize(serialized: SerializedJob): Job;
|
|
2214
|
-
}
|
|
2215
|
-
|
|
2216
|
-
/**
|
|
2217
|
-
* JSON Serializer
|
|
2218
|
-
*
|
|
2219
|
-
* Serializes jobs using JSON.
|
|
2220
|
-
* Suitable for simple scenarios where you only need to persist plain properties.
|
|
2221
|
-
*
|
|
2222
|
-
* Limitation: cannot restore class instances, functions, or complex objects.
|
|
2223
|
-
*
|
|
2224
|
-
* @example
|
|
2225
|
-
* ```typescript
|
|
2226
|
-
* const serializer = new JsonSerializer()
|
|
2227
|
-
* const serialized = serializer.serialize(job)
|
|
2228
|
-
* const job = serializer.deserialize(serialized)
|
|
2229
|
-
* ```
|
|
2230
|
-
*/
|
|
2231
|
-
declare class JsonSerializer implements JobSerializer {
|
|
2232
|
-
/**
|
|
2233
|
-
* Serialize a job.
|
|
2234
|
-
*/
|
|
2235
|
-
serialize(job: Job): SerializedJob;
|
|
2236
|
-
/**
|
|
2237
|
-
* Deserialize a job.
|
|
2238
|
-
*/
|
|
2239
|
-
deserialize(serialized: SerializedJob): Job;
|
|
2240
|
-
}
|
|
2241
|
-
|
|
2242
|
-
export { BufferedPersistence, ClassNameSerializer, Consumer, type ConsumerOptions, DatabaseDriver, type DatabaseDriverConfig, Job, type JobSerializer, JsonSerializer, KafkaDriver, type KafkaDriverConfig, MemoryDriver, MySQLPersistence, OrbitStream, type OrbitStreamOptions, type PersistenceAdapter, type QueueConfig, type QueueConnectionConfig, type QueueDriver, QueueManager, type Queueable, RabbitMQDriver, type RabbitMQDriverConfig, RedisDriver, type RedisDriverConfig, SQLitePersistence, SQSDriver, type SQSDriverConfig, Scheduler, type SerializedJob, type TopicOptions, Worker, type WorkerOptions };
|
|
18
|
+
* // Push a Job
|
|
19
|
+
* await queue.push(new SendEmail())
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export { BatchConsumer, type BatchConsumerOptions } from './BatchConsumer';
|
|
23
|
+
export type { ConsumerOptions } from './Consumer';
|
|
24
|
+
export { Consumer } from './Consumer';
|
|
25
|
+
export type { ConsumerStrategy } from './consumer/ConsumerStrategy';
|
|
26
|
+
export { PollingStrategy } from './consumer/PollingStrategy';
|
|
27
|
+
export { ReactiveStrategy } from './consumer/ReactiveStrategy';
|
|
28
|
+
export { decodeBinaryJobFrame, encodeBinaryJobFrame, Flags as BinaryJobFrameFlags, isGravitoJobFrame, MAGIC as BINARY_JOB_FRAME_MAGIC, VERSION as BINARY_JOB_FRAME_VERSION, } from './drivers/BinaryJobFrame';
|
|
29
|
+
export type { BullMQDriverConfig } from './drivers/BullMQDriver';
|
|
30
|
+
export { BullMQDriver } from './drivers/BullMQDriver';
|
|
31
|
+
export type { DatabaseDriverConfig } from './drivers/DatabaseDriver';
|
|
32
|
+
export { DatabaseDriver } from './drivers/DatabaseDriver';
|
|
33
|
+
export type { GrpcDriverConfig } from './drivers/GrpcDriver';
|
|
34
|
+
export { GrpcDriver } from './drivers/GrpcDriver';
|
|
35
|
+
export type { KafkaDriverConfig } from './drivers/KafkaDriver';
|
|
36
|
+
export { KafkaDriver } from './drivers/KafkaDriver';
|
|
37
|
+
export { MemoryDriver } from './drivers/MemoryDriver';
|
|
38
|
+
export type { QueueDriver } from './drivers/QueueDriver';
|
|
39
|
+
export type { RabbitMQDriverConfig } from './drivers/RabbitMQDriver';
|
|
40
|
+
export { RabbitMQDriver } from './drivers/RabbitMQDriver';
|
|
41
|
+
export type { RedisClient, RedisDriverConfig } from './drivers/RedisDriver';
|
|
42
|
+
export { RedisDriver } from './drivers/RedisDriver';
|
|
43
|
+
export type { SQSDriverConfig } from './drivers/SQSDriver';
|
|
44
|
+
export { SQSDriver } from './drivers/SQSDriver';
|
|
45
|
+
export { Job } from './Job';
|
|
46
|
+
export type { LockOptions } from './locks/DistributedLock';
|
|
47
|
+
export { DistributedLock } from './locks/DistributedLock';
|
|
48
|
+
export type { OrbitStreamOptions } from './OrbitStream';
|
|
49
|
+
export { OrbitStream } from './OrbitStream';
|
|
50
|
+
export { BufferedPersistence } from './persistence/BufferedPersistence';
|
|
51
|
+
export { BunBufferedPersistence, type BunBufferedPersistenceOptions, createBufferedPersistence, } from './persistence/BunBufferedPersistence';
|
|
52
|
+
export { MySQLPersistence } from './persistence/MySQLPersistence';
|
|
53
|
+
export { SQLitePersistence } from './persistence/SQLitePersistence';
|
|
54
|
+
export type { Queueable } from './Queueable';
|
|
55
|
+
export { QueueManager } from './QueueManager';
|
|
56
|
+
export type { ScheduledJobConfig, SchedulerOptions } from './Scheduler';
|
|
57
|
+
export { Scheduler } from './Scheduler';
|
|
58
|
+
export { type RetryStrategy, StreamEventBackend, type StreamEventBackendConfig, } from './StreamEventBackend';
|
|
59
|
+
export { SystemEventJob } from './SystemEventJob';
|
|
60
|
+
export { BinarySerializer } from './serializers/BinarySerializer';
|
|
61
|
+
export { CborNativeSerializer } from './serializers/CborNativeSerializer';
|
|
62
|
+
export { ClassNameSerializer } from './serializers/ClassNameSerializer';
|
|
63
|
+
export type { JobSerializer } from './serializers/JobSerializer';
|
|
64
|
+
export { JsonlSerializer } from './serializers/JsonlSerializer';
|
|
65
|
+
export { JsonSerializer } from './serializers/JsonSerializer';
|
|
66
|
+
export type { PersistenceAdapter, QueueConfig, QueueConnectionConfig, SerializedJob, TopicOptions, } from './types';
|
|
67
|
+
export type { WorkerOptions } from './Worker';
|
|
68
|
+
export { Worker } from './Worker';
|
|
69
|
+
export { SandboxedWorker, type SandboxedWorkerConfig, WorkerPool, type WorkerPoolConfig, type WorkerPoolStats, } from './workers';
|