@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
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import type { QueueDriver } from './drivers/QueueDriver';
|
|
2
|
+
import type { Job } from './Job';
|
|
3
|
+
import type { Queueable } from './Queueable';
|
|
4
|
+
import type { Scheduler } from './Scheduler';
|
|
5
|
+
import type { JobSerializer } from './serializers/JobSerializer';
|
|
6
|
+
import type { JobPushOptions, PersistenceAdapter, QueueConfig, QueueConnectionConfig, QueueStats, SerializedJob } from './types';
|
|
7
|
+
/**
|
|
8
|
+
* The central manager for queue operations.
|
|
9
|
+
*
|
|
10
|
+
* This class manages multiple queue connections and drivers, exposing a unified API for pushing,
|
|
11
|
+
* popping, and managing jobs. It handles connection pooling, serialization, persistence,
|
|
12
|
+
* and driver lazy-loading.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const manager = new QueueManager({
|
|
18
|
+
* default: 'redis',
|
|
19
|
+
* connections: {
|
|
20
|
+
* redis: { driver: 'redis', client: redisClient }
|
|
21
|
+
* }
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* await manager.push(new SendEmailJob('hello@example.com'));
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare class QueueManager {
|
|
28
|
+
private drivers;
|
|
29
|
+
private serializers;
|
|
30
|
+
private defaultConnection;
|
|
31
|
+
private defaultSerializer;
|
|
32
|
+
private persistence?;
|
|
33
|
+
private scheduler?;
|
|
34
|
+
private debug;
|
|
35
|
+
constructor(config?: QueueConfig);
|
|
36
|
+
/**
|
|
37
|
+
* Log debug message.
|
|
38
|
+
*/
|
|
39
|
+
private log;
|
|
40
|
+
/**
|
|
41
|
+
* Registers a new queue connection with the manager.
|
|
42
|
+
*
|
|
43
|
+
* Dynamically loads the required driver implementation based on the configuration.
|
|
44
|
+
*
|
|
45
|
+
* @param name - The name of the connection (e.g., 'primary').
|
|
46
|
+
* @param config - The configuration object for the driver.
|
|
47
|
+
* @throws {Error} If the driver type is missing required dependencies or unsupported.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* manager.registerConnection('analytics', { driver: 'sqs', client: sqs });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
registerConnection(name: string, config: QueueConnectionConfig): void;
|
|
55
|
+
/**
|
|
56
|
+
* Retrieves the driver instance for a specific connection.
|
|
57
|
+
*
|
|
58
|
+
* @param connection - The name of the connection.
|
|
59
|
+
* @returns The configured QueueDriver instance.
|
|
60
|
+
* @throws {Error} If the connection has not been registered.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const driver = manager.getDriver('redis');
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
getDriver(connection: string): QueueDriver;
|
|
68
|
+
/**
|
|
69
|
+
* Gets the name of the default connection.
|
|
70
|
+
*
|
|
71
|
+
* @returns The default connection name.
|
|
72
|
+
*/
|
|
73
|
+
getDefaultConnection(): string;
|
|
74
|
+
/**
|
|
75
|
+
* Retrieves a serializer instance by type.
|
|
76
|
+
*
|
|
77
|
+
* @param type - The serializer type (e.g., 'json', 'class'). If omitted, returns the default serializer.
|
|
78
|
+
* @returns The JobSerializer instance.
|
|
79
|
+
* @throws {Error} If the requested serializer type is not found.
|
|
80
|
+
*/
|
|
81
|
+
getSerializer(type?: string): JobSerializer;
|
|
82
|
+
/**
|
|
83
|
+
* Registers Job classes for the `ClassNameSerializer`.
|
|
84
|
+
*
|
|
85
|
+
* This is required when using 'class' serialization to allow proper hydration of job instances
|
|
86
|
+
* upon deserialization.
|
|
87
|
+
*
|
|
88
|
+
* @param jobClasses - An array of Job class constructors.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* manager.registerJobClasses([SendEmailJob, ProcessOrderJob]);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
registerJobClasses(jobClasses: Array<new (...args: unknown[]) => Job>): void;
|
|
96
|
+
/**
|
|
97
|
+
* Pushes a single job to the queue.
|
|
98
|
+
*
|
|
99
|
+
* Serializes the job, selects the appropriate driver based on job configuration,
|
|
100
|
+
* and dispatches it. Also handles audit logging if persistence is enabled.
|
|
101
|
+
*
|
|
102
|
+
* @template T - The type of the job (extends Job).
|
|
103
|
+
* @param job - The job instance to enqueue.
|
|
104
|
+
* @param options - Optional overrides for push behavior (priority, delay, etc.).
|
|
105
|
+
* @returns The same job instance (for chaining).
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* await manager.push(new SendEmailJob('user@example.com'));
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
push<T extends Job & Queueable>(job: T, options?: JobPushOptions): Promise<T>;
|
|
113
|
+
/**
|
|
114
|
+
* Pushes multiple jobs to the queue in a batch.
|
|
115
|
+
*
|
|
116
|
+
* Optimizes network requests by batching jobs where possible. Groups jobs by connection
|
|
117
|
+
* and queue to maximize throughput.
|
|
118
|
+
*
|
|
119
|
+
* @template T - The type of the jobs.
|
|
120
|
+
* @param jobs - An array of job instances to enqueue.
|
|
121
|
+
* @param options - Configuration for batch size and concurrency.
|
|
122
|
+
* @returns A promise that resolves when all jobs have been pushed.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* await manager.pushMany(jobs, { batchSize: 500, concurrency: 5 });
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
pushMany<T extends Job & Queueable>(jobs: T[], options?: {
|
|
130
|
+
batchSize?: number;
|
|
131
|
+
concurrency?: number;
|
|
132
|
+
}): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Pops a single job from the queue.
|
|
135
|
+
*
|
|
136
|
+
* Retrieves the next available job from the specified queue.
|
|
137
|
+
*
|
|
138
|
+
* @param queue - The queue name (default: 'default').
|
|
139
|
+
* @param connection - The connection name (defaults to default connection).
|
|
140
|
+
* @returns A Job instance if found, or `null` if the queue is empty.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const job = await manager.pop('priority-queue');
|
|
145
|
+
* if (job) await job.handle();
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
pop(queue?: string, connection?: string): Promise<Job | null>;
|
|
149
|
+
/**
|
|
150
|
+
* Pops multiple jobs from the queue efficiently.
|
|
151
|
+
*
|
|
152
|
+
* Attempts to retrieve a batch of jobs from the driver. If the driver does not support
|
|
153
|
+
* batching, it falls back to sequential popping.
|
|
154
|
+
*
|
|
155
|
+
* @param queue - The queue name (default: 'default').
|
|
156
|
+
* @param count - The maximum number of jobs to retrieve (default: 10).
|
|
157
|
+
* @param connection - The connection name.
|
|
158
|
+
* @returns An array of Job instances.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const jobs = await manager.popMany('default', 50);
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
popMany(queue?: string, count?: number, connection?: string): Promise<Job[]>;
|
|
166
|
+
/**
|
|
167
|
+
* Retrieves the current size of a queue.
|
|
168
|
+
*
|
|
169
|
+
* @param queue - The queue name (default: 'default').
|
|
170
|
+
* @param connection - The connection name.
|
|
171
|
+
* @returns The number of waiting jobs.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const count = await manager.size('emails');
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
size(queue?: string, connection?: string): Promise<number>;
|
|
179
|
+
/**
|
|
180
|
+
* Pops a job from the queue with blocking (wait) behavior.
|
|
181
|
+
*
|
|
182
|
+
* Waits for a job to become available for the specified timeout duration.
|
|
183
|
+
* Useful for reducing polling loop frequency.
|
|
184
|
+
*
|
|
185
|
+
* @param queues - A queue name or array of queue names to listen to.
|
|
186
|
+
* @param timeout - Timeout in seconds (0 = block indefinitely).
|
|
187
|
+
* @param connection - The connection name.
|
|
188
|
+
* @returns A Job instance if found, or `null` if timed out.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* // Wait up to 30 seconds for a job
|
|
193
|
+
* const job = await manager.popBlocking('default', 30);
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
popBlocking(queues?: string | string[], timeout?: number, connection?: string): Promise<Job | null>;
|
|
197
|
+
/**
|
|
198
|
+
* Removes all jobs from a specific queue.
|
|
199
|
+
*
|
|
200
|
+
* @param queue - The queue name to purge.
|
|
201
|
+
* @param connection - The connection name.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* await manager.clear('test-queue');
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
clear(queue?: string, connection?: string): Promise<void>;
|
|
209
|
+
/**
|
|
210
|
+
* Retrieves comprehensive statistics for a queue.
|
|
211
|
+
*
|
|
212
|
+
* Includes counts for pending, processing, delayed, and failed jobs.
|
|
213
|
+
*
|
|
214
|
+
* @param queue - The queue name.
|
|
215
|
+
* @param connection - The connection name.
|
|
216
|
+
* @returns A QueueStats object.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* const stats = await manager.stats('default');
|
|
221
|
+
* console.log(stats.size, stats.failed);
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
stats(queue?: string, connection?: string): Promise<QueueStats>;
|
|
225
|
+
/**
|
|
226
|
+
* Marks a job as successfully completed.
|
|
227
|
+
*
|
|
228
|
+
* Removes the job from the processing state and optionally archives it.
|
|
229
|
+
*
|
|
230
|
+
* @param job - The job instance that finished.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* await manager.complete(job);
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
complete<T extends Job & Queueable>(job: T): Promise<void>;
|
|
238
|
+
/**
|
|
239
|
+
* Marks a job as failed.
|
|
240
|
+
*
|
|
241
|
+
* Moves the job to the failed state (Dead Letter Queue) and optionally archives it.
|
|
242
|
+
* This is typically called after max retry attempts are exhausted.
|
|
243
|
+
*
|
|
244
|
+
* @param job - The job instance that failed.
|
|
245
|
+
* @param error - The error that caused the failure.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* await manager.fail(job, new Error('Something went wrong'));
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
fail<T extends Job & Queueable>(job: T, error: Error): Promise<void>;
|
|
253
|
+
/**
|
|
254
|
+
* Retrieves the configured persistence adapter.
|
|
255
|
+
*
|
|
256
|
+
* @returns The PersistenceAdapter instance, or undefined if not configured.
|
|
257
|
+
*/
|
|
258
|
+
getPersistence(): PersistenceAdapter | undefined;
|
|
259
|
+
/**
|
|
260
|
+
* Gets the Scheduler instance associated with this manager.
|
|
261
|
+
*
|
|
262
|
+
* The Scheduler handles delayed jobs and periodic tasks.
|
|
263
|
+
*
|
|
264
|
+
* @returns The Scheduler instance.
|
|
265
|
+
*/
|
|
266
|
+
getScheduler(): Scheduler;
|
|
267
|
+
/**
|
|
268
|
+
* Retrieves failed jobs from the Dead Letter Queue.
|
|
269
|
+
*
|
|
270
|
+
* @param queue - The queue name.
|
|
271
|
+
* @param start - The starting index (pagination).
|
|
272
|
+
* @param end - The ending index (pagination).
|
|
273
|
+
* @param connection - The connection name.
|
|
274
|
+
* @returns An array of serialized jobs.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```typescript
|
|
278
|
+
* const failedJobs = await manager.getFailed('default', 0, 10);
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
getFailed(queue: string, start?: number, end?: number, connection?: string): Promise<SerializedJob[]>;
|
|
282
|
+
/**
|
|
283
|
+
* Retries failed jobs from the Dead Letter Queue.
|
|
284
|
+
*
|
|
285
|
+
* Moves jobs from the failed state back to the active queue for re-processing.
|
|
286
|
+
*
|
|
287
|
+
* @param queue - The queue name.
|
|
288
|
+
* @param count - The number of jobs to retry.
|
|
289
|
+
* @param connection - The connection name.
|
|
290
|
+
* @returns The number of jobs successfully retried.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```typescript
|
|
294
|
+
* await manager.retryFailed('default', 5);
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
retryFailed(queue: string, count?: number, connection?: string): Promise<number>;
|
|
298
|
+
/**
|
|
299
|
+
* Clears all failed jobs from the Dead Letter Queue.
|
|
300
|
+
*
|
|
301
|
+
* @param queue - The queue name.
|
|
302
|
+
* @param connection - The connection name.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* await manager.clearFailed('default');
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
clearFailed(queue: string, connection?: string): Promise<void>;
|
|
310
|
+
/**
|
|
311
|
+
* Retrieves high-level statistics across all registered connections and queues.
|
|
312
|
+
*
|
|
313
|
+
* Iterates through all drivers and collects metadata to provide a comprehensive
|
|
314
|
+
* snapshot of the entire queue system's health.
|
|
315
|
+
*
|
|
316
|
+
* @returns A promise resolving to a GlobalStats object.
|
|
317
|
+
*/
|
|
318
|
+
getGlobalStats(): Promise<import('./types').GlobalStats>;
|
|
319
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the contract for queueable items (jobs) with fluent configuration.
|
|
3
|
+
*
|
|
4
|
+
* This interface enables method chaining for configuring job properties such as
|
|
5
|
+
* target queue, connection, delay, and priority before dispatching. It ensures
|
|
6
|
+
* consistent API surface across different job implementations.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* class MyJob implements Queueable {
|
|
12
|
+
* queueName?: string
|
|
13
|
+
* // ... implementation
|
|
14
|
+
* onQueue(queue: string): this {
|
|
15
|
+
* this.queueName = queue
|
|
16
|
+
* return this
|
|
17
|
+
* }
|
|
18
|
+
* // ...
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export interface Queueable {
|
|
23
|
+
/**
|
|
24
|
+
* The specific queue name where the job should be processed.
|
|
25
|
+
*
|
|
26
|
+
* If not set, the default queue for the connection will be used.
|
|
27
|
+
*/
|
|
28
|
+
queueName?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The connection name (e.g., 'redis', 'sqs') to use for this job.
|
|
31
|
+
*
|
|
32
|
+
* If not set, the default connection configured in QueueManager will be used.
|
|
33
|
+
*/
|
|
34
|
+
connectionName?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The number of seconds to delay the job execution.
|
|
37
|
+
*/
|
|
38
|
+
delaySeconds?: number;
|
|
39
|
+
/**
|
|
40
|
+
* The priority level of the job.
|
|
41
|
+
*/
|
|
42
|
+
priority?: string | number;
|
|
43
|
+
/**
|
|
44
|
+
* Sets the target queue for the job.
|
|
45
|
+
*
|
|
46
|
+
* @param queue - The name of the queue to push the job to.
|
|
47
|
+
* @returns The instance for method chaining.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* job.onQueue('notifications');
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
onQueue(queue: string): this;
|
|
55
|
+
/**
|
|
56
|
+
* Sets the target connection for the job.
|
|
57
|
+
*
|
|
58
|
+
* @param connection - The name of the connection to use.
|
|
59
|
+
* @returns The instance for method chaining.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* job.onConnection('sqs');
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
onConnection(connection: string): this;
|
|
67
|
+
/**
|
|
68
|
+
* Sets the priority of the job.
|
|
69
|
+
*
|
|
70
|
+
* @param priority - The priority level (e.g., 'high', 'low', 10).
|
|
71
|
+
* @returns The instance for method chaining.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* job.withPriority('critical');
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
withPriority(priority: string | number): this;
|
|
79
|
+
/**
|
|
80
|
+
* Sets a delay before the job is available for processing.
|
|
81
|
+
*
|
|
82
|
+
* @param delay - The delay in seconds.
|
|
83
|
+
* @returns The instance for method chaining.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* job.delay(300); // 5 minutes
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
delay(delay: number): this;
|
|
91
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import type { QueueManager } from './QueueManager';
|
|
2
|
+
import type { SerializedJob } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for a recurring scheduled job.
|
|
5
|
+
*
|
|
6
|
+
* Defines the schedule (CRON), the job to execute, and metadata tracking execution times.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
* @since 3.0.0
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const config: ScheduledJobConfig = {
|
|
13
|
+
* id: 'daily-report',
|
|
14
|
+
* cron: '0 0 * * *',
|
|
15
|
+
* queue: 'reports',
|
|
16
|
+
* job: serializedJob,
|
|
17
|
+
* enabled: true
|
|
18
|
+
* };
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export interface ScheduledJobConfig {
|
|
22
|
+
/** Unique identifier for the scheduled task. */
|
|
23
|
+
id: string;
|
|
24
|
+
/** Cron expression defining the schedule (e.g., '* * * * *'). */
|
|
25
|
+
cron: string;
|
|
26
|
+
/** The target queue name where the job should be pushed. */
|
|
27
|
+
queue: string;
|
|
28
|
+
/** The serialized job data. */
|
|
29
|
+
job: SerializedJob;
|
|
30
|
+
/** Timestamp of the last successful execution in milliseconds. */
|
|
31
|
+
lastRun?: number;
|
|
32
|
+
/** Timestamp of the next scheduled execution in milliseconds. */
|
|
33
|
+
nextRun?: number;
|
|
34
|
+
/** Whether the scheduled job is active. */
|
|
35
|
+
enabled: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Configuration options for the Scheduler.
|
|
39
|
+
*
|
|
40
|
+
* Defines behavior for scheduling tasks, including distributed lock settings.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
* @since 3.1.0
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const options: SchedulerOptions = {
|
|
47
|
+
* prefix: 'myapp:queue:',
|
|
48
|
+
* lockTtl: 60000, // Lock held for 60 seconds
|
|
49
|
+
* lockRefreshInterval: 20000 // Auto-renew every 20 seconds
|
|
50
|
+
* };
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export interface SchedulerOptions {
|
|
54
|
+
/**
|
|
55
|
+
* Prefix for Redis keys.
|
|
56
|
+
*
|
|
57
|
+
* @default 'queue:'
|
|
58
|
+
*/
|
|
59
|
+
prefix?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Time-to-live for the distributed lock in milliseconds.
|
|
62
|
+
*
|
|
63
|
+
* Setting a longer TTL ensures long-running tasks are not executed repeatedly
|
|
64
|
+
* due to lock expiration. Recommended to be 2-3 times the expected execution time.
|
|
65
|
+
*
|
|
66
|
+
* @default 60000 (60 seconds)
|
|
67
|
+
*/
|
|
68
|
+
lockTtl?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Interval for automatic lock renewal in milliseconds.
|
|
71
|
+
*
|
|
72
|
+
* If set, the lock will be automatically extended every `lockRefreshInterval`.
|
|
73
|
+
* Recommended to be 1/3 of `lockTtl`.
|
|
74
|
+
*
|
|
75
|
+
* @default 20000 (20 seconds)
|
|
76
|
+
*/
|
|
77
|
+
lockRefreshInterval?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Number of retries when acquiring a lock fails.
|
|
80
|
+
*
|
|
81
|
+
* @default 0
|
|
82
|
+
*/
|
|
83
|
+
lockRetryCount?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Delay between lock acquisition retries in milliseconds.
|
|
86
|
+
*
|
|
87
|
+
* @default 100
|
|
88
|
+
*/
|
|
89
|
+
lockRetryDelay?: number;
|
|
90
|
+
/**
|
|
91
|
+
* The interval in milliseconds at which the scheduler checks for due tasks.
|
|
92
|
+
*
|
|
93
|
+
* @default 60000 (1 minute)
|
|
94
|
+
*/
|
|
95
|
+
tickInterval?: number;
|
|
96
|
+
/**
|
|
97
|
+
* The time-to-live for the leader lock in milliseconds.
|
|
98
|
+
*
|
|
99
|
+
* Ensures that only one node acts as the scheduler leader.
|
|
100
|
+
* @default 30000 (30 seconds)
|
|
101
|
+
*/
|
|
102
|
+
leaderTtl?: number;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Manages recurring tasks and cron jobs.
|
|
106
|
+
*
|
|
107
|
+
* The Scheduler allows you to register jobs to run at specific intervals using CRON syntax.
|
|
108
|
+
* It uses Redis (or a compatible driver) to coordinate distributed execution, ensuring that
|
|
109
|
+
* a scheduled job runs only once per interval across multiple scheduler instances.
|
|
110
|
+
*
|
|
111
|
+
* @public
|
|
112
|
+
* @since 3.0.0
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const scheduler = manager.getScheduler();
|
|
116
|
+
* await scheduler.register({
|
|
117
|
+
* id: 'cleanup',
|
|
118
|
+
* cron: '0 * * * *', // Every hour
|
|
119
|
+
* job: new CleanupJob()
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* // Automatically start the scheduler loop
|
|
123
|
+
* await scheduler.start();
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare class Scheduler {
|
|
127
|
+
private manager;
|
|
128
|
+
private prefix;
|
|
129
|
+
private lockTtl;
|
|
130
|
+
private lockRefreshInterval?;
|
|
131
|
+
private lockRetryCount;
|
|
132
|
+
private lockRetryDelay;
|
|
133
|
+
private tickInterval;
|
|
134
|
+
private leaderTtl;
|
|
135
|
+
private distributedLock?;
|
|
136
|
+
private running;
|
|
137
|
+
private timer;
|
|
138
|
+
private isLeader;
|
|
139
|
+
constructor(manager: QueueManager, options?: SchedulerOptions);
|
|
140
|
+
private get client();
|
|
141
|
+
/**
|
|
142
|
+
* Gets or creates the distributed lock instance.
|
|
143
|
+
*
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
private getDistributedLock;
|
|
147
|
+
/**
|
|
148
|
+
* Registers a new scheduled job or updates an existing one.
|
|
149
|
+
*
|
|
150
|
+
* Calculates the next run time based on the CRON expression and stores the configuration in Redis.
|
|
151
|
+
*
|
|
152
|
+
* @param config - The job configuration (excluding nextRun and enabled status which are auto-set).
|
|
153
|
+
* @throws {Error} If Redis client does not support pipelining.
|
|
154
|
+
*/
|
|
155
|
+
register(config: Omit<ScheduledJobConfig, 'nextRun' | 'enabled'>): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
* Removes a scheduled job.
|
|
158
|
+
*
|
|
159
|
+
* Deletes the job metadata and schedule entry from Redis.
|
|
160
|
+
*
|
|
161
|
+
* @param id - The unique identifier of the scheduled job.
|
|
162
|
+
*/
|
|
163
|
+
remove(id: string): Promise<void>;
|
|
164
|
+
/**
|
|
165
|
+
* Lists all registered scheduled jobs.
|
|
166
|
+
*
|
|
167
|
+
* @returns An array of all scheduled job configurations.
|
|
168
|
+
*/
|
|
169
|
+
list(): Promise<ScheduledJobConfig[]>;
|
|
170
|
+
/**
|
|
171
|
+
* Starts the automatic scheduler loop.
|
|
172
|
+
*
|
|
173
|
+
* Periodically triggers `tick()` to process due jobs. Uses leader election
|
|
174
|
+
* to ensure that only one node performs the scanning in a multi-node environment.
|
|
175
|
+
*/
|
|
176
|
+
start(): Promise<void>;
|
|
177
|
+
/**
|
|
178
|
+
* Stops the automatic scheduler loop.
|
|
179
|
+
*/
|
|
180
|
+
stop(): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* Acquires the leader lock and performs a tick.
|
|
183
|
+
*
|
|
184
|
+
* @private
|
|
185
|
+
*/
|
|
186
|
+
private performTickWithLeaderElection;
|
|
187
|
+
/**
|
|
188
|
+
* Releases the leader lock.
|
|
189
|
+
*
|
|
190
|
+
* @private
|
|
191
|
+
*/
|
|
192
|
+
private releaseLeader;
|
|
193
|
+
/**
|
|
194
|
+
* Manually triggers a scheduled job immediately.
|
|
195
|
+
*
|
|
196
|
+
* Forces execution of the job regardless of its schedule, without affecting the next scheduled run time.
|
|
197
|
+
*
|
|
198
|
+
* @param id - The unique identifier of the scheduled job.
|
|
199
|
+
*/
|
|
200
|
+
runNow(id: string): Promise<void>;
|
|
201
|
+
/**
|
|
202
|
+
* Checks for and triggers tasks that are due for execution.
|
|
203
|
+
*
|
|
204
|
+
* This method should be called periodically (e.g., via a system cron or a dedicated tick loop).
|
|
205
|
+
* It scans the schedule for tasks with `nextRun <= now`, acquires a distributed lock for each,
|
|
206
|
+
* pushes them to their queue, and updates the `nextRun` time.
|
|
207
|
+
*
|
|
208
|
+
* The distributed lock ensures that in a multi-node environment, each scheduled job is executed
|
|
209
|
+
* only once per interval, even if multiple scheduler instances are running.
|
|
210
|
+
*
|
|
211
|
+
* @returns The number of jobs triggered in this tick.
|
|
212
|
+
*/
|
|
213
|
+
tick(): Promise<number>;
|
|
214
|
+
}
|