@tstdl/base 0.93.91 → 0.93.93

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.
Files changed (42) hide show
  1. package/authentication/client/authentication.service.js +8 -8
  2. package/document-management/server/services/document-validation.service.js +5 -5
  3. package/document-management/server/services/document-workflow.service.js +2 -2
  4. package/orm/sqls/sqls.d.ts +6 -6
  5. package/package.json +2 -2
  6. package/task-queue/enqueue-batch.d.ts +16 -11
  7. package/task-queue/enqueue-batch.js +2 -2
  8. package/task-queue/index.d.ts +2 -1
  9. package/task-queue/index.js +2 -1
  10. package/task-queue/postgres/drizzle/{0000_thin_black_panther.sql → 0000_simple_invisible_woman.sql} +5 -5
  11. package/task-queue/postgres/drizzle/meta/0000_snapshot.json +11 -11
  12. package/task-queue/postgres/drizzle/meta/_journal.json +2 -2
  13. package/task-queue/postgres/module.js +2 -2
  14. package/task-queue/postgres/schemas.d.ts +1 -1
  15. package/task-queue/postgres/schemas.js +2 -2
  16. package/task-queue/postgres/task-queue.d.ts +101 -47
  17. package/task-queue/postgres/task-queue.js +149 -139
  18. package/task-queue/postgres/task-queue.provider.d.ts +3 -4
  19. package/task-queue/postgres/task-queue.provider.js +2 -2
  20. package/task-queue/postgres/task.model.d.ts +5 -5
  21. package/task-queue/postgres/task.model.js +5 -5
  22. package/task-queue/provider.d.ts +2 -2
  23. package/task-queue/task-context.d.ts +34 -18
  24. package/task-queue/task-context.js +23 -13
  25. package/task-queue/task-queue.d.ts +160 -132
  26. package/task-queue/task-queue.js +8 -8
  27. package/task-queue/tests/complex.test.js +36 -29
  28. package/task-queue/tests/dependencies.test.js +17 -17
  29. package/task-queue/tests/enqueue-item.test.d.ts +1 -0
  30. package/task-queue/tests/enqueue-item.test.js +12 -0
  31. package/task-queue/tests/queue-generic.test.d.ts +1 -0
  32. package/task-queue/tests/queue-generic.test.js +8 -0
  33. package/task-queue/tests/queue.test.js +50 -50
  34. package/task-queue/tests/task-context.test.d.ts +1 -0
  35. package/task-queue/tests/task-context.test.js +7 -0
  36. package/task-queue/tests/task-union.test.d.ts +1 -0
  37. package/task-queue/tests/task-union.test.js +18 -0
  38. package/task-queue/tests/typing.test.d.ts +1 -0
  39. package/task-queue/tests/typing.test.js +9 -0
  40. package/task-queue/tests/worker.test.js +16 -16
  41. package/task-queue/types.d.ts +48 -0
  42. package/task-queue/types.js +1 -0
@@ -1,7 +1,6 @@
1
- import { TaskQueueProvider, type QueueConfig } from '../../task-queue/index.js';
2
- import type { ObjectLiteral } from '../../types/index.js';
3
- import { PostgresQueue } from './task-queue.js';
1
+ import { TaskQueueProvider, type QueueConfig, type TaskDefinitionMap } from '../../task-queue/index.js';
2
+ import { PostgresTaskQueue } from './task-queue.js';
4
3
  export declare class PostgresTaskQueueProvider extends TaskQueueProvider {
5
4
  #private;
6
- get<Data extends ObjectLiteral, State extends ObjectLiteral, Result extends ObjectLiteral>(namespace: string, config?: QueueConfig): PostgresQueue<Data, State, Result>;
5
+ get<Definitions extends TaskDefinitionMap = TaskDefinitionMap>(namespace: string, config?: QueueConfig): PostgresTaskQueue<Definitions>;
7
6
  }
@@ -8,11 +8,11 @@ import { Singleton } from '../../injector/decorators.js';
8
8
  import { inject } from '../../injector/inject.js';
9
9
  import { Injector } from '../../injector/injector.js';
10
10
  import { TaskQueueProvider } from '../../task-queue/index.js';
11
- import { PostgresQueue } from './task-queue.js';
11
+ import { PostgresTaskQueue } from './task-queue.js';
12
12
  let PostgresTaskQueueProvider = class PostgresTaskQueueProvider extends TaskQueueProvider {
13
13
  #injector = inject(Injector);
14
14
  get(namespace, config) {
15
- return this.#injector.resolve((PostgresQueue), { ...config, namespace });
15
+ return this.#injector.resolve((PostgresTaskQueue), { ...config, namespace });
16
16
  }
17
17
  };
