@tstdl/base 0.93.141 → 0.93.142
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/authentication/client/authentication.service.d.ts +1 -0
- package/authentication/client/authentication.service.js +3 -2
- package/circuit-breaker/circuit-breaker.d.ts +6 -4
- package/circuit-breaker/postgres/circuit-breaker.d.ts +1 -0
- package/circuit-breaker/postgres/circuit-breaker.js +8 -5
- package/circuit-breaker/tests/circuit-breaker.test.js +20 -0
- package/examples/document-management/main.js +2 -2
- package/notification/tests/notification-api.test.js +5 -1
- package/notification/tests/notification-flow.test.js +9 -6
- package/orm/decorators.d.ts +17 -4
- package/orm/decorators.js +9 -0
- package/orm/server/bootstrap.d.ts +11 -0
- package/orm/server/bootstrap.js +31 -0
- package/orm/server/drizzle/schema-converter.d.ts +3 -1
- package/orm/server/drizzle/schema-converter.js +71 -29
- package/orm/server/extension.d.ts +14 -0
- package/orm/server/extension.js +27 -0
- package/orm/server/index.d.ts +2 -0
- package/orm/server/index.js +2 -0
- package/orm/server/migration.d.ts +2 -3
- package/orm/server/migration.js +7 -21
- package/orm/server/repository.d.ts +1 -0
- package/orm/server/repository.js +19 -9
- package/orm/server/transaction.d.ts +1 -0
- package/orm/server/transaction.js +3 -0
- package/orm/tests/database-extension.test.js +63 -0
- package/orm/tests/database-migration.test.js +7 -6
- package/orm/tests/repository-compound-primary-key.test.d.ts +2 -0
- package/orm/tests/repository-compound-primary-key.test.js +234 -0
- package/orm/tests/schema-generation.test.d.ts +1 -0
- package/orm/tests/schema-generation.test.js +52 -5
- package/package.json +4 -4
- package/task-queue/README.md +0 -1
- package/task-queue/postgres/drizzle/0000_great_gwen_stacy.sql +84 -0
- package/task-queue/postgres/drizzle/meta/0000_snapshot.json +151 -68
- package/task-queue/postgres/drizzle/meta/_journal.json +2 -2
- package/task-queue/postgres/module.js +2 -1
- package/task-queue/postgres/schemas.d.ts +6 -0
- package/task-queue/postgres/task-queue.d.ts +18 -5
- package/task-queue/postgres/task-queue.js +593 -372
- package/task-queue/postgres/task.model.d.ts +9 -5
- package/task-queue/postgres/task.model.js +26 -26
- package/task-queue/task-context.d.ts +10 -5
- package/task-queue/task-context.js +5 -3
- package/task-queue/task-queue.d.ts +339 -35
- package/task-queue/task-queue.js +135 -31
- package/task-queue/tests/coverage-branch.test.js +45 -57
- package/task-queue/tests/coverage-enhancement.test.js +123 -117
- package/task-queue/tests/{extensive-dependencies.test.js → dag.test.js} +61 -32
- package/task-queue/tests/dependencies.test.js +139 -21
- package/task-queue/tests/enqueue-batch.test.js +125 -0
- package/task-queue/tests/fan-out-spawning.test.js +43 -2
- package/task-queue/tests/idempotent-replacement.test.js +54 -1
- package/task-queue/tests/missing-idempotent-tasks.test.js +9 -8
- package/task-queue/tests/queue.test.js +261 -25
- package/task-queue/tests/shutdown.test.js +41 -0
- package/task-queue/tests/transactions.test.d.ts +1 -0
- package/task-queue/tests/transactions.test.js +47 -0
- package/task-queue/tests/worker.test.js +46 -13
- package/task-queue/tests/zombie-parent.test.js +1 -1
- package/task-queue/tests/zombie-recovery.test.js +3 -3
- package/testing/integration-setup.js +5 -3
- package/utils/timing.d.ts +2 -2
- package/task-queue/postgres/drizzle/0000_wakeful_sunspot.sql +0 -82
- package/task-queue/tests/cascading-cancellations.test.js +0 -38
- package/task-queue/tests/complex.test.js +0 -122
- package/task-queue/tests/dag-dependencies.test.js +0 -41
- /package/{task-queue/tests/cascading-cancellations.test.d.ts → orm/tests/database-extension.test.d.ts} +0 -0
- /package/task-queue/tests/{complex.test.d.ts → dag.test.d.ts} +0 -0
- /package/task-queue/tests/{dag-dependencies.test.d.ts → enqueue-batch.test.d.ts} +0 -0
- /package/task-queue/tests/{extensive-dependencies.test.d.ts → shutdown.test.d.ts} +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
/** biome-ignore-all lint/nursery/noExcessiveClassesPerFile: <explanation> */
|
|
2
|
+
import type { CancellationSignal } from '../cancellation/index.js';
|
|
2
3
|
import { type EnumType } from '../enumeration/enumeration.js';
|
|
3
4
|
import { Injector } from '../injector/index.js';
|
|
4
5
|
import type { Resolvable, resolveArgumentType } from '../injector/interfaces.js';
|
|
@@ -8,14 +9,38 @@ import { Transactional } from '../orm/server/transactional.js';
|
|
|
8
9
|
import type { OneOrMany, Record, UndefinableJson } from '../types/types.js';
|
|
9
10
|
import { TaskQueueEnqueueBatch } from './enqueue-batch.js';
|
|
10
11
|
import type { ProcessWorker, TaskData, TaskDefinitionMap, TaskOfType, TaskProcessResultPayload, TaskResult, TasksResults, TasksStates, TaskState, TaskTypes } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Represents the result of processing a task.
|
|
14
|
+
* @template Result The type of the result data.
|
|
15
|
+
*/
|
|
11
16
|
export declare class TaskProcessResult<Result = unknown> {
|
|
12
17
|
readonly payload: TaskProcessResultPayload<Result>;
|
|
13
18
|
private constructor();
|
|
19
|
+
/**
|
|
20
|
+
* Creates a successful process result.
|
|
21
|
+
* @param result The optional result data.
|
|
22
|
+
*/
|
|
14
23
|
static Complete<Result>(result?: Result): TaskProcessResult<Result>;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a failed process result.
|
|
26
|
+
* @param error The error that occurred.
|
|
27
|
+
* @param fatal Whether the error is fatal and the task should not be retried.
|
|
28
|
+
*/
|
|
15
29
|
static Fail<R>(error: unknown, fatal?: boolean): TaskProcessResult<R>;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a result that reschedules the task to a specific timestamp.
|
|
32
|
+
* @param timestamp The timestamp to reschedule to.
|
|
33
|
+
*/
|
|
16
34
|
static RescheduleTo<R>(timestamp: number): TaskProcessResult<R>;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a result that reschedules the task by a specific number of milliseconds.
|
|
37
|
+
* @param milliseconds The number of milliseconds to reschedule by.
|
|
38
|
+
*/
|
|
17
39
|
static RescheduleBy<R>(milliseconds: number): TaskProcessResult<R>;
|
|
18
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Represents the status of a task in the queue.
|
|
43
|
+
*/
|
|
19
44
|
export declare const TaskStatus: {
|
|
20
45
|
/**
|
|
21
46
|
* The task is ready to be processed and is waiting for a worker.
|
|
@@ -49,40 +74,103 @@ export declare const TaskStatus: {
|
|
|
49
74
|
* The task has been manually paused and will not be dequeued until resumed.
|
|
50
75
|
*/
|
|
51
76
|
readonly Paused: "paused";
|
|
77
|
+
/**
|
|
78
|
+
* The task has failed but has remaining attempts and is waiting for its next scheduled attempt.
|
|
79
|
+
*/
|
|
80
|
+
readonly Retrying: "retrying";
|
|
81
|
+
/**
|
|
82
|
+
* The task was forcefully terminated because its execution time exceeded maxExecutionTime.
|
|
83
|
+
*/
|
|
84
|
+
readonly TimedOut: "timed-out";
|
|
85
|
+
/**
|
|
86
|
+
* The task expired in the queue before it could be picked up by a worker.
|
|
87
|
+
*/
|
|
88
|
+
readonly Expired: "expired";
|
|
89
|
+
/**
|
|
90
|
+
* The task was never attempted because one of its prerequisites failed or was cancelled.
|
|
91
|
+
*/
|
|
92
|
+
readonly Skipped: "skipped";
|
|
93
|
+
/**
|
|
94
|
+
* The task was abandoned by a worker and has exhausted all retry attempts.
|
|
95
|
+
*/
|
|
96
|
+
readonly Orphaned: "orphaned";
|
|
52
97
|
};
|
|
98
|
+
/**
|
|
99
|
+
* Type of task status.
|
|
100
|
+
*/
|
|
53
101
|
export type TaskStatus = EnumType<typeof TaskStatus>;
|
|
102
|
+
/**
|
|
103
|
+
* Represents the type of dependency between tasks.
|
|
104
|
+
*/
|
|
54
105
|
export declare const TaskDependencyType: {
|
|
106
|
+
/**
|
|
107
|
+
* The task should only be scheduled after the dependency has reached a specific status.
|
|
108
|
+
*/
|
|
55
109
|
readonly Schedule: "schedule";
|
|
110
|
+
/**
|
|
111
|
+
* The task is only considered complete after the dependency has reached a specific status.
|
|
112
|
+
*/
|
|
56
113
|
readonly Complete: "complete";
|
|
114
|
+
/**
|
|
115
|
+
* The task is only considered complete after the child task has reached a specific status.
|
|
116
|
+
*/
|
|
117
|
+
readonly Child: "child";
|
|
57
118
|
};
|
|
119
|
+
/**
|
|
120
|
+
* Type of task dependency.
|
|
121
|
+
*/
|
|
58
122
|
export type TaskDependencyType = EnumType<typeof TaskDependencyType>;
|
|
123
|
+
/**
|
|
124
|
+
* Represents a task in the queue.
|
|
125
|
+
* @template Definitions The type map of task definitions.
|
|
126
|
+
*/
|
|
59
127
|
export type Task<Definitions extends TaskDefinitionMap = Record<string, {
|
|
60
128
|
data: unknown;
|
|
61
129
|
state: unknown;
|
|
62
130
|
result: unknown;
|
|
63
131
|
}>> = {
|
|
64
132
|
[Type in TaskTypes<Definitions>]: {
|
|
133
|
+
/** The unique ID of the task. */
|
|
65
134
|
id: string;
|
|
135
|
+
/** The namespace the task belongs to. */
|
|
66
136
|
namespace: string;
|
|
137
|
+
/** The type of the task. */
|
|
67
138
|
type: Type;
|
|
139
|
+
/** The current status of the task. */
|
|
68
140
|
status: TaskStatus;
|
|
141
|
+
/**
|
|
142
|
+
* The unique token assigned to the worker processing the task.
|
|
143
|
+
* Null if the task is not currently being processed.
|
|
144
|
+
*/
|
|
69
145
|
token: string | null;
|
|
70
146
|
/**
|
|
71
147
|
* The lower the number, the higher the priority.
|
|
72
148
|
* @default 1000
|
|
73
149
|
*/
|
|
74
150
|
priority: number;
|
|
151
|
+
/** An optional key used for idempotency. */
|
|
75
152
|
idempotencyKey: string | null;
|
|
153
|
+
/** An optional trace ID for distributed tracing. */
|
|
76
154
|
traceId: string | null;
|
|
155
|
+
/** Tags associated with the task. */
|
|
77
156
|
tags: string[];
|
|
157
|
+
/** The number of unresolved schedule dependencies. */
|
|
78
158
|
unresolvedScheduleDependencies: number;
|
|
159
|
+
/** The number of unresolved completion dependencies. */
|
|
79
160
|
unresolvedCompleteDependencies: number;
|
|
161
|
+
/** Whether to skip the task if any of its dependencies fail. */
|
|
80
162
|
failFast: boolean;
|
|
163
|
+
/** The data associated with the task. */
|
|
81
164
|
data: TaskData<Definitions, Type>;
|
|
165
|
+
/** The ID of the parent task, if any. */
|
|
82
166
|
parentId: string | null;
|
|
167
|
+
/** The number of times the task has been attempted. */
|
|
83
168
|
tries: number;
|
|
169
|
+
/** The timestamp when the task was created. */
|
|
84
170
|
creationTimestamp: number;
|
|
171
|
+
/** The timestamp when the task's priority was last aged. */
|
|
85
172
|
priorityAgeTimestamp: number;
|
|
173
|
+
/** The timestamp when the task is scheduled to be processed. */
|
|
86
174
|
scheduleTimestamp: number;
|
|
87
175
|
/**
|
|
88
176
|
* Timestamp when the task most recently switched to Running state.
|
|
@@ -98,48 +186,95 @@ export type Task<Definitions extends TaskDefinitionMap = Record<string, {
|
|
|
98
186
|
* Token expiration (Soft Timeout).
|
|
99
187
|
*/
|
|
100
188
|
visibilityDeadline: number | null;
|
|
189
|
+
/** The timestamp when the task was completed. */
|
|
101
190
|
completeTimestamp: number | null;
|
|
102
191
|
/** A number between 0 and 1 indicating the progress of the task. */
|
|
103
192
|
progress: number;
|
|
104
193
|
/** A snapshot of the current state of the task. */
|
|
105
194
|
state: TaskState<Definitions, Type> | null;
|
|
195
|
+
/** The result of the task, if successful. */
|
|
106
196
|
result: TaskResult<Definitions, Type> | null;
|
|
197
|
+
/** The error that occurred, if the task failed. */
|
|
107
198
|
error: UndefinableJson | null;
|
|
108
199
|
};
|
|
109
200
|
}[Extract<keyof Definitions, string>];
|
|
201
|
+
/** Default priority for tasks. */
|
|
110
202
|
export declare const defaultTaskPriority = 1000;
|
|
203
|
+
/**
|
|
204
|
+
* Represents a task dependency specification.
|
|
205
|
+
* Can be:
|
|
206
|
+
* - A string: The ID of the task.
|
|
207
|
+
* - A number: The index of the task in the current batch.
|
|
208
|
+
* - An object: Providing ID or index, and optional namespace and required statuses.
|
|
209
|
+
*/
|
|
210
|
+
export type TaskDependencySpecification = number | string | {
|
|
211
|
+
/** The ID of the task to depend on. */
|
|
212
|
+
id?: string;
|
|
213
|
+
/** The index of the task in the batch to depend on. */
|
|
214
|
+
index?: number;
|
|
215
|
+
/** The statuses the dependency must reach. Defaults to [TaskStatus.Completed]. */
|
|
216
|
+
requiredStatuses?: TaskStatus[];
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* Options for enqueuing a task.
|
|
220
|
+
*/
|
|
111
221
|
export type EnqueueOptions = {
|
|
222
|
+
/** The priority of the task. */
|
|
112
223
|
priority?: number;
|
|
224
|
+
/** The ID of the parent task. */
|
|
113
225
|
parentId?: string;
|
|
226
|
+
/** A unique key for idempotency. */
|
|
114
227
|
idempotencyKey?: string;
|
|
228
|
+
/** Whether to replace an existing task with the same idempotency key. */
|
|
115
229
|
replace?: boolean;
|
|
230
|
+
/** Tags to associate with the task. */
|
|
116
231
|
tags?: string[];
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Tasks that must reach a specific status before this task can be scheduled.
|
|
234
|
+
* Can be an ID, an index in the batch, or an object with ID or index, and namespace.
|
|
235
|
+
*/
|
|
236
|
+
scheduleAfter?: TaskDependencySpecification[];
|
|
237
|
+
/**
|
|
238
|
+
* Tasks that must reach a specific status before this task is considered complete.
|
|
239
|
+
* Can be an ID, an index in the batch, or an object with ID or index, and namespace.
|
|
240
|
+
*/
|
|
241
|
+
completeAfter?: TaskDependencySpecification[];
|
|
242
|
+
/** Whether to skip the task if any of its dependencies fail. */
|
|
125
243
|
failFast?: boolean;
|
|
244
|
+
/** Whether the parent task should wait for this task to complete. */
|
|
126
245
|
waitForCompletion?: boolean;
|
|
246
|
+
/** The timestamp when the task should be processed. */
|
|
127
247
|
scheduleTimestamp?: number;
|
|
248
|
+
/** The duration (ms) before the task is considered expired. */
|
|
128
249
|
timeToLive?: number;
|
|
250
|
+
/** The transaction to use for the operation. */
|
|
129
251
|
transaction?: Transaction;
|
|
130
252
|
};
|
|
253
|
+
/** Options for enqueuing a single task. */
|
|
131
254
|
export type EnqueueOneOptions = EnqueueOptions;
|
|
255
|
+
/**
|
|
256
|
+
* An item to be enqueued in a batch operation.
|
|
257
|
+
* @template Definitions The type map of task definitions.
|
|
258
|
+
* @template Type The type of the task.
|
|
259
|
+
*/
|
|
132
260
|
export type EnqueueManyItem<Definitions extends TaskDefinitionMap = TaskDefinitionMap, Type extends TaskTypes<Definitions> = TaskTypes<Definitions>> = {
|
|
133
261
|
[Type in TaskTypes<Definitions>]: {
|
|
262
|
+
/** The type of the task. */
|
|
134
263
|
type: Type;
|
|
264
|
+
/** The data for the task. */
|
|
135
265
|
data: TaskData<Definitions, Type>;
|
|
136
266
|
} & EnqueueOptions;
|
|
137
267
|
}[Type];
|
|
268
|
+
/** Options for enqueuing multiple tasks in a batch. */
|
|
138
269
|
export type EnqueueManyOptions = {
|
|
270
|
+
/** Whether to replace existing tasks with the same idempotency keys. */
|
|
139
271
|
replace?: boolean;
|
|
272
|
+
/** Whether to return the enqueued tasks. */
|
|
140
273
|
returnTasks?: boolean;
|
|
274
|
+
/** The transaction to use for the operation. */
|
|
141
275
|
transaction?: Transaction;
|
|
142
276
|
};
|
|
277
|
+
/** Configuration for a task queue. */
|
|
143
278
|
export type QueueConfig = {
|
|
144
279
|
/**
|
|
145
280
|
* Duration (ms) before a worker token is considered lost.
|
|
@@ -156,8 +291,6 @@ export type QueueConfig = {
|
|
|
156
291
|
maxTries?: number;
|
|
157
292
|
/** Duration to retain terminal tasks before archival. Default: 30 days */
|
|
158
293
|
retention?: number;
|
|
159
|
-
/** Maximum number of running tasks across all workers (`null` = unlimited) */
|
|
160
|
-
globalConcurrency?: number | null;
|
|
161
294
|
/** Number of consecutive failures before tripping the circuit breaker */
|
|
162
295
|
circuitBreakerThreshold?: number;
|
|
163
296
|
/** Milliseconds to wait before trying to close the circuit breaker */
|
|
@@ -183,86 +316,191 @@ export type QueueConfig = {
|
|
|
183
316
|
/** Time to retain idempotency keys (ms). Default: 1 hour */
|
|
184
317
|
idempotencyWindow?: number;
|
|
185
318
|
};
|
|
319
|
+
/** Argument for injecting a task queue. Can be a namespace string or a full configuration. */
|
|
186
320
|
export type TaskQueueArgument = string | (QueueConfig & {
|
|
187
321
|
namespace: string;
|
|
188
322
|
});
|
|
323
|
+
/** Options for waiting for tasks to complete. */
|
|
189
324
|
export type TaskQueueWaitOptions = {
|
|
325
|
+
/** The statuses to wait for. Defaults to finalized statuses (Completed, Cancelled, Dead, TimedOut, Expired, Skipped, Orphaned). */
|
|
326
|
+
statuses?: TaskStatus[];
|
|
327
|
+
/** The interval (ms) at which to check the task status. */
|
|
190
328
|
interval?: number;
|
|
329
|
+
/** The maximum duration (ms) to wait. */
|
|
191
330
|
timeout?: number;
|
|
331
|
+
/** A signal used to cancel the wait operation. */
|
|
192
332
|
cancellationSignal?: CancellationSignal;
|
|
193
333
|
};
|
|
334
|
+
/** The result of waiting for tasks. */
|
|
194
335
|
export type TaskQueueWaitResult = {
|
|
336
|
+
/** Whether the wait was cancelled. */
|
|
195
337
|
cancelled: boolean;
|
|
196
338
|
};
|
|
197
339
|
export declare const defaultQueueConfig: Required<QueueConfig>;
|
|
340
|
+
/**
|
|
341
|
+
* Abstract base class for task queues.
|
|
342
|
+
* @template Definitions The type map of task definitions.
|
|
343
|
+
*/
|
|
198
344
|
export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap = TaskDefinitionMap> extends Transactional<QueueConfig & {
|
|
199
345
|
namespace: string;
|
|
200
346
|
}> implements Resolvable<TaskQueueArgument> {
|
|
201
|
-
|
|
347
|
+
#private;
|
|
202
348
|
protected readonly injector: Injector;
|
|
203
349
|
protected readonly config: QueueConfig & {
|
|
204
350
|
namespace: string;
|
|
205
351
|
};
|
|
206
352
|
protected readonly logger: Logger;
|
|
353
|
+
get namespace(): string;
|
|
354
|
+
/** Duration (ms) before a worker token is considered lost. */
|
|
207
355
|
abstract readonly visibilityTimeout: number;
|
|
356
|
+
/** Maximum number of attempts allowed for a task. */
|
|
208
357
|
abstract readonly maxTries: number;
|
|
358
|
+
readonly [resolveArgumentType]: TaskQueueArgument;
|
|
359
|
+
/**
|
|
360
|
+
* Starts a new batch operation for enqueuing multiple tasks.
|
|
361
|
+
* @returns A TaskQueueEnqueueBatch instance.
|
|
362
|
+
*/
|
|
209
363
|
batch(): TaskQueueEnqueueBatch<Definitions>;
|
|
364
|
+
/**
|
|
365
|
+
* Enqueues a single task.
|
|
366
|
+
* @param type The type of the task.
|
|
367
|
+
* @param data The data for the task.
|
|
368
|
+
* @param options Enqueue options.
|
|
369
|
+
* @returns A promise that resolves to the enqueued task.
|
|
370
|
+
*/
|
|
210
371
|
abstract enqueue<Type extends TaskTypes<Definitions>>(type: Type, data: TaskData<Definitions, Type>, options?: EnqueueOneOptions): Promise<TaskOfType<Definitions, Type>>;
|
|
211
372
|
/**
|
|
212
|
-
* Enqueues multiple tasks.
|
|
373
|
+
* Enqueues multiple tasks in a batch.
|
|
213
374
|
* @param items The items to enqueue.
|
|
214
|
-
* @param options Enqueue options.
|
|
215
|
-
* **NOTE:** The order of returned tasks is NOT guaranteed to match the order of input items.
|
|
375
|
+
* @param options Enqueue options.
|
|
216
376
|
*/
|
|
217
377
|
abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options?: EnqueueManyOptions & {
|
|
218
378
|
returnTasks?: false;
|
|
219
379
|
}): Promise<void>;
|
|
380
|
+
/**
|
|
381
|
+
* Enqueues multiple tasks in a batch and returns the enqueued tasks.
|
|
382
|
+
* @param items The items to enqueue.
|
|
383
|
+
* @param options Enqueue options with returnTasks set to true.
|
|
384
|
+
* @returns A promise that resolves to an array of the enqueued tasks.
|
|
385
|
+
*/
|
|
220
386
|
abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options: EnqueueManyOptions & {
|
|
221
387
|
returnTasks: true;
|
|
222
388
|
}): Promise<TaskOfType<Definitions, Type>[]>;
|
|
389
|
+
/**
|
|
390
|
+
* Enqueues multiple tasks in a batch.
|
|
391
|
+
* @param items The items to enqueue.
|
|
392
|
+
* @param options Enqueue options.
|
|
393
|
+
* @returns A promise that resolves to an array of the enqueued tasks if returnTasks is true, otherwise undefined.
|
|
394
|
+
*/
|
|
223
395
|
abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options?: EnqueueManyOptions): Promise<TaskOfType<Definitions, Type>[] | undefined>;
|
|
396
|
+
/**
|
|
397
|
+
* Checks if a task with the given ID exists in the queue.
|
|
398
|
+
* @param id The ID of the task.
|
|
399
|
+
* @param options Optional transaction.
|
|
400
|
+
* @returns A promise that resolves to true if the task exists, false otherwise.
|
|
401
|
+
*/
|
|
224
402
|
abstract has(id: string, options?: {
|
|
225
403
|
transaction?: Transaction;
|
|
226
404
|
}): Promise<boolean>;
|
|
227
|
-
|
|
405
|
+
/**
|
|
406
|
+
* Counts tasks in the current namespace.
|
|
407
|
+
* @param options Optional status filter and transaction.
|
|
408
|
+
* @returns A promise that resolves to the number of tasks.
|
|
409
|
+
*/
|
|
410
|
+
abstract count(options?: {
|
|
411
|
+
status?: TaskStatus;
|
|
412
|
+
transaction?: Transaction;
|
|
413
|
+
}): Promise<number>;
|
|
414
|
+
/**
|
|
415
|
+
* Retrieves a task by its ID.
|
|
416
|
+
* @param id The ID of the task.
|
|
417
|
+
* @param options Optional transaction.
|
|
418
|
+
* @returns A promise that resolves to the task if found, undefined otherwise.
|
|
419
|
+
*/
|
|
420
|
+
abstract getTask<Type extends TaskTypes<Definitions> = TaskTypes<Definitions>>(id: string, options?: {
|
|
228
421
|
transaction?: Transaction;
|
|
229
|
-
}): Promise<
|
|
230
|
-
|
|
422
|
+
}): Promise<TaskOfType<Definitions, Type> | undefined>;
|
|
423
|
+
/**
|
|
424
|
+
* Retrieves tasks associated with the given tags.
|
|
425
|
+
* @param tags One or more tags.
|
|
426
|
+
* @param options Optional transaction.
|
|
427
|
+
* @returns A promise that resolves to an array of tasks.
|
|
428
|
+
*/
|
|
429
|
+
abstract getManyByTags<Type extends TaskTypes<Definitions>>(tags: OneOrMany<string>, options?: {
|
|
231
430
|
transaction?: Transaction;
|
|
232
|
-
}): Promise<
|
|
431
|
+
}): Promise<TaskOfType<Definitions, Type>[]>;
|
|
432
|
+
/**
|
|
433
|
+
* Counts tasks associated with the given tags.
|
|
434
|
+
* @param tags One or more tags.
|
|
435
|
+
* @param options Optional transaction.
|
|
436
|
+
* @returns A promise that resolves to the number of tasks.
|
|
437
|
+
*/
|
|
233
438
|
abstract countByTags(tags: OneOrMany<string>, options?: {
|
|
234
439
|
transaction?: Transaction;
|
|
235
440
|
}): Promise<number>;
|
|
441
|
+
/**
|
|
442
|
+
* Retrieves all descendants of the given root task ID(s).
|
|
443
|
+
* @param rootId The root task ID or an array of root task IDs.
|
|
444
|
+
* @param options Optional transaction.
|
|
445
|
+
* @returns A promise that resolves to an array of tasks in the tree.
|
|
446
|
+
*/
|
|
236
447
|
abstract getTree(rootId: string | string[], options?: {
|
|
237
448
|
transaction?: Transaction;
|
|
238
449
|
}): Promise<Task[]>;
|
|
239
450
|
/**
|
|
240
|
-
* Waits for the specified tasks to reach a finalized state (Completed, Cancelled, or Dead) or be removed
|
|
451
|
+
* Waits for the specified tasks to reach a finalized state (Completed, Cancelled, or Dead) or be removed from the queue.
|
|
241
452
|
* @param ids The IDs of the tasks to wait for.
|
|
242
453
|
* @param options Timeout and cancellation options.
|
|
454
|
+
* @returns A promise that resolves to a TaskQueueWaitResult.
|
|
455
|
+
*/
|
|
456
|
+
abstract waitForTasks(ids: string[], options?: TaskQueueWaitOptions): Promise<TaskQueueWaitResult>;
|
|
457
|
+
/**
|
|
458
|
+
* Cancels a task.
|
|
459
|
+
* @param id The ID of the task to cancel.
|
|
460
|
+
* @param options Optional transaction.
|
|
243
461
|
*/
|
|
244
|
-
abstract waitForTasks(ids: string[], options?: {
|
|
245
|
-
timeout?: number;
|
|
246
|
-
cancellationSignal?: CancellationSignal;
|
|
247
|
-
}): Promise<TaskQueueWaitResult>;
|
|
248
462
|
abstract cancel(id: string, options?: {
|
|
249
463
|
transaction?: Transaction;
|
|
250
464
|
}): Promise<void>;
|
|
465
|
+
/**
|
|
466
|
+
* Cancels multiple tasks by their IDs.
|
|
467
|
+
* @param ids The IDs of the tasks to cancel.
|
|
468
|
+
* @param options Optional transaction.
|
|
469
|
+
*/
|
|
251
470
|
abstract cancelMany(ids: string[], options?: {
|
|
252
471
|
transaction?: Transaction;
|
|
253
472
|
}): Promise<void>;
|
|
473
|
+
/**
|
|
474
|
+
* Cancels all tasks associated with the given tags.
|
|
475
|
+
* @param tags One or more tags.
|
|
476
|
+
* @param options Optional transaction.
|
|
477
|
+
*/
|
|
254
478
|
abstract cancelManyByTags(tags: OneOrMany<string>, options?: {
|
|
255
479
|
transaction?: Transaction;
|
|
256
480
|
}): Promise<void>;
|
|
257
|
-
/**
|
|
481
|
+
/**
|
|
482
|
+
* Clears all tasks from the queue. Use with caution!
|
|
483
|
+
* @param options Optional transaction.
|
|
484
|
+
*/
|
|
258
485
|
abstract clear(options?: {
|
|
259
486
|
transaction?: Transaction;
|
|
260
487
|
}): Promise<void>;
|
|
488
|
+
/**
|
|
489
|
+
* Dequeues a task for processing.
|
|
490
|
+
* @param options Dequeue options.
|
|
491
|
+
* @returns A promise that resolves to a task if one is available, undefined otherwise.
|
|
492
|
+
*/
|
|
261
493
|
abstract dequeue<Type extends TaskTypes<Definitions>>(options?: {
|
|
262
494
|
forceDequeue?: boolean;
|
|
263
495
|
types?: Type[];
|
|
264
496
|
transaction?: Transaction;
|
|
265
497
|
}): Promise<TaskOfType<Definitions, Type> | undefined>;
|
|
498
|
+
/**
|
|
499
|
+
* Dequeues multiple tasks for processing.
|
|
500
|
+
* @param count The number of tasks to dequeue.
|
|
501
|
+
* @param options Dequeue options.
|
|
502
|
+
* @returns A promise that resolves to an array of dequeued tasks.
|
|
503
|
+
*/
|
|
266
504
|
abstract dequeueMany<Type extends TaskTypes<Definitions>>(count: number, options?: {
|
|
267
505
|
forceDequeue?: boolean;
|
|
268
506
|
types?: Type[];
|
|
@@ -270,20 +508,36 @@ export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap =
|
|
|
270
508
|
}): Promise<TaskOfType<Definitions, Type>[]>;
|
|
271
509
|
/**
|
|
272
510
|
* Reschedules a task to run at a specific time.
|
|
273
|
-
*
|
|
511
|
+
* @param id The ID of the task.
|
|
512
|
+
* @param timestamp The timestamp when the task should run.
|
|
513
|
+
* @param options Optional transaction.
|
|
274
514
|
*/
|
|
275
515
|
abstract reschedule(id: string, timestamp: number, options?: {
|
|
276
516
|
transaction?: Transaction;
|
|
277
517
|
}): Promise<void>;
|
|
518
|
+
/**
|
|
519
|
+
* Reschedules multiple tasks to run at a specific time.
|
|
520
|
+
* @param ids The IDs of the tasks.
|
|
521
|
+
* @param timestamp The timestamp when the tasks should run.
|
|
522
|
+
* @param options Optional transaction.
|
|
523
|
+
*/
|
|
278
524
|
abstract rescheduleMany(ids: string[], timestamp: number, options?: {
|
|
279
525
|
transaction?: Transaction;
|
|
280
526
|
}): Promise<void>;
|
|
527
|
+
/**
|
|
528
|
+
* Reschedules tasks with the given tags to run at a specific time.
|
|
529
|
+
* @param tags One or more tags.
|
|
530
|
+
* @param timestamp The timestamp when the tasks should run.
|
|
531
|
+
* @param options Optional transaction.
|
|
532
|
+
*/
|
|
281
533
|
abstract rescheduleManyByTags(tags: OneOrMany<string>, timestamp: number, options?: {
|
|
282
534
|
transaction?: Transaction;
|
|
283
535
|
}): Promise<void>;
|
|
284
536
|
/**
|
|
285
|
-
* Updates task progress
|
|
286
|
-
*
|
|
537
|
+
* Updates a task's progress and state and refreshes its lock.
|
|
538
|
+
* @param task The task to touch.
|
|
539
|
+
* @param options Progress, state, and optional transaction.
|
|
540
|
+
* @returns A promise that resolves to the updated task, or undefined if the task was lost.
|
|
287
541
|
*/
|
|
288
542
|
abstract touch<Type extends TaskTypes<Definitions>>(task: TaskOfType<Definitions, Type>, options?: {
|
|
289
543
|
progress?: number;
|
|
@@ -291,53 +545,100 @@ export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap =
|
|
|
291
545
|
transaction?: Transaction;
|
|
292
546
|
}): Promise<TaskOfType<Definitions, Type> | undefined>;
|
|
293
547
|
/**
|
|
294
|
-
* Updates multiple tasks' progress
|
|
295
|
-
*
|
|
548
|
+
* Updates multiple tasks' progress and state and refreshes their locks.
|
|
549
|
+
* @param tasks The tasks to touch.
|
|
550
|
+
* @param options Progresses, states, and optional transaction.
|
|
551
|
+
* @returns A promise that resolves to an array of successfully updated task IDs.
|
|
296
552
|
*/
|
|
297
553
|
abstract touchMany<Tasks extends Task<Definitions>[]>(tasks: Tasks, options?: {
|
|
298
554
|
progresses?: number[];
|
|
299
555
|
states?: TasksStates<Tasks>;
|
|
300
556
|
transaction?: Transaction;
|
|
301
557
|
}): Promise<string[]>;
|
|
558
|
+
/**
|
|
559
|
+
* Marks a task as completed.
|
|
560
|
+
* @param task The task to complete.
|
|
561
|
+
* @param options Optional result and transaction.
|
|
562
|
+
*/
|
|
302
563
|
abstract complete<Type extends TaskTypes<Definitions>>(task: TaskOfType<Definitions, Type>, options?: {
|
|
303
564
|
result?: TaskResult<Definitions, Type>;
|
|
304
565
|
transaction?: Transaction;
|
|
305
566
|
}): Promise<void>;
|
|
567
|
+
/**
|
|
568
|
+
* Marks multiple tasks as completed.
|
|
569
|
+
* @param tasks The tasks to complete.
|
|
570
|
+
* @param options Optional results and transaction.
|
|
571
|
+
*/
|
|
306
572
|
abstract completeMany<Tasks extends Task<Definitions>[]>(tasks: Tasks, options?: {
|
|
307
573
|
results?: TasksResults<Tasks>;
|
|
308
574
|
transaction?: Transaction;
|
|
309
575
|
}): Promise<void>;
|
|
576
|
+
/**
|
|
577
|
+
* Marks a task as failed.
|
|
578
|
+
* @param task The task that failed.
|
|
579
|
+
* @param error The error that occurred.
|
|
580
|
+
* @param options Optional fatal flag and transaction.
|
|
581
|
+
*/
|
|
310
582
|
abstract fail(task: Task<Definitions>, error: unknown, options?: {
|
|
311
583
|
fatal?: boolean;
|
|
312
584
|
transaction?: Transaction;
|
|
313
585
|
}): Promise<void>;
|
|
586
|
+
/**
|
|
587
|
+
* Marks multiple tasks as failed.
|
|
588
|
+
* @param tasks The tasks that failed.
|
|
589
|
+
* @param errors The errors that occurred.
|
|
590
|
+
* @param options Optional transaction.
|
|
591
|
+
*/
|
|
314
592
|
abstract failMany(tasks: Task<Definitions>[], errors: unknown[], options?: {
|
|
315
593
|
transaction?: Transaction;
|
|
316
594
|
}): Promise<void>;
|
|
595
|
+
/**
|
|
596
|
+
* Performs maintenance tasks on the queue (e.g., handling timeouts, aging priorities, archival).
|
|
597
|
+
* @param options Optional transaction.
|
|
598
|
+
*/
|
|
317
599
|
abstract maintenance(options?: {
|
|
318
600
|
transaction?: Transaction;
|
|
319
601
|
}): Promise<void>;
|
|
602
|
+
/**
|
|
603
|
+
* Restarts a task from its initial state.
|
|
604
|
+
* @param id The ID of the task to restart.
|
|
605
|
+
* @param options Optional resetState flag and transaction.
|
|
606
|
+
*/
|
|
320
607
|
abstract restart(id: string, options?: {
|
|
321
608
|
resetState?: boolean;
|
|
322
609
|
transaction?: Transaction;
|
|
323
610
|
}): Promise<void>;
|
|
324
611
|
/**
|
|
325
612
|
* Notifies the queue that new tasks might be available for processing.
|
|
326
|
-
*
|
|
327
|
-
* Useful for unit tests.
|
|
613
|
+
* @param namespace The namespace to notify. Defaults to the current namespace.
|
|
328
614
|
* @internal
|
|
329
615
|
*/
|
|
330
|
-
abstract notify(): void;
|
|
616
|
+
abstract notify(namespace?: string): void;
|
|
617
|
+
/**
|
|
618
|
+
* Gets an async iterator for consuming tasks from the queue.
|
|
619
|
+
* @param cancellationSignal A signal to stop consuming tasks.
|
|
620
|
+
* @param options Dequeue options.
|
|
621
|
+
* @returns An async iterable iterator of tasks.
|
|
622
|
+
*/
|
|
331
623
|
abstract getConsumer<Type extends TaskTypes<Definitions>>(cancellationSignal: CancellationSignal, options?: {
|
|
332
624
|
forceDequeue?: boolean;
|
|
333
625
|
types?: Type[];
|
|
334
626
|
}): AsyncIterableIterator<TaskOfType<Definitions, Type>>;
|
|
627
|
+
/**
|
|
628
|
+
* Gets an async iterator for consuming batches of tasks from the queue.
|
|
629
|
+
* @param size The batch size.
|
|
630
|
+
* @param cancellationSignal A signal to stop consuming tasks.
|
|
631
|
+
* @param options Dequeue options.
|
|
632
|
+
* @returns An async iterable iterator of task batches.
|
|
633
|
+
*/
|
|
335
634
|
abstract getBatchConsumer<Type extends TaskTypes<Definitions>>(size: number, cancellationSignal: CancellationSignal, options?: {
|
|
336
635
|
forceDequeue?: boolean;
|
|
337
636
|
types?: Type[];
|
|
338
637
|
}): AsyncIterableIterator<TaskOfType<Definitions, Type>[]>;
|
|
339
638
|
/**
|
|
340
|
-
* Starts processing tasks with the provided worker function
|
|
639
|
+
* Starts processing tasks with the provided worker function.
|
|
640
|
+
* @param options Concurrency, cancellation signal, task types, and forceDequeue flag.
|
|
641
|
+
* @param handler The worker function to process tasks.
|
|
341
642
|
*/
|
|
342
643
|
process<Type extends TaskTypes<Definitions>>({ concurrency, cancellationSignal, types, forceDequeue }: {
|
|
343
644
|
concurrency?: number;
|
|
@@ -349,7 +650,10 @@ export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap =
|
|
|
349
650
|
namespace: string;
|
|
350
651
|
};
|
|
351
652
|
/**
|
|
352
|
-
*
|
|
653
|
+
* Internal method to process tasks using a worker function.
|
|
654
|
+
* @param cancellationSignal A signal to stop processing tasks.
|
|
655
|
+
* @param handler The worker function.
|
|
656
|
+
* @param options Dequeue options.
|
|
353
657
|
*/
|
|
354
658
|
processWorker<Type extends TaskTypes<Definitions>>(cancellationSignal: CancellationSignal, handler: ProcessWorker<Definitions, Type>, options?: {
|
|
355
659
|
types?: Type[];
|