@tstdl/base 0.93.140 → 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/application/application.d.ts +1 -1
- package/application/application.js +1 -1
- package/application/providers.d.ts +20 -2
- package/application/providers.js +34 -7
- package/audit/module.d.ts +5 -0
- package/audit/module.js +9 -1
- package/authentication/client/authentication.service.d.ts +1 -0
- package/authentication/client/authentication.service.js +3 -2
- package/authentication/server/module.d.ts +5 -0
- package/authentication/server/module.js +9 -1
- package/authentication/tests/authentication.api-controller.test.js +1 -1
- package/authentication/tests/authentication.api-request-token.provider.test.js +1 -1
- package/authentication/tests/authentication.client-service.test.js +1 -1
- 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/postgres/module.d.ts +1 -0
- package/circuit-breaker/postgres/module.js +5 -1
- package/circuit-breaker/tests/circuit-breaker.test.js +20 -0
- package/document-management/server/configure.js +5 -1
- package/document-management/server/module.d.ts +1 -1
- package/document-management/server/module.js +1 -1
- package/document-management/server/services/document-management-ancillary.service.js +1 -1
- package/document-management/tests/ai-config-hierarchy.test.js +0 -5
- package/document-management/tests/document-management-ai-overrides.test.js +0 -1
- package/document-management/tests/document-validation-ai-overrides.test.js +0 -1
- package/examples/document-management/main.d.ts +1 -0
- package/examples/document-management/main.js +14 -11
- package/key-value-store/postgres/module.d.ts +1 -0
- package/key-value-store/postgres/module.js +5 -1
- package/lock/postgres/module.d.ts +1 -0
- package/lock/postgres/module.js +5 -1
- package/mail/module.d.ts +5 -1
- package/mail/module.js +11 -6
- package/module/modules/web-server.module.js +2 -3
- package/notification/server/module.d.ts +1 -0
- package/notification/server/module.js +5 -1
- package/notification/tests/notification-api.test.js +5 -1
- package/notification/tests/notification-flow.test.js +8 -5
- package/orm/decorators.d.ts +22 -5
- package/orm/decorators.js +10 -1
- 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 +85 -56
- package/orm/server/encryption.d.ts +0 -1
- package/orm/server/encryption.js +1 -4
- package/orm/server/extension.d.ts +14 -0
- package/orm/server/extension.js +27 -0
- package/orm/server/index.d.ts +3 -6
- package/orm/server/index.js +3 -6
- package/orm/server/migration.d.ts +18 -0
- package/orm/server/migration.js +58 -0
- package/orm/server/repository.d.ts +2 -1
- package/orm/server/repository.js +19 -9
- package/orm/server/transaction.d.ts +6 -10
- package/orm/server/transaction.js +25 -26
- package/orm/server/transactional.js +3 -3
- package/orm/tests/database-extension.test.js +63 -0
- package/orm/tests/database-migration.test.js +83 -0
- package/orm/tests/encryption.test.js +3 -4
- 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/orm/utils.d.ts +17 -2
- package/orm/utils.js +49 -1
- package/package.json +5 -4
- package/rate-limit/postgres/module.d.ts +1 -0
- package/rate-limit/postgres/module.js +5 -1
- package/reflection/decorator-data.js +11 -12
- package/task-queue/README.md +2 -10
- package/task-queue/postgres/drizzle/0000_great_gwen_stacy.sql +84 -0
- package/task-queue/postgres/drizzle/meta/0000_snapshot.json +250 -89
- package/task-queue/postgres/drizzle/meta/_journal.json +2 -2
- package/task-queue/postgres/module.d.ts +1 -0
- package/task-queue/postgres/module.js +6 -1
- package/task-queue/postgres/schemas.d.ts +15 -6
- package/task-queue/postgres/schemas.js +4 -3
- package/task-queue/postgres/task-queue.d.ts +18 -15
- package/task-queue/postgres/task-queue.js +797 -499
- package/task-queue/postgres/task.model.d.ts +20 -9
- package/task-queue/postgres/task.model.js +65 -39
- package/task-queue/task-context.d.ts +12 -7
- package/task-queue/task-context.js +8 -6
- package/task-queue/task-queue.d.ts +364 -43
- package/task-queue/task-queue.js +153 -41
- package/task-queue/tests/coverage-branch.test.d.ts +1 -0
- package/task-queue/tests/coverage-branch.test.js +395 -0
- package/task-queue/tests/coverage-enhancement.test.d.ts +1 -0
- package/task-queue/tests/coverage-enhancement.test.js +150 -0
- package/task-queue/tests/dag.test.d.ts +1 -0
- package/task-queue/tests/dag.test.js +188 -0
- package/task-queue/tests/dependencies.test.js +165 -47
- package/task-queue/tests/enqueue-batch.test.d.ts +1 -0
- package/task-queue/tests/enqueue-batch.test.js +125 -0
- package/task-queue/tests/fan-out-spawning.test.d.ts +1 -0
- package/task-queue/tests/fan-out-spawning.test.js +94 -0
- package/task-queue/tests/idempotent-replacement.test.d.ts +1 -0
- package/task-queue/tests/idempotent-replacement.test.js +114 -0
- package/task-queue/tests/missing-idempotent-tasks.test.d.ts +1 -0
- package/task-queue/tests/missing-idempotent-tasks.test.js +39 -0
- package/task-queue/tests/queue.test.js +294 -49
- package/task-queue/tests/shutdown.test.d.ts +1 -0
- 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 +63 -15
- package/task-queue/tests/zombie-parent.test.d.ts +1 -0
- package/task-queue/tests/zombie-parent.test.js +45 -0
- package/task-queue/tests/zombie-recovery.test.d.ts +1 -0
- package/task-queue/tests/zombie-recovery.test.js +51 -0
- package/test5.js +5 -5
- package/testing/integration-setup.d.ts +4 -4
- package/testing/integration-setup.js +56 -29
- package/text/localization.service.js +2 -2
- package/utils/file-reader.js +1 -2
- package/utils/timing.d.ts +2 -2
- package/task-queue/postgres/drizzle/0000_simple_invisible_woman.sql +0 -74
- package/task-queue/tests/complex.test.js +0 -306
- package/task-queue/tests/extensive-dependencies.test.js +0 -234
- /package/{task-queue/tests/complex.test.d.ts → orm/tests/database-extension.test.d.ts} +0 -0
- /package/{task-queue/tests/extensive-dependencies.test.d.ts → orm/tests/database-migration.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,75 +9,168 @@ 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
|
-
* The task is
|
|
46
|
+
* The task is ready to be processed and is waiting for a worker.
|
|
22
47
|
*/
|
|
23
48
|
readonly Pending: "pending";
|
|
24
49
|
/**
|
|
25
|
-
* The task is currently being processed.
|
|
50
|
+
* The task is currently being processed by a worker.
|
|
26
51
|
*/
|
|
27
52
|
readonly Running: "running";
|
|
28
53
|
/**
|
|
29
|
-
* The task has been completed successfully.
|
|
54
|
+
* The task has been completed successfully and all its lifecycle requirements met.
|
|
30
55
|
*/
|
|
31
56
|
readonly Completed: "completed";
|
|
32
57
|
/**
|
|
33
|
-
* The task has been cancelled and will not be processed.
|
|
58
|
+
* The task has been cancelled and will not be processed further.
|
|
34
59
|
*/
|
|
35
60
|
readonly Cancelled: "cancelled";
|
|
36
61
|
/**
|
|
37
|
-
* The task
|
|
62
|
+
* The task has failed and will not be retried.
|
|
63
|
+
*/
|
|
64
|
+
readonly Dead: "dead";
|
|
65
|
+
/**
|
|
66
|
+
* The task is waiting for its pre-execution (schedule) dependencies to be met.
|
|
38
67
|
*/
|
|
39
68
|
readonly Waiting: "waiting";
|
|
40
69
|
/**
|
|
41
|
-
* The task has
|
|
70
|
+
* The task has finished execution but is waiting for its completion dependencies (children) to finish.
|
|
42
71
|
*/
|
|
43
|
-
readonly
|
|
72
|
+
readonly WaitingChildren: "waiting-children";
|
|
73
|
+
/**
|
|
74
|
+
* The task has been manually paused and will not be dequeued until resumed.
|
|
75
|
+
*/
|
|
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";
|
|
44
97
|
};
|
|
98
|
+
/**
|
|
99
|
+
* Type of task status.
|
|
100
|
+
*/
|
|
45
101
|
export type TaskStatus = EnumType<typeof TaskStatus>;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Represents the type of dependency between tasks.
|
|
104
|
+
*/
|
|
105
|
+
export declare const TaskDependencyType: {
|
|
106
|
+
/**
|
|
107
|
+
* The task should only be scheduled after the dependency has reached a specific status.
|
|
108
|
+
*/
|
|
109
|
+
readonly Schedule: "schedule";
|
|
110
|
+
/**
|
|
111
|
+
* The task is only considered complete after the dependency has reached a specific status.
|
|
112
|
+
*/
|
|
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";
|
|
49
118
|
};
|
|
50
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Type of task dependency.
|
|
121
|
+
*/
|
|
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
|
+
*/
|
|
51
127
|
export type Task<Definitions extends TaskDefinitionMap = Record<string, {
|
|
52
128
|
data: unknown;
|
|
53
129
|
state: unknown;
|
|
54
130
|
result: unknown;
|
|
55
131
|
}>> = {
|
|
56
132
|
[Type in TaskTypes<Definitions>]: {
|
|
133
|
+
/** The unique ID of the task. */
|
|
57
134
|
id: string;
|
|
135
|
+
/** The namespace the task belongs to. */
|
|
58
136
|
namespace: string;
|
|
137
|
+
/** The type of the task. */
|
|
59
138
|
type: Type;
|
|
139
|
+
/** The current status of the task. */
|
|
60
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
|
+
*/
|
|
61
145
|
token: string | null;
|
|
62
146
|
/**
|
|
63
147
|
* The lower the number, the higher the priority.
|
|
64
148
|
* @default 1000
|
|
65
149
|
*/
|
|
66
150
|
priority: number;
|
|
151
|
+
/** An optional key used for idempotency. */
|
|
67
152
|
idempotencyKey: string | null;
|
|
153
|
+
/** An optional trace ID for distributed tracing. */
|
|
68
154
|
traceId: string | null;
|
|
155
|
+
/** Tags associated with the task. */
|
|
69
156
|
tags: string[];
|
|
70
|
-
|
|
71
|
-
|
|
157
|
+
/** The number of unresolved schedule dependencies. */
|
|
158
|
+
unresolvedScheduleDependencies: number;
|
|
159
|
+
/** The number of unresolved completion dependencies. */
|
|
160
|
+
unresolvedCompleteDependencies: number;
|
|
161
|
+
/** Whether to skip the task if any of its dependencies fail. */
|
|
72
162
|
failFast: boolean;
|
|
73
|
-
|
|
74
|
-
dependencyTriggerStatuses: TaskStatus[];
|
|
163
|
+
/** The data associated with the task. */
|
|
75
164
|
data: TaskData<Definitions, Type>;
|
|
165
|
+
/** The ID of the parent task, if any. */
|
|
76
166
|
parentId: string | null;
|
|
167
|
+
/** The number of times the task has been attempted. */
|
|
77
168
|
tries: number;
|
|
169
|
+
/** The timestamp when the task was created. */
|
|
78
170
|
creationTimestamp: number;
|
|
171
|
+
/** The timestamp when the task's priority was last aged. */
|
|
79
172
|
priorityAgeTimestamp: number;
|
|
173
|
+
/** The timestamp when the task is scheduled to be processed. */
|
|
80
174
|
scheduleTimestamp: number;
|
|
81
175
|
/**
|
|
82
176
|
* Timestamp when the task most recently switched to Running state.
|
|
@@ -92,43 +186,95 @@ export type Task<Definitions extends TaskDefinitionMap = Record<string, {
|
|
|
92
186
|
* Token expiration (Soft Timeout).
|
|
93
187
|
*/
|
|
94
188
|
visibilityDeadline: number | null;
|
|
189
|
+
/** The timestamp when the task was completed. */
|
|
95
190
|
completeTimestamp: number | null;
|
|
96
191
|
/** A number between 0 and 1 indicating the progress of the task. */
|
|
97
192
|
progress: number;
|
|
98
193
|
/** A snapshot of the current state of the task. */
|
|
99
194
|
state: TaskState<Definitions, Type> | null;
|
|
195
|
+
/** The result of the task, if successful. */
|
|
100
196
|
result: TaskResult<Definitions, Type> | null;
|
|
197
|
+
/** The error that occurred, if the task failed. */
|
|
101
198
|
error: UndefinableJson | null;
|
|
102
199
|
};
|
|
103
200
|
}[Extract<keyof Definitions, string>];
|
|
201
|
+
/** Default priority for tasks. */
|
|
104
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
|
+
*/
|
|
105
221
|
export type EnqueueOptions = {
|
|
222
|
+
/** The priority of the task. */
|
|
106
223
|
priority?: number;
|
|
224
|
+
/** The ID of the parent task. */
|
|
107
225
|
parentId?: string;
|
|
226
|
+
/** A unique key for idempotency. */
|
|
108
227
|
idempotencyKey?: string;
|
|
228
|
+
/** Whether to replace an existing task with the same idempotency key. */
|
|
109
229
|
replace?: boolean;
|
|
230
|
+
/** Tags to associate with the task. */
|
|
110
231
|
tags?: string[];
|
|
111
|
-
|
|
112
|
-
|
|
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. */
|
|
113
243
|
failFast?: boolean;
|
|
114
|
-
|
|
115
|
-
|
|
244
|
+
/** Whether the parent task should wait for this task to complete. */
|
|
245
|
+
waitForCompletion?: boolean;
|
|
246
|
+
/** The timestamp when the task should be processed. */
|
|
116
247
|
scheduleTimestamp?: number;
|
|
248
|
+
/** The duration (ms) before the task is considered expired. */
|
|
117
249
|
timeToLive?: number;
|
|
250
|
+
/** The transaction to use for the operation. */
|
|
118
251
|
transaction?: Transaction;
|
|
119
252
|
};
|
|
253
|
+
/** Options for enqueuing a single task. */
|
|
120
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
|
+
*/
|
|
121
260
|
export type EnqueueManyItem<Definitions extends TaskDefinitionMap = TaskDefinitionMap, Type extends TaskTypes<Definitions> = TaskTypes<Definitions>> = {
|
|
122
261
|
[Type in TaskTypes<Definitions>]: {
|
|
262
|
+
/** The type of the task. */
|
|
123
263
|
type: Type;
|
|
264
|
+
/** The data for the task. */
|
|
124
265
|
data: TaskData<Definitions, Type>;
|
|
125
266
|
} & EnqueueOptions;
|
|
126
267
|
}[Type];
|
|
268
|
+
/** Options for enqueuing multiple tasks in a batch. */
|
|
127
269
|
export type EnqueueManyOptions = {
|
|
270
|
+
/** Whether to replace existing tasks with the same idempotency keys. */
|
|
128
271
|
replace?: boolean;
|
|
272
|
+
/** Whether to return the enqueued tasks. */
|
|
129
273
|
returnTasks?: boolean;
|
|
274
|
+
/** The transaction to use for the operation. */
|
|
130
275
|
transaction?: Transaction;
|
|
131
276
|
};
|
|
277
|
+
/** Configuration for a task queue. */
|
|
132
278
|
export type QueueConfig = {
|
|
133
279
|
/**
|
|
134
280
|
* Duration (ms) before a worker token is considered lost.
|
|
@@ -145,8 +291,6 @@ export type QueueConfig = {
|
|
|
145
291
|
maxTries?: number;
|
|
146
292
|
/** Duration to retain terminal tasks before archival. Default: 30 days */
|
|
147
293
|
retention?: number;
|
|
148
|
-
/** Maximum number of running tasks across all workers (`null` = unlimited) */
|
|
149
|
-
globalConcurrency?: number | null;
|
|
150
294
|
/** Number of consecutive failures before tripping the circuit breaker */
|
|
151
295
|
circuitBreakerThreshold?: number;
|
|
152
296
|
/** Milliseconds to wait before trying to close the circuit breaker */
|
|
@@ -172,80 +316,191 @@ export type QueueConfig = {
|
|
|
172
316
|
/** Time to retain idempotency keys (ms). Default: 1 hour */
|
|
173
317
|
idempotencyWindow?: number;
|
|
174
318
|
};
|
|
319
|
+
/** Argument for injecting a task queue. Can be a namespace string or a full configuration. */
|
|
175
320
|
export type TaskQueueArgument = string | (QueueConfig & {
|
|
176
321
|
namespace: string;
|
|
177
322
|
});
|
|
323
|
+
/** Options for waiting for tasks to complete. */
|
|
178
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. */
|
|
179
328
|
interval?: number;
|
|
329
|
+
/** The maximum duration (ms) to wait. */
|
|
180
330
|
timeout?: number;
|
|
331
|
+
/** A signal used to cancel the wait operation. */
|
|
181
332
|
cancellationSignal?: CancellationSignal;
|
|
182
333
|
};
|
|
334
|
+
/** The result of waiting for tasks. */
|
|
183
335
|
export type TaskQueueWaitResult = {
|
|
336
|
+
/** Whether the wait was cancelled. */
|
|
184
337
|
cancelled: boolean;
|
|
185
338
|
};
|
|
186
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
|
+
*/
|
|
187
344
|
export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap = TaskDefinitionMap> extends Transactional<QueueConfig & {
|
|
188
345
|
namespace: string;
|
|
189
346
|
}> implements Resolvable<TaskQueueArgument> {
|
|
190
|
-
|
|
347
|
+
#private;
|
|
191
348
|
protected readonly injector: Injector;
|
|
192
349
|
protected readonly config: QueueConfig & {
|
|
193
350
|
namespace: string;
|
|
194
351
|
};
|
|
195
352
|
protected readonly logger: Logger;
|
|
353
|
+
get namespace(): string;
|
|
354
|
+
/** Duration (ms) before a worker token is considered lost. */
|
|
196
355
|
abstract readonly visibilityTimeout: number;
|
|
356
|
+
/** Maximum number of attempts allowed for a task. */
|
|
197
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
|
+
*/
|
|
198
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
|
+
*/
|
|
199
371
|
abstract enqueue<Type extends TaskTypes<Definitions>>(type: Type, data: TaskData<Definitions, Type>, options?: EnqueueOneOptions): Promise<TaskOfType<Definitions, Type>>;
|
|
372
|
+
/**
|
|
373
|
+
* Enqueues multiple tasks in a batch.
|
|
374
|
+
* @param items The items to enqueue.
|
|
375
|
+
* @param options Enqueue options.
|
|
376
|
+
*/
|
|
200
377
|
abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options?: EnqueueManyOptions & {
|
|
201
378
|
returnTasks?: false;
|
|
202
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
|
+
*/
|
|
203
386
|
abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options: EnqueueManyOptions & {
|
|
204
387
|
returnTasks: true;
|
|
205
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
|
+
*/
|
|
206
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
|
+
*/
|
|
207
402
|
abstract has(id: string, options?: {
|
|
208
403
|
transaction?: Transaction;
|
|
209
404
|
}): Promise<boolean>;
|
|
210
|
-
|
|
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;
|
|
211
412
|
transaction?: Transaction;
|
|
212
|
-
}): Promise<
|
|
213
|
-
|
|
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?: {
|
|
421
|
+
transaction?: Transaction;
|
|
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?: {
|
|
214
430
|
transaction?: Transaction;
|
|
215
|
-
}): 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
|
+
*/
|
|
216
438
|
abstract countByTags(tags: OneOrMany<string>, options?: {
|
|
217
439
|
transaction?: Transaction;
|
|
218
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
|
+
*/
|
|
219
447
|
abstract getTree(rootId: string | string[], options?: {
|
|
220
448
|
transaction?: Transaction;
|
|
221
449
|
}): Promise<Task[]>;
|
|
222
450
|
/**
|
|
223
|
-
* 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.
|
|
224
452
|
* @param ids The IDs of the tasks to wait for.
|
|
225
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.
|
|
226
461
|
*/
|
|
227
|
-
abstract waitForTasks(ids: string[], options?: {
|
|
228
|
-
timeout?: number;
|
|
229
|
-
cancellationSignal?: CancellationSignal;
|
|
230
|
-
}): Promise<TaskQueueWaitResult>;
|
|
231
462
|
abstract cancel(id: string, options?: {
|
|
232
463
|
transaction?: Transaction;
|
|
233
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
|
+
*/
|
|
234
470
|
abstract cancelMany(ids: string[], options?: {
|
|
235
471
|
transaction?: Transaction;
|
|
236
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
|
+
*/
|
|
237
478
|
abstract cancelManyByTags(tags: OneOrMany<string>, options?: {
|
|
238
479
|
transaction?: Transaction;
|
|
239
480
|
}): Promise<void>;
|
|
240
|
-
/**
|
|
481
|
+
/**
|
|
482
|
+
* Clears all tasks from the queue. Use with caution!
|
|
483
|
+
* @param options Optional transaction.
|
|
484
|
+
*/
|
|
241
485
|
abstract clear(options?: {
|
|
242
486
|
transaction?: Transaction;
|
|
243
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
|
+
*/
|
|
244
493
|
abstract dequeue<Type extends TaskTypes<Definitions>>(options?: {
|
|
245
494
|
forceDequeue?: boolean;
|
|
246
495
|
types?: Type[];
|
|
247
496
|
transaction?: Transaction;
|
|
248
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
|
+
*/
|
|
249
504
|
abstract dequeueMany<Type extends TaskTypes<Definitions>>(count: number, options?: {
|
|
250
505
|
forceDequeue?: boolean;
|
|
251
506
|
types?: Type[];
|
|
@@ -253,20 +508,36 @@ export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap =
|
|
|
253
508
|
}): Promise<TaskOfType<Definitions, Type>[]>;
|
|
254
509
|
/**
|
|
255
510
|
* Reschedules a task to run at a specific time.
|
|
256
|
-
*
|
|
511
|
+
* @param id The ID of the task.
|
|
512
|
+
* @param timestamp The timestamp when the task should run.
|
|
513
|
+
* @param options Optional transaction.
|
|
257
514
|
*/
|
|
258
515
|
abstract reschedule(id: string, timestamp: number, options?: {
|
|
259
516
|
transaction?: Transaction;
|
|
260
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
|
+
*/
|
|
261
524
|
abstract rescheduleMany(ids: string[], timestamp: number, options?: {
|
|
262
525
|
transaction?: Transaction;
|
|
263
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
|
+
*/
|
|
264
533
|
abstract rescheduleManyByTags(tags: OneOrMany<string>, timestamp: number, options?: {
|
|
265
534
|
transaction?: Transaction;
|
|
266
535
|
}): Promise<void>;
|
|
267
536
|
/**
|
|
268
|
-
* Updates task progress
|
|
269
|
-
*
|
|
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.
|
|
270
541
|
*/
|
|
271
542
|
abstract touch<Type extends TaskTypes<Definitions>>(task: TaskOfType<Definitions, Type>, options?: {
|
|
272
543
|
progress?: number;
|
|
@@ -274,53 +545,100 @@ export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap =
|
|
|
274
545
|
transaction?: Transaction;
|
|
275
546
|
}): Promise<TaskOfType<Definitions, Type> | undefined>;
|
|
276
547
|
/**
|
|
277
|
-
* Updates multiple tasks' progress
|
|
278
|
-
*
|
|
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.
|
|
279
552
|
*/
|
|
280
553
|
abstract touchMany<Tasks extends Task<Definitions>[]>(tasks: Tasks, options?: {
|
|
281
554
|
progresses?: number[];
|
|
282
555
|
states?: TasksStates<Tasks>;
|
|
283
556
|
transaction?: Transaction;
|
|
284
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
|
+
*/
|
|
285
563
|
abstract complete<Type extends TaskTypes<Definitions>>(task: TaskOfType<Definitions, Type>, options?: {
|
|
286
564
|
result?: TaskResult<Definitions, Type>;
|
|
287
565
|
transaction?: Transaction;
|
|
288
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
|
+
*/
|
|
289
572
|
abstract completeMany<Tasks extends Task<Definitions>[]>(tasks: Tasks, options?: {
|
|
290
573
|
results?: TasksResults<Tasks>;
|
|
291
574
|
transaction?: Transaction;
|
|
292
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
|
+
*/
|
|
293
582
|
abstract fail(task: Task<Definitions>, error: unknown, options?: {
|
|
294
583
|
fatal?: boolean;
|
|
295
584
|
transaction?: Transaction;
|
|
296
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
|
+
*/
|
|
297
592
|
abstract failMany(tasks: Task<Definitions>[], errors: unknown[], options?: {
|
|
298
593
|
transaction?: Transaction;
|
|
299
594
|
}): Promise<void>;
|
|
595
|
+
/**
|
|
596
|
+
* Performs maintenance tasks on the queue (e.g., handling timeouts, aging priorities, archival).
|
|
597
|
+
* @param options Optional transaction.
|
|
598
|
+
*/
|
|
300
599
|
abstract maintenance(options?: {
|
|
301
600
|
transaction?: Transaction;
|
|
302
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
|
+
*/
|
|
303
607
|
abstract restart(id: string, options?: {
|
|
304
608
|
resetState?: boolean;
|
|
305
609
|
transaction?: Transaction;
|
|
306
610
|
}): Promise<void>;
|
|
307
611
|
/**
|
|
308
612
|
* Notifies the queue that new tasks might be available for processing.
|
|
309
|
-
*
|
|
310
|
-
* Useful for unit tests.
|
|
613
|
+
* @param namespace The namespace to notify. Defaults to the current namespace.
|
|
311
614
|
* @internal
|
|
312
615
|
*/
|
|
313
|
-
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
|
+
*/
|
|
314
623
|
abstract getConsumer<Type extends TaskTypes<Definitions>>(cancellationSignal: CancellationSignal, options?: {
|
|
315
624
|
forceDequeue?: boolean;
|
|
316
625
|
types?: Type[];
|
|
317
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
|
+
*/
|
|
318
634
|
abstract getBatchConsumer<Type extends TaskTypes<Definitions>>(size: number, cancellationSignal: CancellationSignal, options?: {
|
|
319
635
|
forceDequeue?: boolean;
|
|
320
636
|
types?: Type[];
|
|
321
637
|
}): AsyncIterableIterator<TaskOfType<Definitions, Type>[]>;
|
|
322
638
|
/**
|
|
323
|
-
* 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.
|
|
324
642
|
*/
|
|
325
643
|
process<Type extends TaskTypes<Definitions>>({ concurrency, cancellationSignal, types, forceDequeue }: {
|
|
326
644
|
concurrency?: number;
|
|
@@ -332,7 +650,10 @@ export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap =
|
|
|
332
650
|
namespace: string;
|
|
333
651
|
};
|
|
334
652
|
/**
|
|
335
|
-
*
|
|
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.
|
|
336
657
|
*/
|
|
337
658
|
processWorker<Type extends TaskTypes<Definitions>>(cancellationSignal: CancellationSignal, handler: ProcessWorker<Definitions, Type>, options?: {
|
|
338
659
|
types?: Type[];
|