@lavoro/core 0.3.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/build/index.d.ts +595 -0
- package/build/index.js +984 -0
- package/build/index.js.map +1 -0
- package/package.json +54 -0
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import { Logger as Logger$1, LogObject, Levels } from '@julr/utils/logger';
|
|
2
|
+
import { LockFactory } from '@verrou/core';
|
|
3
|
+
import { SerializedLock, Duration } from '@verrou/core/types';
|
|
4
|
+
import { Cron } from 'croner';
|
|
5
|
+
|
|
6
|
+
declare class Logger {
|
|
7
|
+
private internalLogger;
|
|
8
|
+
constructor(internalLogger: Logger$1);
|
|
9
|
+
child(obj: LogObject): Logger;
|
|
10
|
+
trace(msg: any, obj?: any): void;
|
|
11
|
+
debug(msg: any, obj?: any): void;
|
|
12
|
+
warn(msg: any, obj?: any): void;
|
|
13
|
+
error(msg: any, obj?: any): void;
|
|
14
|
+
fatal(msg: any, obj?: any): void;
|
|
15
|
+
info(msg: any, obj?: any): void;
|
|
16
|
+
}
|
|
17
|
+
declare function createDefaultLogger(name: string, level?: Levels): Logger;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Interface to be augmented by users to define their queue names.
|
|
21
|
+
* This enables type-safe queue names throughout the application.
|
|
22
|
+
*/
|
|
23
|
+
interface QueueList {
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Interface to be augmented by users to map connections to their queue names.
|
|
27
|
+
* This enables connection-specific type-safe queue names.
|
|
28
|
+
*/
|
|
29
|
+
interface ConnectionQueues {
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Extract queue names from QueueList.
|
|
33
|
+
* Defaults to string if no queues are defined.
|
|
34
|
+
*/
|
|
35
|
+
type QueueName = keyof QueueList extends never ? string : keyof QueueList;
|
|
36
|
+
/**
|
|
37
|
+
* Extract queue names for a specific connection from ConnectionQueues.
|
|
38
|
+
*/
|
|
39
|
+
type QueueNameForConnection<C extends QueueConnectionName> = C extends keyof ConnectionQueues ? ConnectionQueues[C] : QueueName;
|
|
40
|
+
type QueueDriverStopOptions = {
|
|
41
|
+
/**
|
|
42
|
+
* Whether to wait for the jobs to finish processing before stopping.
|
|
43
|
+
*
|
|
44
|
+
* Default: true
|
|
45
|
+
*/
|
|
46
|
+
graceful?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The timeout in milliseconds to wait for the jobs to finish processing.
|
|
49
|
+
*
|
|
50
|
+
* Default: 30000 (30 seconds)
|
|
51
|
+
*/
|
|
52
|
+
timeout?: number;
|
|
53
|
+
};
|
|
54
|
+
type QueueDriverConfig = {};
|
|
55
|
+
declare abstract class QueueDriver<Config extends QueueDriverConfig = QueueDriverConfig> {
|
|
56
|
+
protected config: QueueConfig;
|
|
57
|
+
protected options: Record<string, WorkerOptions>;
|
|
58
|
+
protected driverConfig: Config;
|
|
59
|
+
protected logger: Logger;
|
|
60
|
+
protected registeredQueues: Set<string>;
|
|
61
|
+
protected registeredJobs: Map<string, new () => Job>;
|
|
62
|
+
connection: QueueConnectionName | undefined;
|
|
63
|
+
constructor(config: QueueConfig, options: Record<string, WorkerOptions>, driverConfig?: Config);
|
|
64
|
+
setLogger(logger: Logger): void;
|
|
65
|
+
protected getMergedWorkerOptions(queue: QueueName, options?: WorkerOptions): WorkerOptions;
|
|
66
|
+
listen(queue: QueueName, options?: WorkerOptions): Promise<void>;
|
|
67
|
+
register(job: new () => Job): Promise<void>;
|
|
68
|
+
unregister(job: new () => Job): Promise<void>;
|
|
69
|
+
start(): Promise<void>;
|
|
70
|
+
stop(_options?: QueueDriverStopOptions): Promise<void>;
|
|
71
|
+
protected checkIfQueueIsRegistered(queue: string): void;
|
|
72
|
+
protected checkIfJobIsRegistered(job: string): void;
|
|
73
|
+
getDefaultQueue(): QueueName;
|
|
74
|
+
/**
|
|
75
|
+
* Parent method to be extended by the driver.
|
|
76
|
+
* It implements checks for the job and queue being registered.
|
|
77
|
+
*/
|
|
78
|
+
enqueue<T extends Job, P extends Payload<T>>(job: T, payload: P): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Create a lock factory instance for this driver.
|
|
81
|
+
*
|
|
82
|
+
* Each driver implementation should return a
|
|
83
|
+
* [Verrou LockFactory instance](https://verrou.dev/docs/quick-setup#lockfactory-api)
|
|
84
|
+
* that matches the driver's backing store.
|
|
85
|
+
*
|
|
86
|
+
* @returns A LockFactory instance
|
|
87
|
+
*/
|
|
88
|
+
abstract createLockProvider(): LockFactory;
|
|
89
|
+
/**
|
|
90
|
+
* Clean up resources associated with a lock factory created by this driver.
|
|
91
|
+
* This is called when the queue is stopped to ensure proper resource cleanup.
|
|
92
|
+
*
|
|
93
|
+
* @param lockFactory - The lock factory instance to clean up
|
|
94
|
+
*/
|
|
95
|
+
destroyLockProvider(_lockFactory: LockFactory): Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
declare class PendingDispatch<T extends Job, P extends Payload<T>, C extends QueueConnectionName = DefaultConnection extends {
|
|
99
|
+
name: infer N;
|
|
100
|
+
} ? N extends QueueConnectionName ? N : QueueConnectionName : QueueConnectionName> {
|
|
101
|
+
protected job: T;
|
|
102
|
+
protected payload: P;
|
|
103
|
+
constructor(job: T, payload: P);
|
|
104
|
+
onConnection<NewC extends QueueConnectionName>(connection: NewC): PendingDispatch<T, P, NewC>;
|
|
105
|
+
onQueue(queue: QueueNameForConnection<C>): this;
|
|
106
|
+
protected execute(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* By defining the "then" method, PendingDispatch becomes "thenable",
|
|
109
|
+
* allowing it to trigger automatically when await is called.
|
|
110
|
+
*/
|
|
111
|
+
then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
declare class Queue {
|
|
115
|
+
private config;
|
|
116
|
+
private drivers;
|
|
117
|
+
private started;
|
|
118
|
+
private logger;
|
|
119
|
+
private scheduledJobs;
|
|
120
|
+
private lockFactories;
|
|
121
|
+
constructor(config: QueueConfig & {
|
|
122
|
+
logger?: Logger;
|
|
123
|
+
});
|
|
124
|
+
private createDriver;
|
|
125
|
+
start(): Promise<void>;
|
|
126
|
+
stop(options?: QueueDriverStopOptions): Promise<void>;
|
|
127
|
+
protected register(job: new () => Job): Promise<void>;
|
|
128
|
+
protected unregister(job: new () => Job): Promise<void>;
|
|
129
|
+
enqueue<T extends Job, P extends Payload<T>>(job: T, payload: P): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Get the default connection name.
|
|
132
|
+
*
|
|
133
|
+
* Returns the name of the first available connection.
|
|
134
|
+
* Throws an error if no connections are available.
|
|
135
|
+
*/
|
|
136
|
+
getDefaultConnection(): QueueConnectionName;
|
|
137
|
+
getDefaultQueue(): QueueName;
|
|
138
|
+
/**
|
|
139
|
+
* Get the lock factory instance for a specific connection.
|
|
140
|
+
* Returns the explicitly configured lock provider or the auto-created lock factory.
|
|
141
|
+
* Throws an error if no lock factory is available.
|
|
142
|
+
*
|
|
143
|
+
* @param connection - The connection name
|
|
144
|
+
* @returns The lock factory instance
|
|
145
|
+
*/
|
|
146
|
+
getLockProvider(connection: QueueConnectionName): LockFactory;
|
|
147
|
+
/**
|
|
148
|
+
* Register a scheduled job ID with this Queue instance.
|
|
149
|
+
* This allows the Queue to clean up scheduled jobs when it stops.
|
|
150
|
+
*/
|
|
151
|
+
registerScheduledJob(id: string): void;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
type Payload<T extends Job> = T extends Job<infer P> ? P : unknown;
|
|
155
|
+
type PayloadWithLock<T extends Job, P extends Payload<T>> = P & {
|
|
156
|
+
_lock?: SerializedLock;
|
|
157
|
+
};
|
|
158
|
+
type Options = {
|
|
159
|
+
connection?: QueueConnectionName;
|
|
160
|
+
queue?: string;
|
|
161
|
+
retries: number;
|
|
162
|
+
delay: number;
|
|
163
|
+
};
|
|
164
|
+
declare abstract class Job<P = unknown> {
|
|
165
|
+
private _id;
|
|
166
|
+
options: Options;
|
|
167
|
+
static compileName(queue: string, name: string): string;
|
|
168
|
+
static parseName(name: string): {
|
|
169
|
+
queue: string;
|
|
170
|
+
name: string;
|
|
171
|
+
};
|
|
172
|
+
get connection(): QueueConnectionName | undefined;
|
|
173
|
+
set connection(connection: QueueConnectionName | undefined);
|
|
174
|
+
get queue(): string | undefined;
|
|
175
|
+
set queue(queue: string | undefined);
|
|
176
|
+
get name(): string;
|
|
177
|
+
get id(): string;
|
|
178
|
+
set id(id: string);
|
|
179
|
+
get fullyQualifiedName(): string;
|
|
180
|
+
private static defaultQueueServiceResolver;
|
|
181
|
+
static setDefaultQueueServiceResolver(queueServiceResolver: () => Promise<Queue>): void;
|
|
182
|
+
setQueueServiceResolver(queueServiceResolver: () => Promise<Queue>): void;
|
|
183
|
+
private queueServiceResolver?;
|
|
184
|
+
get getQueueServiceResolver(): () => Promise<Queue>;
|
|
185
|
+
/**
|
|
186
|
+
* Handle the job with the typed payload.
|
|
187
|
+
*/
|
|
188
|
+
abstract handle(payload: P): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* Dispatch a job of type with a typed payload.
|
|
191
|
+
*/
|
|
192
|
+
static dispatch<T extends Job, P extends Payload<T>>(this: new () => T, payload: P): PendingDispatch<T, P>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
type WorkerOptions = {
|
|
196
|
+
concurrency?: number;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* Constructor type for an abstract QueueDriver
|
|
200
|
+
* that accepts optional driver-specific config.
|
|
201
|
+
*/
|
|
202
|
+
type QueueDriverConstructor<Driver extends QueueDriver = QueueDriver> = new (config: QueueConfig, queues: Record<string, WorkerOptions>, driverConfig?: any) => Driver;
|
|
203
|
+
/**
|
|
204
|
+
* Driver descriptor that combines both the driver and its config.
|
|
205
|
+
* This is used by builder functions like postgres() and memory().
|
|
206
|
+
*/
|
|
207
|
+
type ConfiguredDriver<Driver extends QueueDriver = QueueDriver, Config extends QueueDriverConfig = QueueDriverConfig> = {
|
|
208
|
+
constructor: QueueDriverConstructor<Driver>;
|
|
209
|
+
config?: Config;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Configuration for a specific queue connection.
|
|
213
|
+
*/
|
|
214
|
+
type QueueConnectionConfig<Driver extends QueueDriver = QueueDriver, Queues extends Record<string, WorkerOptions> = Record<string, WorkerOptions>> = {
|
|
215
|
+
driver: ConfiguredDriver<Driver>;
|
|
216
|
+
queues: Queues;
|
|
217
|
+
lockProvider?: LockFactory;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Interface to be augmented by users to define their connection names.
|
|
221
|
+
* This enables type-safe connection names throughout the application.
|
|
222
|
+
*
|
|
223
|
+
* Each key should be a connection name mapped to the connection's configuration type.
|
|
224
|
+
*
|
|
225
|
+
* Usage in config files:
|
|
226
|
+
* ```ts
|
|
227
|
+
* declare module 'lavoro' {
|
|
228
|
+
* export interface QueueConnections
|
|
229
|
+
* extends InferConnections<typeof queueConfig> {}
|
|
230
|
+
* }
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
interface QueueConnections {
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Interface to be augmented by users to define their default connection.
|
|
237
|
+
* This is used to provide correct type hints when no explicit connection is specified.
|
|
238
|
+
*/
|
|
239
|
+
interface DefaultConnection {
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* List of registered queue connections
|
|
243
|
+
* and their related driver configuration.
|
|
244
|
+
*/
|
|
245
|
+
type QueueConnectionsList = Record<string, QueueConnectionConfig<QueueDriver>>;
|
|
246
|
+
/**
|
|
247
|
+
* Possible connection names using keys from augmented QueueConnections.
|
|
248
|
+
*
|
|
249
|
+
* If QueueConnections is not augmented, defaults to string.
|
|
250
|
+
*/
|
|
251
|
+
type QueueConnectionName = keyof QueueConnections extends never ? string : keyof QueueConnections;
|
|
252
|
+
/**
|
|
253
|
+
* Queue service configuration
|
|
254
|
+
*/
|
|
255
|
+
type QueueConfig = {
|
|
256
|
+
jobs: readonly (new () => Job)[];
|
|
257
|
+
worker?: boolean;
|
|
258
|
+
connection: QueueConnectionName;
|
|
259
|
+
connections: QueueConnectionsList;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Infer connection names from the config
|
|
263
|
+
*/
|
|
264
|
+
type InferConnections<T> = T extends {
|
|
265
|
+
connections: infer Connections;
|
|
266
|
+
} ? Connections : never;
|
|
267
|
+
/**
|
|
268
|
+
* Infer the default connection name from the config
|
|
269
|
+
*/
|
|
270
|
+
type InferDefaultConnection<T> = T extends {
|
|
271
|
+
connection: infer Connection;
|
|
272
|
+
} ? Connection : never;
|
|
273
|
+
/**
|
|
274
|
+
* Infer queue names from all connections
|
|
275
|
+
*/
|
|
276
|
+
type InferQueueNames<T> = T extends {
|
|
277
|
+
connections: infer Connections;
|
|
278
|
+
} ? Connections extends Record<string, {
|
|
279
|
+
queues: Record<string, any>;
|
|
280
|
+
}> ? {
|
|
281
|
+
[K in {
|
|
282
|
+
[CK in keyof Connections]: keyof Connections[CK]['queues'];
|
|
283
|
+
}[keyof Connections]]: never;
|
|
284
|
+
} : never : never;
|
|
285
|
+
/**
|
|
286
|
+
* Infer queue names for a specific connection
|
|
287
|
+
*/
|
|
288
|
+
type InferQueueNamesForConnection<T, ConnectionName extends string> = T extends {
|
|
289
|
+
connections: infer Connections;
|
|
290
|
+
} ? Connections extends Record<string, {
|
|
291
|
+
queues: Record<string, any>;
|
|
292
|
+
}> ? ConnectionName extends keyof Connections ? keyof Connections[ConnectionName]['queues'] : never : never : never;
|
|
293
|
+
/**
|
|
294
|
+
* Infer the connection-to-queues mapping from the config
|
|
295
|
+
* Returns a mapped type where each connection name maps to its queue names
|
|
296
|
+
*/
|
|
297
|
+
type InferConnectionQueues<T> = T extends {
|
|
298
|
+
connections: infer Connections;
|
|
299
|
+
} ? Connections extends Record<string, {
|
|
300
|
+
queues: Record<string, any>;
|
|
301
|
+
}> ? {
|
|
302
|
+
[K in keyof Connections]: keyof Connections[K]['queues'];
|
|
303
|
+
} : never : never;
|
|
304
|
+
|
|
305
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
306
|
+
|
|
307
|
+
type QueueConfigWithConnections<Connections, Connection> = QueueConfig & {
|
|
308
|
+
connection: Connection;
|
|
309
|
+
connections: Connections;
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* Define config for queue service.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```ts
|
|
316
|
+
* // config/queue.ts
|
|
317
|
+
* import { memory } from '@lavoro/memory'
|
|
318
|
+
* import { postgres } from '@lavoro/postgres'
|
|
319
|
+
*
|
|
320
|
+
* const config = {
|
|
321
|
+
* jobs: [...],
|
|
322
|
+
* connection: 'main',
|
|
323
|
+
* connections: {
|
|
324
|
+
* main: {
|
|
325
|
+
* driver: memory(),
|
|
326
|
+
* queues: {
|
|
327
|
+
* default: { concurrency: 1 },
|
|
328
|
+
* emails: { concurrency: 3 },
|
|
329
|
+
* },
|
|
330
|
+
* },
|
|
331
|
+
* background: {
|
|
332
|
+
* driver: postgres({ ... }),
|
|
333
|
+
* queues: {
|
|
334
|
+
* 'heavy-tasks': { concurrency: 2 },
|
|
335
|
+
* reports: { concurrency: 1 },
|
|
336
|
+
* },
|
|
337
|
+
* },
|
|
338
|
+
* },
|
|
339
|
+
* }
|
|
340
|
+
*
|
|
341
|
+
* const queueConfig = defineConfig(config)
|
|
342
|
+
*
|
|
343
|
+
* // Type augmentation to enable type-safe queue names
|
|
344
|
+
* declare module '@lavoro/core' {
|
|
345
|
+
* interface QueueList extends InferQueueNames<typeof config> {}
|
|
346
|
+
* interface DefaultConnection {
|
|
347
|
+
* name: InferDefaultConnection<typeof config>
|
|
348
|
+
* }
|
|
349
|
+
* interface QueueConnections extends InferConnections<typeof config> {}
|
|
350
|
+
* interface ConnectionQueues extends InferConnectionQueues<typeof config> {}
|
|
351
|
+
* }
|
|
352
|
+
* ```
|
|
353
|
+
*
|
|
354
|
+
* After defining your queue names, you'll get autocomplete and type checking:
|
|
355
|
+
* ```ts
|
|
356
|
+
* await queue.listen('emails') // ✓ Valid
|
|
357
|
+
* await queue.listen('heavy-tasks') // ✓ Valid
|
|
358
|
+
* await queue.listen('invalid') // ✗ Type error
|
|
359
|
+
*
|
|
360
|
+
* await job.dispatch(payload).onQueue('emails') // ✓ Valid
|
|
361
|
+
* await job.dispatch(payload).onQueue('typo') // ✗ Type error
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
declare function defineConfig<const Connections extends QueueConnectionsList, const Connection extends keyof Connections = keyof Connections>(config: QueueConfigWithConnections<Connections, Connection>): QueueConfigWithConnections<Connections, Connection>;
|
|
365
|
+
|
|
366
|
+
type ScheduleInterval = 'second' | 'two seconds' | 'three seconds' | 'four seconds' | 'five seconds' | 'ten seconds' | 'fifteen seconds' | 'twenty seconds' | 'thirty seconds' | 'minute' | 'two minutes' | 'three minutes' | 'four minutes' | 'five minutes' | 'ten minutes' | 'fifteen minutes' | 'twenty minutes' | 'thirty minutes' | 'hour' | 'two hours' | 'three hours' | 'four hours' | 'five hours' | 'six hours' | 'seven hours' | 'eight hours' | 'nine hours' | 'ten hours' | 'eleven hours' | 'twelve hours' | 'day' | 'week' | 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'month' | 'last day of month';
|
|
367
|
+
type ScheduleIntervalDayOfWeek = 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday';
|
|
368
|
+
type Hour = `${0 | 1 | 2}${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`;
|
|
369
|
+
type Minute = `${0 | 1 | 2 | 3 | 4 | 5}${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`;
|
|
370
|
+
type ScheduleIntervalTime = `${Hour}:${Minute}`;
|
|
371
|
+
declare function parseTime(time: ScheduleIntervalTime): [number, number];
|
|
372
|
+
interface IntervalCronOptions {
|
|
373
|
+
/** Minute to run. Default: 0 */
|
|
374
|
+
minute?: number;
|
|
375
|
+
/** Hour to run. Default: 0 */
|
|
376
|
+
hour?: number;
|
|
377
|
+
/** Day of month to run. Default: 1 */
|
|
378
|
+
dayOfMonth?: number;
|
|
379
|
+
/** Day of week to run. Default: 0 */
|
|
380
|
+
dayOfWeek?: ScheduleIntervalDayOfWeek;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Converts a ScheduleInterval to a cron pattern string.
|
|
384
|
+
* For longer intervals (hour, day, week, etc.), you can customize when they run.
|
|
385
|
+
*
|
|
386
|
+
* @param interval - The schedule interval
|
|
387
|
+
* @param options - Options to customize the cron pattern
|
|
388
|
+
* @returns A cron pattern string
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* intervalToCron('minute') // '* * * * *' - every minute
|
|
392
|
+
* intervalToCron('hour', { minute: 30 }) // '30 * * * *' - every hour at :30
|
|
393
|
+
* intervalToCron('day', { hour: 14, minute: 30 }) // '30 14 * * *' - daily at 2:30 PM
|
|
394
|
+
* intervalToCron('week', { dayOfWeek: 1, hour: 9 }) // '0 9 * * 1' - Mondays at 9 AM
|
|
395
|
+
*/
|
|
396
|
+
declare function intervalToCron(interval: ScheduleInterval, options?: IntervalCronOptions): string;
|
|
397
|
+
|
|
398
|
+
declare const getDistributedLockKey: (name: string) => string;
|
|
399
|
+
declare class PendingSchedule {
|
|
400
|
+
protected name: string;
|
|
401
|
+
protected cb: (serializedLock?: SerializedLock) => MaybePromise<void>;
|
|
402
|
+
protected lockProviderResolver: () => LockFactory;
|
|
403
|
+
protected cronPattern?: string;
|
|
404
|
+
protected interval: ScheduleInterval;
|
|
405
|
+
protected intervalOptions?: IntervalCronOptions;
|
|
406
|
+
protected distributedLockOptions: {
|
|
407
|
+
/**
|
|
408
|
+
* The lock key is based on the task name to ensure only one instance runs
|
|
409
|
+
*
|
|
410
|
+
* @param name - The task name
|
|
411
|
+
* @returns The lock key
|
|
412
|
+
*/
|
|
413
|
+
key: (name: string) => string;
|
|
414
|
+
/**
|
|
415
|
+
* The lock TTL is the duration for which the lock is held
|
|
416
|
+
*/
|
|
417
|
+
ttl: Duration;
|
|
418
|
+
/**
|
|
419
|
+
* Whether to allow overlapping executions of the same task
|
|
420
|
+
*/
|
|
421
|
+
overlap: boolean;
|
|
422
|
+
/**
|
|
423
|
+
* Whether to hand off the lock to the queue worker
|
|
424
|
+
*/
|
|
425
|
+
handOff: boolean;
|
|
426
|
+
};
|
|
427
|
+
constructor(name: string, cb: (serializedLock?: SerializedLock) => MaybePromise<void>, lockProviderResolver: () => LockFactory);
|
|
428
|
+
/**
|
|
429
|
+
* Schedule using a cron pattern.
|
|
430
|
+
* You can use https://crontab.guru to generate a cron pattern.
|
|
431
|
+
*
|
|
432
|
+
* @param pattern - Cron pattern string
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* Schedule.call('my-task', () => {}).cron('0 0 * * *') // Daily at midnight
|
|
436
|
+
* Schedule.call('my-task', () => {}).cron('*\/5 * * * *') // Every 5 minutes
|
|
437
|
+
* Schedule.call('my-task', () => {}).cron('0 *\/2 * * *') // Every 2 hours
|
|
438
|
+
*/
|
|
439
|
+
cron(pattern: string): this;
|
|
440
|
+
/**
|
|
441
|
+
* Schedule to run at a specific interval.
|
|
442
|
+
* For longer intervals (hour, day, week, etc.), you can customize when they run.
|
|
443
|
+
*
|
|
444
|
+
* @param interval - The schedule interval
|
|
445
|
+
* @param options - Options to customize the cron pattern
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* Schedule.call('my-task', () => {}).every('minute')
|
|
449
|
+
* Schedule.call('my-task', () => {}).every('hour', { minute: 30 })
|
|
450
|
+
* Schedule.call('my-task', () => {}).every('day', { hour: 14, minute: 30 })
|
|
451
|
+
* Schedule.call('my-task', () => {}).every('week', { dayOfWeek: 1, hour: 9 })
|
|
452
|
+
*/
|
|
453
|
+
every(interval: ScheduleInterval, options?: IntervalCronOptions): this;
|
|
454
|
+
on(dayOfWeek: ScheduleIntervalDayOfWeek): this;
|
|
455
|
+
at(time: ScheduleIntervalTime): this;
|
|
456
|
+
/**
|
|
457
|
+
* Set the duration during which other instances
|
|
458
|
+
* of the same task will be prevented from running.
|
|
459
|
+
*
|
|
460
|
+
* This should be roughly the duration of the task execution or longer
|
|
461
|
+
* since the lock will be released automatically after the task execution.
|
|
462
|
+
*
|
|
463
|
+
* @param ttl - The lock duration
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* Schedule.call('my-task', () => {}).lockFor('10s')
|
|
467
|
+
*/
|
|
468
|
+
lockFor(ttl: Duration): this;
|
|
469
|
+
/**
|
|
470
|
+
* Allow overlapping executions of the same task.
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* Schedule.call('my-task', () => {}).overlapping()
|
|
474
|
+
*/
|
|
475
|
+
overlapping(): this;
|
|
476
|
+
protected execute(): Promise<void>;
|
|
477
|
+
/**
|
|
478
|
+
* By defining the "then" method, PendingDispatch becomes "thenable",
|
|
479
|
+
* allowing it to trigger automatically when await is called.
|
|
480
|
+
*/
|
|
481
|
+
then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
declare class PendingJobSchedule<T extends Job, P extends Payload<T>, C extends QueueConnectionName = DefaultConnection extends {
|
|
485
|
+
name: infer N;
|
|
486
|
+
} ? N extends QueueConnectionName ? N : QueueConnectionName : QueueConnectionName> extends PendingSchedule {
|
|
487
|
+
protected job: T;
|
|
488
|
+
protected payload: PayloadWithLock<T, P>;
|
|
489
|
+
protected lockProviderResolver: () => LockFactory;
|
|
490
|
+
/**
|
|
491
|
+
* Use composition pattern and store pending
|
|
492
|
+
* job dispatch inside a pending schedule.
|
|
493
|
+
*/
|
|
494
|
+
protected dispatch: PendingDispatch<T, PayloadWithLock<T, P>, C>;
|
|
495
|
+
private _connection?;
|
|
496
|
+
private _queue?;
|
|
497
|
+
private get jobName();
|
|
498
|
+
constructor(job: T, payload: PayloadWithLock<T, P>, lockProviderResolver: () => LockFactory);
|
|
499
|
+
onConnection<NewC extends QueueConnectionName>(connection: NewC): PendingJobSchedule<T, P, NewC>;
|
|
500
|
+
onQueue(queue: QueueNameForConnection<C>): this;
|
|
501
|
+
protected execute(): Promise<void>;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Default lock provider instance for distributed locking.
|
|
506
|
+
* Uses memory store by default.
|
|
507
|
+
*/
|
|
508
|
+
declare const defaultScheduleLockProvider: LockFactory;
|
|
509
|
+
declare class Schedule {
|
|
510
|
+
/**
|
|
511
|
+
* Default lock provider resolver for distributed locking.
|
|
512
|
+
* Returns the default memory-based instance if not overridden.
|
|
513
|
+
*/
|
|
514
|
+
private static defaultLockProviderResolver;
|
|
515
|
+
/**
|
|
516
|
+
* Set a custom lock provider resolver for distributed locking.
|
|
517
|
+
*
|
|
518
|
+
* This allows dynamic resolution of lock provider instances, useful when
|
|
519
|
+
* integrating with Queue or other services that manage lock instances.
|
|
520
|
+
*
|
|
521
|
+
* @param resolver - Function that returns a LockFactory instance
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* import { LockFactory } from '@verrou/core'
|
|
525
|
+
* import { redisStore } from '@verrou/core/drivers/redis'
|
|
526
|
+
*
|
|
527
|
+
* const customLockProvider = new LockFactory(
|
|
528
|
+
* redisStore({ connection: redisClient })
|
|
529
|
+
* )
|
|
530
|
+
*
|
|
531
|
+
* Schedule.setLockProviderResolver(() => customLockProvider)
|
|
532
|
+
*/
|
|
533
|
+
static setLockProviderResolver(resolver: () => LockFactory): void;
|
|
534
|
+
/**
|
|
535
|
+
* Get the current lock provider instance from the resolver.
|
|
536
|
+
* @internal
|
|
537
|
+
*/
|
|
538
|
+
static getLockProvider(): LockFactory;
|
|
539
|
+
/**
|
|
540
|
+
* Clear all scheduled tasks (or specific one if name is specified).
|
|
541
|
+
*
|
|
542
|
+
* @param name - The name of the task to clear
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* Schedule.clear() // Clear all tasks
|
|
546
|
+
* Schedule.clear('my-task') // Clear specific task
|
|
547
|
+
*/
|
|
548
|
+
static clear(name?: string): void;
|
|
549
|
+
/**
|
|
550
|
+
* Schedule a callback to run at specified intervals.
|
|
551
|
+
*
|
|
552
|
+
* @param name - Unique identifier for the task (required for distributed systems)
|
|
553
|
+
* @param cb - The callback function to execute
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* // Using cron pattern
|
|
557
|
+
* const cleanupUsers = async () => { ... }
|
|
558
|
+
* Schedule.call('cleanup-users', cleanupUsers).cron('0 0 * * *')
|
|
559
|
+
*
|
|
560
|
+
* // Using convenience methods
|
|
561
|
+
* Schedule.call('hourly-task', () => { ... }).hourly()
|
|
562
|
+
* Schedule.call('daily-task', () => { ... }).daily()
|
|
563
|
+
*/
|
|
564
|
+
static call(name: string, cb: () => MaybePromise<void>): PendingSchedule;
|
|
565
|
+
/**
|
|
566
|
+
* Schedule a job to run at specified intervals.
|
|
567
|
+
*
|
|
568
|
+
* You can specify the connection and queue to use for the job
|
|
569
|
+
* the same way you would dispatch a job.
|
|
570
|
+
*
|
|
571
|
+
* @param job - The job class to schedule
|
|
572
|
+
* @param payload - The payload to pass to the job
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* Schedule.job(TestJob, { arg1: 'hello', arg2: 1 }).every('minute')
|
|
576
|
+
* Schedule.job(TestJob, { arg1: 'hello', arg2: 1 })
|
|
577
|
+
* .onConnection('main')
|
|
578
|
+
* .onQueue('default')
|
|
579
|
+
* .every('minute')
|
|
580
|
+
*/
|
|
581
|
+
static job<T extends Job, P extends Payload<T>>(job: new () => T, payload: P): PendingJobSchedule<T, P>;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Cron instance registry for internal use
|
|
586
|
+
*/
|
|
587
|
+
declare class ScheduleRegistry {
|
|
588
|
+
private static instances;
|
|
589
|
+
static add(name: string, cron: Cron): void;
|
|
590
|
+
static all(): Record<string, Cron>;
|
|
591
|
+
static get(name: string): Cron | undefined;
|
|
592
|
+
static clear(name?: string): void;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export { type ConfiguredDriver, type ConnectionQueues, type DefaultConnection, type InferConnectionQueues, type InferConnections, type InferDefaultConnection, type InferQueueNames, type InferQueueNamesForConnection, type IntervalCronOptions, Job, Logger, type MaybePromise, type Payload, PendingDispatch, PendingJobSchedule, PendingSchedule, Queue, type QueueConfig, type QueueConnectionConfig, type QueueConnectionName, type QueueConnections, type QueueConnectionsList, QueueDriver, type QueueDriverConfig, type QueueDriverStopOptions, type QueueList, type QueueName, type QueueNameForConnection, Schedule, type ScheduleInterval, type ScheduleIntervalTime, ScheduleRegistry, type WorkerOptions, createDefaultLogger, defaultScheduleLockProvider, defineConfig, getDistributedLockKey, intervalToCron, parseTime };
|