18
18
  PostgresTaskQueueProvider = __decorate([
@@ -1,10 +1,10 @@
1
1
  import { BaseEntity, type Json, type Timestamp } from '../../orm/index.js';
2
2
  import type { ObjectLiteral, TypedOmit } from '../../types/types.js';
3
- import { DependencyJoinMode, type Task, TaskState } from '../task-queue.js';
4
- export declare abstract class PostgresTaskBase<Data extends ObjectLiteral = ObjectLiteral, State extends ObjectLiteral = ObjectLiteral, Result extends ObjectLiteral = ObjectLiteral> extends BaseEntity implements TypedOmit<Task<Data, State, Result>, 'parentId'> {
3
+ import { DependencyJoinMode, type Task, TaskStatus } from '../task-queue.js';
4
+ export declare abstract class PostgresTaskBase<Data extends ObjectLiteral = ObjectLiteral, State extends ObjectLiteral = ObjectLiteral, Result extends ObjectLiteral = ObjectLiteral> extends BaseEntity implements TypedOmit<Task, 'parentId'> {
5
5
  namespace: string;
6
6
  type: string;
7
- status: TaskState;
7
+ status: TaskStatus;
8
8
  idempotencyKey: string | null;
9
9
  traceId: string | null;
10
10
  tags: string[];
@@ -12,7 +12,7 @@ export declare abstract class PostgresTaskBase<Data extends ObjectLiteral = Obje
12
12
  scheduleAfterTags: string[];
13
13
  failFast: boolean;
14
14
  dependencyJoinMode: DependencyJoinMode;
15
- dependencyTriggerStates: TaskState[];
15
+ dependencyTriggerStatuses: TaskStatus[];
16
16
  priority: number;
17
17
  token: string | null;
18
18
  creationTimestamp: Timestamp;
@@ -29,7 +29,7 @@ export declare abstract class PostgresTaskBase<Data extends ObjectLiteral = Obje
29
29
  result: Json<Result> | null;
30
30
  error: Json<ObjectLiteral> | null;
31
31
  }
32
- export declare class PostgresTask<Data extends ObjectLiteral = ObjectLiteral, State extends ObjectLiteral = ObjectLiteral, Result extends ObjectLiteral = ObjectLiteral> extends PostgresTaskBase<Data, State, Result> implements Task<Data, State, Result> {
32
+ export declare class PostgresTask<Data extends ObjectLiteral = ObjectLiteral, State extends ObjectLiteral = ObjectLiteral, Result extends ObjectLiteral = ObjectLiteral> extends PostgresTaskBase<Data, State, Result> implements Task {
33
33
  static readonly entityName = "Task";
34
34
  parentId: string | null;
35
35
  }
@@ -9,7 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  };
10
10
  import { BaseEntity, Index, JsonProperty, Reference, Table, TimestampProperty, Unique, UuidProperty } from '../../orm/index.js';
11
11
  import { Array as ArrayProperty, BooleanProperty, Enumeration, Integer, NumberProperty, StringProperty } from '../../schema/index.js';
12
- import { DependencyJoinMode, TaskState } from '../task-queue.js';
12
+ import { DependencyJoinMode, TaskStatus } from '../task-queue.js';
13
13
  export class PostgresTaskBase extends BaseEntity {
14
14
  namespace;
15
15
  type;
@@ -21,7 +21,7 @@ export class PostgresTaskBase extends BaseEntity {
21
21
  scheduleAfterTags;
22
22
  failFast;
23
23
  dependencyJoinMode;
24
- dependencyTriggerStates;
24
+ dependencyTriggerStatuses;
25
25
  priority;
26
26
  token;
27
27
  creationTimestamp;
@@ -47,7 +47,7 @@ __decorate([
47
47
  __metadata("design:type", String)
48
48
  ], PostgresTaskBase.prototype, "type", void 0);
49
49
  __decorate([
50
- Enumeration(TaskState),
50
+ Enumeration(TaskStatus),
51
51
  __metadata("design:type", String)
52
52
  ], PostgresTaskBase.prototype, "status", void 0);
53
53
  __decorate([
@@ -79,9 +79,9 @@ __decorate([
79
79
  __metadata("design:type", String)
80
80
  ], PostgresTaskBase.prototype, "dependencyJoinMode", void 0);
81
81
  __decorate([
82
- Enumeration(TaskState, { array: true }),
82
+ Enumeration(TaskStatus, { array: true }),
83
83
  __metadata("design:type", Array)
84
- ], PostgresTaskBase.prototype, "dependencyTriggerStates", void 0);
84
+ ], PostgresTaskBase.prototype, "dependencyTriggerStatuses", void 0);
85
85
  __decorate([
86
86
  Integer(),
87
87
  __metadata("design:type", Number)
@@ -1,5 +1,5 @@
1
- import type { ObjectLiteral } from '../types/index.js';
2
1
  import type { TaskQueue, QueueConfig } from './task-queue.js';
2
+ import type { TaskDefinitionMap } from './types.js';
3
3
  export declare abstract class TaskQueueProvider {
4
- abstract get<Data extends ObjectLiteral, State extends ObjectLiteral, Result extends ObjectLiteral>(key: string, config?: QueueConfig): TaskQueue<Data, State, Result>;
4
+ abstract get<Definitions extends TaskDefinitionMap = TaskDefinitionMap>(key: string, config?: QueueConfig): TaskQueue<Definitions>;
5
5
  }
@@ -1,38 +1,54 @@
1
1
  import type { CancellationSignal, CancellationToken } from '../cancellation/index.js';
2
2
  import type { Logger } from '../logger/index.js';
3
3
  import type { Transaction } from '../orm/server/index.js';
4
- import { type EnqueueManyItem, type EnqueueOptions, TaskQueue, type Task } from './task-queue.js';
5
- export declare class TaskContext<Data, State = unknown, Result = unknown> {
4
+ import { TaskQueue, type EnqueueManyItem, type EnqueueOptions } from './task-queue.js';
5
+ import type { TaskData, TaskDefinitionMap, TaskOfType, TaskResult, TaskState, TaskTypes, TasksStates } from './types.js';
6
+ export declare class TaskContext<Definitions extends TaskDefinitionMap, Type extends TaskTypes<Definitions>> {
6
7
  #private;
7
- constructor(queue: TaskQueue<Data, State, Result>, task: Task<Data, State, Result>, signal: CancellationToken, logger: Logger);
8
+ constructor(queue: TaskQueue<Definitions>, task: TaskOfType<Definitions, Type>, signal: CancellationToken, logger: Logger);
8
9
  get id(): string;
9
- get data(): Data;
10
+ get type(): Type;
11
+ get data(): TaskData<Definitions, Type>;
12
+ get state(): TaskState<Definitions, Type> | null;
10
13
  get attempt(): number;
11
14
  get triesLeft(): number;
12
15
  get signal(): CancellationSignal;
13
16
  get logger(): Logger;
14
- complete(result?: Result, transaction?: Transaction): Promise<void>;
17
+ complete(result?: TaskResult<Definitions, Type>, options?: {
18
+ transaction?: Transaction;
19
+ }): Promise<void>;
15
20
  checkpoint(options: {
16
21
  progress?: number;
17
- state?: State;
22
+ state?: TaskState<Definitions, Type>;
23
+ transaction?: Transaction;
18
24
  }): Promise<void>;
19
- spawn(type: string, data: Data, options?: Omit<EnqueueOptions, 'parentId'>): Promise<Task<Data, State, Result>>;
20
- spawn<D, S, R>(queue: TaskQueue<D, S, R>, type: string, data: D, options?: Omit<EnqueueOptions, 'parentId'>): Promise<Task<D, S, R>>;
21
- spawnMany(items: EnqueueManyItem<Data>[]): Promise<Task<Data, State, Result>[]>;
22
- spawnMany<D = Data, S = any, R = any>(queue: TaskQueue<D, S, R>, items: EnqueueManyItem<D>[]): Promise<Task<D, S, R>[]>;
25
+ spawn<Type extends TaskTypes<Definitions>>(type: Type, data: TaskData<Definitions, Type>, options?: Omit<EnqueueOptions, 'parentId'>): Promise<TaskOfType<Definitions, Type>>;
26
+ spawn<OtherDefinitions extends TaskDefinitionMap, T extends TaskTypes<OtherDefinitions>>(queue: TaskQueue<OtherDefinitions>, type: T, data: OtherDefinitions[T]['data'], options?: Omit<EnqueueOptions, 'parentId'>): Promise<TaskOfType<OtherDefinitions, T>>;
27
+ spawnMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[]): Promise<TaskOfType<Definitions, Type>[]>;
28
+ spawnMany<OtherDefinitions extends TaskDefinitionMap, Type extends TaskTypes<OtherDefinitions>>(queue: TaskQueue<OtherDefinitions>, items: EnqueueManyItem<OtherDefinitions, Type>[]): Promise<TaskOfType<OtherDefinitions, Type>[]>;
23
29
  /** Stop execution and reschedule the task for later without incrementing tries if possible */
24
- reschedule(timestamp: number): Promise<void>;
25
- reschedule(options: {
30
+ reschedule(timestamp: number, options?: {
31
+ transaction?: Transaction;
32
+ }): Promise<void>;
33
+ reschedule(rescheduleOptions: {
26
34
  delay: number;
35
+ transaction?: Transaction;
36
+ }): Promise<void>;
37
+ fail(error: unknown, options?: {
38
+ fatal?: boolean;
39
+ transaction?: Transaction;
27
40
  }): Promise<void>;
28
- fail(error: any, fatal?: boolean, transaction?: Transaction): Promise<void>;
29
41
  }
30
- export declare class BatchTaskContext<Data, State, Result> {
42
+ export declare class BatchTaskContext<Definitions extends TaskDefinitionMap, Type extends TaskTypes<Definitions>> {
31
43
  #private;
32
- constructor(queue: TaskQueue<Data, State, Result>, tasks: Task<Data, State, Result>[], signal: CancellationToken, logger: Logger);
33
- get tasks(): Task<Data, State, Result>[];
44
+ constructor(queue: TaskQueue<Definitions>, tasks: TaskOfType<Definitions, Type>[], signal: CancellationToken, logger: Logger);
45
+ get tasks(): TaskOfType<Definitions, Type>[];
34
46
  get signal(): CancellationSignal;
35
47
  get logger(): Logger;
36
- for(task: Task<Data, State, Result>): TaskContext<Data, State, Result>;
37
- checkpointAll(progresses?: number[], states?: State[]): Promise<void>;
48
+ for<Type extends TaskTypes<Definitions>>(task: TaskOfType<Definitions, Type>): TaskContext<Definitions, Type>;
49
+ checkpointAll(options?: {
50
+ progresses?: number[];
51
+ states?: TasksStates<TaskOfType<Definitions, Type>[]>;
52
+ transaction?: Transaction;
53
+ }): Promise<void>;
38
54
  }
@@ -15,9 +15,15 @@ export class TaskContext {
15
15
  get id() {
16
16
  return this.#task.id;
17
17
  }
18
+ get type() {
19
+ return this.#task.type;
20
+ }
18
21
  get data() {
19
22
  return this.#task.data;
20
23
  }
24
+ get state() {
25
+ return this.#task.state;
26
+ }
21
27
  get attempt() {
22
28
  return this.#task.tries;
23
29
  }
@@ -30,8 +36,8 @@ export class TaskContext {
30
36
  get logger() {
31
37
  return this.#logger;
32
38
  }
33
- async complete(result, transaction) {
34
- await this.#queue.complete(this.#task, result, transaction);
39
+ async complete(result, options) {
40
+ await this.#queue.complete(this.#task, { result, ...options });
35
41
  }
36
42
  async checkpoint(options) {
37
43
  const updatedTask = await this.#queue.touch(this.#task, options);
@@ -44,10 +50,14 @@ export class TaskContext {
44
50
  }
45
51
  }
46
52
  async spawn(queueOrType, typeOrData, dataOrOptionsOrNothing, optionsOrNothing) {
47
- if (isInstanceOf(queueOrType, TaskQueue)) {
48
- return await queueOrType.enqueue(typeOrData, dataOrOptionsOrNothing, { ...optionsOrNothing, parentId: this.#task.id });
53
+ const isForOtherQueue = isInstanceOf(queueOrType, TaskQueue);
54
+ const type = (isForOtherQueue ? typeOrData : queueOrType);
55
+ const data = isForOtherQueue ? dataOrOptionsOrNothing : typeOrData;
56
+ const options = (isForOtherQueue ? optionsOrNothing : dataOrOptionsOrNothing);
57
+ if (isForOtherQueue) {
58
+ return await queueOrType.enqueue(type, data, { ...options, parentId: this.#task.id });
49
59
  }
50
- return await this.#queue.enqueue(queueOrType, typeOrData, { ...dataOrOptionsOrNothing, parentId: this.#task.id });
60
+ return await this.#queue.enqueue(type, data, { ...options, parentId: this.#task.id });
51
61
  }
52
62
  async spawnMany(queueOrItems, itemsOrNothing) {
53
63
  const isForOtherQueue = isInstanceOf(queueOrItems, TaskQueue);
@@ -58,15 +68,15 @@ export class TaskContext {
58
68
  return await this.#queue.enqueueMany(items, { returnTasks: true });
59
69
  }
60
70
  async reschedule(timestampOrDelay) {
61
- const timestamp = isNumber(timestampOrDelay)
62
- ? timestampOrDelay
63
- : currentTimestamp() + timestampOrDelay.delay;
71
+ const isTimestamp = isNumber(timestampOrDelay);
72
+ const timestamp = isTimestamp ? timestampOrDelay : (currentTimestamp() + timestampOrDelay.delay);
73
+ const transaction = isTimestamp ? undefined : timestampOrDelay.transaction;
64
74
  this.#logger.debug(`Rescheduling task for ${new Date(timestamp).toISOString()}.`);
65
- await this.#queue.reschedule(this.#task.id, timestamp);
75
+ await this.#queue.reschedule(this.#task.id, timestamp, { transaction });
66
76
  this.#signal.set();
67
77
  }
68
- async fail(error, fatal, transaction) {
69
- await this.#queue.fail(this.#task, error, fatal, transaction);
78
+ async fail(error, options) {
79
+ await this.#queue.fail(this.#task, error, options);
70
80
  this.#signal.set();
71
81
  }
72
82
  }
@@ -93,8 +103,8 @@ export class BatchTaskContext {
93
103
  for(task) {
94
104
  return new TaskContext(this.#queue, task, this.#signal, this.#logger);
95
105
  }
96
- async checkpointAll(progresses, states) {
97
- const validIds = await this.#queue.touchMany(this.#tasks, progresses, states);
106
+ async checkpointAll(options) {
107
+ const validIds = await this.#queue.touchMany(this.#tasks, options);
98
108
  if (validIds.length < this.#tasks.length) {
99
109
  this.#logger.warn(`${this.#tasks.length - validIds.length} tasks in batch lost their lease during checkpoint`);
100
110
  }
@@ -4,43 +4,18 @@ import type { Resolvable, resolveArgumentType } from '../injector/interfaces.js'
4
4
  import { Logger } from '../logger/logger.js';
5
5
  import type { Transaction } from '../orm/server/transaction.js';
6
6
  import { Transactional } from '../orm/server/transactional.js';
7
- import type { OneOrMany, UndefinableJson } from '../types/types.js';
7
+ import type { OneOrMany, Record, UndefinableJson } from '../types/types.js';
8
8
  import { TaskQueueEnqueueBatch } from './enqueue-batch.js';
9
- import { BatchTaskContext, type TaskContext } from './task-context.js';
10
- type TaskResultPayload<Result> = {
11
- action: 'complete';
12
- result: Result | undefined;
13
- } | {
14
- action: 'fail';
15
- error: any;
16
- fatal: boolean;
17
- } | {
18
- action: 'reschedule';
19
- timestamp: number;
20
- };
21
- export declare class TaskResult<Result = unknown> {
22
- readonly payload: TaskResultPayload<Result>;
9
+ import type { ProcessBatchWorker, ProcessWorker, TaskData, TaskDefinitionMap, TaskOfType, TaskProcessResultPayload, TaskResult, TaskState, TaskTypes, TasksResults, TasksStates } from './types.js';
10
+ export declare class TaskProcessResult<Result = unknown> {
11
+ readonly payload: TaskProcessResultPayload<Result>;
23
12
  private constructor();
24
- static Complete<Result>(result?: Result): TaskResult<Result>;
25
- static Fail(error: any, fatal?: boolean): TaskResult;
26
- static RescheduleTo(timestamp: number): TaskResult;
27
- static RescheduleBy(milliseconds: number): TaskResult;
28
- }
29
- export interface ProcessWorker<Data, State, Result> {
30
- /**
31
- * A worker function that processes a single task.
32
- * @param context The task context providing data, logger, and orchestration helpers.
33
- */
34
- (context: TaskContext<Data, State, Result>): TaskResult<Result> | Promise<TaskResult<Result>>;
35
- }
36
- export interface ProcessBatchWorker<Data, State, Result> {
37
- /**
38
- * A worker function that processes a batch of tasks.
39
- * @param context The batch context providing tasks and helpers.
40
- */
41
- (context: BatchTaskContext<Data, State, Result>): TaskResult<Result>[] | Promise<TaskResult<Result>[]>;
13
+ static Complete<Result>(result?: Result): TaskProcessResult<Result>;
14
+ static Fail(error: unknown, fatal?: boolean): TaskProcessResult;
15
+ static RescheduleTo(timestamp: number): TaskProcessResult;
16
+ static RescheduleBy(milliseconds: number): TaskProcessResult;
42
17
  }
43
- export declare const TaskState: {
18
+ export declare const TaskStatus: {
44
19
  /**
45
20
  * The task is waiting to be processed.
46
21
  */
@@ -66,59 +41,65 @@ export declare const TaskState: {
66
41
  */
67
42
  readonly Dead: "dead";
68
43
  };
69
- export type TaskState = EnumType<typeof TaskState>;
44
+ export type TaskStatus = EnumType<typeof TaskStatus>;
70
45
  export declare const DependencyJoinMode: {
71
46
  readonly And: "and";
72
47
  readonly Or: "or";
73
48
  };
74
49
  export type DependencyJoinMode = EnumType<typeof DependencyJoinMode>;
75
- export type Task<Data = unknown, State = unknown, Result = unknown> = {
76
- id: string;
77
- namespace: string;
78
- type: string;
79
- status: TaskState;
80
- token: string | null;
81
- /**
82
- * The lower the number, the higher the priority.
83
- * @default 1000
84
- */
85
- priority: number;
86
- idempotencyKey: string | null;
87
- traceId: string | null;
88
- tags: string[];
89
- completeAfterTags: string[];
90
- scheduleAfterTags: string[];
91
- failFast: boolean;
92
- dependencyJoinMode: DependencyJoinMode;
93
- dependencyTriggerStates: TaskState[];
94
- data: Data | null;
95
- parentId: string | null;
96
- tries: number;
97
- creationTimestamp: number;
98
- priorityAgeTimestamp: number;
99
- scheduleTimestamp: number;
100
- /**
101
- * Timestamp when the task most recently switched to Running state.
102
- * Used for Hard Execution Timeouts.
103
- */
104
- startTimestamp: number | null;
105
- /**
106
- * Timestamp after which the task is considered expired if it hasn't started Running.
107
- * If null, the task never expires in the queue.
108
- */
109
- timeToLive: number | null;
110
- /**
111
- * Token expiration (Soft Timeout).
112
- */
113
- visibilityDeadline: number | null;
114
- completeTimestamp: number | null;
115
- /** A number between 0 and 1 indicating the progress of the task. */
116
- progress: number;
117
- /** A snapshot of the current state of the task. */
118
- state: State | null;
119
- result: Result | null;
120
- error: UndefinableJson | null;
121
- };
50
+ export type Task<Definitions extends TaskDefinitionMap = Record<string, {
51
+ data: unknown;
52
+ state: unknown;
53
+ result: unknown;
54
+ }>> = {
55
+ [Type in TaskTypes<Definitions>]: {
56
+ id: string;
57
+ namespace: string;
58
+ type: Type;
59
+ status: TaskStatus;
60
+ token: string | null;
61
+ /**
62
+ * The lower the number, the higher the priority.
63
+ * @default 1000
64
+ */
65
+ priority: number;
66
+ idempotencyKey: string | null;
67
+ traceId: string | null;
68
+ tags: string[];
69
+ completeAfterTags: string[];
70
+ scheduleAfterTags: string[];
71
+ failFast: boolean;
72
+ dependencyJoinMode: DependencyJoinMode;
73
+ dependencyTriggerStatuses: TaskStatus[];
74
+ data: TaskData<Definitions, Type>;
75
+ parentId: string | null;
76
+ tries: number;
77
+ creationTimestamp: number;
78
+ priorityAgeTimestamp: number;
79
+ scheduleTimestamp: number;
80
+ /**
81
+ * Timestamp when the task most recently switched to Running state.
82
+ * Used for Hard Execution Timeouts.
83
+ */
84
+ startTimestamp: number | null;
85
+ /**
86
+ * Timestamp after which the task is considered expired if it hasn't started Running.
87
+ * If null, the task never expires in the queue.
88
+ */
89
+ timeToLive: number | null;
90
+ /**
91
+ * Token expiration (Soft Timeout).
92
+ */
93
+ visibilityDeadline: number | null;
94
+ completeTimestamp: number | null;
95
+ /** A number between 0 and 1 indicating the progress of the task. */
96
+ progress: number;
97
+ /** A snapshot of the current state of the task. */
98
+ state: TaskState<Definitions, Type> | null;
99
+ result: TaskResult<Definitions, Type> | null;
100
+ error: UndefinableJson | null;
101
+ };
102
+ }[Extract<keyof Definitions, string>];
122
103
  export declare const defaultTaskPriority = 1000;
123
104
  export type EnqueueOptions = {
124
105
  priority?: number;
@@ -130,16 +111,18 @@ export type EnqueueOptions = {
130
111
  scheduleAfterTags?: string[];
131
112
  failFast?: boolean;
132
113
  dependencyJoinMode?: DependencyJoinMode;
133
- dependencyTriggerStates?: TaskState[];
114
+ dependencyTriggerStatuses?: TaskStatus[];
134
115
  scheduleTimestamp?: number;
135
116
  timeToLive?: number;
136
117
  transaction?: Transaction;
137
118
  };
138
119
  export type EnqueueOneOptions = EnqueueOptions;
139
- export type EnqueueManyItem<T> = EnqueueOptions & {
140
- type: string;
141
- data: T;
142
- };
120
+ export type EnqueueManyItem<Definitions extends TaskDefinitionMap = TaskDefinitionMap, Type extends TaskTypes<Definitions> = TaskTypes<Definitions>> = {
121
+ [Type in TaskTypes<Definitions>]: {
122
+ type: Type;
123
+ data: TaskData<Definitions, Type>;
124
+ } & EnqueueOptions;
125
+ }[Type];
143
126
  export type EnqueueManyOptions = {
144
127
  replace?: boolean;
145
128
  returnTasks?: boolean;
@@ -192,7 +175,7 @@ export type TaskQueueArgument = string | (QueueConfig & {
192
175
  namespace: string;
193
176
  });
194
177
  export declare const defaultQueueConfig: Required<QueueConfig>;
195
- export declare abstract class TaskQueue<Data, State = unknown, Result = unknown> extends Transactional implements Resolvable<TaskQueueArgument> {
178
+ export declare abstract class TaskQueue<Definitions extends TaskDefinitionMap = TaskDefinitionMap> extends Transactional implements Resolvable<TaskQueueArgument> {
196
179
  readonly [resolveArgumentType]: TaskQueueArgument;
197
180
  protected readonly config: QueueConfig & {
198
181
  namespace: string;
@@ -200,82 +183,127 @@ export declare abstract class TaskQueue<Data, State = unknown, Result = unknown>
200
183
  protected readonly logger: Logger;
201
184
  abstract readonly visibilityTimeout: number;
202
185
  abstract readonly maxTries: number;
203
- batch(): TaskQueueEnqueueBatch<Data, State, Result>;
204
- abstract enqueue(type: string, data: Data, options?: EnqueueOneOptions): Promise<Task<Data, State, Result>>;
205
- abstract enqueueMany(items: EnqueueManyItem<Data>[], options?: EnqueueManyOptions & {
186
+ batch(): TaskQueueEnqueueBatch<Definitions>;
187
+ abstract enqueue<Type extends TaskTypes<Definitions>>(type: Type, data: TaskData<Definitions, Type>, options?: EnqueueOneOptions): Promise<TaskOfType<Definitions, Type>>;
188
+ abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options?: EnqueueManyOptions & {
206
189
  returnTasks?: false;
207
190
  }): Promise<void>;
208
- abstract enqueueMany(items: EnqueueManyItem<Data>[], options: EnqueueManyOptions & {
191
+ abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options: EnqueueManyOptions & {
209
192
  returnTasks: true;
210
- }): Promise<Task<Data, State, Result>[]>;
211
- abstract enqueueMany(items: EnqueueManyItem<Data>[], options?: EnqueueManyOptions): Promise<Task<Data, State, Result>[] | undefined>;
212
- abstract has(id: string): Promise<boolean>;
213
- abstract getTask(id: string): Promise<Task<Data, State, Result> | undefined>;
214
- abstract getManyByTags(tags: OneOrMany<string>): Promise<Task<Data, State, Result>[]>;
215
- abstract countByTags(tags: OneOrMany<string>): Promise<number>;
216
- abstract getTree(rootId: string | string[]): Promise<Task<unknown, unknown, unknown>[]>;
217
- abstract cancel(id: string): Promise<void>;
218
- abstract cancelMany(ids: string[]): Promise<void>;
219
- abstract cancelManyByTags(tags: OneOrMany<string>): Promise<void>;
193
+ }): Promise<TaskOfType<Definitions, Type>[]>;
194
+ abstract enqueueMany<Type extends TaskTypes<Definitions>>(items: EnqueueManyItem<Definitions, Type>[], options?: EnqueueManyOptions): Promise<TaskOfType<Definitions, Type>[] | undefined>;
195
+ abstract has(id: string, options?: {
196
+ transaction?: Transaction;
197
+ }): Promise<boolean>;
198
+ abstract getTask(id: string, options?: {
199
+ transaction?: Transaction;
200
+ }): Promise<Task<Definitions> | undefined>;
201
+ abstract getManyByTags(tags: OneOrMany<string>, options?: {
202
+ transaction?: Transaction;
203
+ }): Promise<Task<Definitions>[]>;
204
+ abstract countByTags(tags: OneOrMany<string>, options?: {
205
+ transaction?: Transaction;
206
+ }): Promise<number>;
207
+ abstract getTree(rootId: string | string[], options?: {
208
+ transaction?: Transaction;
209
+ }): Promise<Task[]>;
210
+ abstract cancel(id: string, options?: {
211
+ transaction?: Transaction;
212
+ }): Promise<void>;
213
+ abstract cancelMany(ids: string[], options?: {
214
+ transaction?: Transaction;
215
+ }): Promise<void>;
216
+ abstract cancelManyByTags(tags: OneOrMany<string>, options?: {
217
+ transaction?: Transaction;
218
+ }): Promise<void>;
220
219
  /** Clears all tasks from the queue. Use with caution! */
221
- abstract clear(): Promise<void>;
222
- abstract dequeue(): Promise<Task<Data, State, Result> | undefined>;
223
- abstract dequeueMany(count: number, options?: {
220
+ abstract clear(options?: {
221
+ transaction?: Transaction;
222
+ }): Promise<void>;
223
+ abstract dequeue<Type extends TaskTypes<Definitions>>(options?: {
224
224
  forceDequeue?: boolean;
225
- types?: string[];
226
- }): Promise<Task<Data, State, Result>[]>;
225
+ types?: Type[];
226
+ transaction?: Transaction;
227
+ }): Promise<TaskOfType<Definitions, Type> | undefined>;
228
+ abstract dequeueMany<Type extends TaskTypes<Definitions>>(count: number, options?: {
229
+ forceDequeue?: boolean;
230
+ types?: Type[];
231
+ transaction?: Transaction;
232
+ }): Promise<TaskOfType<Definitions, Type>[]>;
227
233
  /**
228
234
  * Reschedules a task to run at a specific time.
229
235
  * NOTE: If the task is currently running, its retry count is decremented (refunded) so this attempt doesn't count against maxTries.
230
236
  */
231
- abstract reschedule(id: string, timestamp: number, transaction?: Transaction): Promise<void>;
232
- abstract rescheduleMany(ids: string[], timestamp: number, transaction?: Transaction): Promise<void>;
233
- abstract rescheduleManyByTags(tags: OneOrMany<string>, timestamp: number, transaction?: Transaction): Promise<void>;
237
+ abstract reschedule(id: string, timestamp: number, options?: {
238
+ transaction?: Transaction;
239
+ }): Promise<void>;
240
+ abstract rescheduleMany(ids: string[], timestamp: number, options?: {
241
+ transaction?: Transaction;
242
+ }): Promise<void>;
243
+ abstract rescheduleManyByTags(tags: OneOrMany<string>, timestamp: number, options?: {
244
+ transaction?: Transaction;
245
+ }): Promise<void>;
234
246
  /**
235
247
  * Updates task progress, state and lock.
236
248
  * Returns the updated task if successful, `undefined` if task is lost/cancelled/timed out.
237
249
  */
238
- abstract touch(task: Task, options?: {
250
+ abstract touch<Type extends TaskTypes<Definitions>>(task: TaskOfType<Definitions, Type>, options?: {
239
251
  progress?: number;
240
- state?: State;
252
+ state?: TaskState<Definitions, Type>;
241
253
  transaction?: Transaction;
242
- }): Promise<Task<Data, State, Result> | undefined>;
254
+ }): Promise<TaskOfType<Definitions, Type> | undefined>;
243
255
  /**
244
256
  * Updates multiple tasks' progress, state and lock.
245
257
  * Returns the IDs of the successfully updated tasks.
246
258
  */
247
- abstract touchMany(tasks: Task[], progresses?: number[], states?: State[], transaction?: Transaction): Promise<string[]>;
248
- abstract complete<R>(task: Task<any, any, R>, result?: R, transaction?: Transaction): Promise<void>;
249
- abstract completeMany<R>(tasks: Task<any, any, R>[], results?: R[], transaction?: Transaction): Promise<void>;
250
- abstract fail(task: Task, error: any, fatal?: boolean, transaction?: Transaction): Promise<void>;
251
- abstract failMany(tasks: Task[], error: any, transaction?: Transaction): Promise<void>;
252
- abstract maintenance(): Promise<void>;
259
+ abstract touchMany<Tasks extends Task<Definitions>[]>(tasks: Tasks, options?: {
260
+ progresses?: number[];
261
+ states?: TasksStates<Tasks>;
262
+ transaction?: Transaction;
263
+ }): Promise<string[]>;
264
+ abstract complete<Type extends TaskTypes<Definitions>>(task: TaskOfType<Definitions, Type>, options?: {
265
+ result?: TaskResult<Definitions, Type>;
266
+ transaction?: Transaction;
267
+ }): Promise<void>;
268
+ abstract completeMany<Tasks extends Task<Definitions>[]>(tasks: Tasks, options?: {
269
+ results?: TasksResults<Tasks>;
270
+ transaction?: Transaction;
271
+ }): Promise<void>;
272
+ abstract fail(task: Task<Definitions>, error: unknown, options?: {
273
+ fatal?: boolean;
274
+ transaction?: Transaction;
275
+ }): Promise<void>;
276
+ abstract failMany(tasks: Task<Definitions>[], errors: unknown[], options?: {
277
+ transaction?: Transaction;
278
+ }): Promise<void>;
279
+ abstract maintenance(options?: {
280
+ transaction?: Transaction;
281
+ }): Promise<void>;
253
282
  abstract restart(id: string, options?: {
254
283
  resetState?: boolean;
255
284
  transaction?: Transaction;
256
285
  }): Promise<void>;
257
- abstract getConsumer(cancellationSignal: CancellationSignal, options?: {
286
+ abstract getConsumer<Type extends TaskTypes<Definitions>>(cancellationSignal: CancellationSignal, options?: {
258
287
  forceDequeue?: boolean;
259
- types?: string[];
260
- }): AsyncIterableIterator<Task<Data, State, Result>>;
261
- abstract getBatchConsumer(size: number, cancellationSignal: CancellationSignal, options?: {
288
+ types?: Type[];
289
+ }): AsyncIterableIterator<TaskOfType<Definitions, Type>>;
290
+ abstract getBatchConsumer<Type extends TaskTypes<Definitions>>(size: number, cancellationSignal: CancellationSignal, options?: {
262
291
  forceDequeue?: boolean;
263
- types?: string[];
264
- }): AsyncIterableIterator<Task<Data, State, Result>[]>;
265
- process({ concurrency, cancellationSignal, types, forceDequeue }: {
292
+ types?: Type[];
293
+ }): AsyncIterableIterator<TaskOfType<Definitions, Type>[]>;
294
+ process<Type extends TaskTypes<Definitions>>({ concurrency, cancellationSignal, types, forceDequeue }: {
266
295
  concurrency?: number;
267
296
  cancellationSignal: CancellationSignal;
268
- types?: string[];
297
+ types?: Type[];
269
298
  forceDequeue?: boolean;
270
- }, handler: ProcessWorker<Data, State, Result>): void;
271
- processBatch({ batchSize, concurrency, cancellationSignal, types, forceDequeue }: {
299
+ }, handler: ProcessWorker<Definitions, Type>): void;
300
+ processBatch<Type extends TaskTypes<Definitions>>({ batchSize, concurrency, cancellationSignal, types, forceDequeue }: {
272
301
  batchSize?: number;
273
302
  concurrency?: number;
274
303
  cancellationSignal: CancellationSignal;
275
- types?: string[];
304
+ types?: Type[];
276
305
  forceDequeue?: boolean;
277
- }, handler: ProcessBatchWorker<Data, State, Result>): void;
306
+ }, handler: ProcessBatchWorker<Definitions, Type>): void;
278
307
  private processWorker;
279
308
  private processBatchWorker;
280
309
  }
281
- export {};