@twin.org/background-task-models 0.0.1-next.2 → 0.0.1-next.20

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.
@@ -1,5 +1,7 @@
1
1
  export * from "./factories/backgroundTaskConnectorFactory";
2
- export * from "./models/backgroundTaskHandler";
3
2
  export * from "./models/IBackgroundTask";
4
3
  export * from "./models/IBackgroundTaskConnector";
4
+ export * from "./models/IScheduledTaskInfo";
5
+ export * from "./models/IScheduledTaskTime";
6
+ export * from "./models/ITaskSchedulerComponent";
5
7
  export * from "./models/taskStatus";
@@ -1,7 +1,7 @@
1
1
  import type { IError } from "@twin.org/core";
2
2
  import type { TaskStatus } from "./taskStatus";
3
3
  /**
4
- * Interface describing a background task connector.
4
+ * Interface describing a background task.
5
5
  */
6
6
  export interface IBackgroundTask<T = any, U = any> {
7
7
  /**
@@ -1,6 +1,5 @@
1
1
  import type { IComponent } from "@twin.org/core";
2
2
  import type { SortDirection } from "@twin.org/entity";
3
- import type { BackgroundTaskHandler } from "./backgroundTaskHandler";
4
3
  import type { IBackgroundTask } from "./IBackgroundTask";
5
4
  import type { TaskStatus } from "./taskStatus";
6
5
  /**
@@ -10,10 +9,12 @@ export interface IBackgroundTaskConnector extends IComponent {
10
9
  /**
11
10
  * Register a handler for a task.
12
11
  * @param taskType The type of the task the handler can process.
13
- * @param handler The handler for the task.
12
+ * @param module The module the handler is in.
13
+ * @param method The method in the module to execute.
14
+ * @param stateChangeCallback The callback to execute when the task state is updated.
14
15
  * @returns Nothing.
15
16
  */
16
- registerHandler<T, U>(taskType: string, handler: BackgroundTaskHandler<T, U>): Promise<void>;
17
+ registerHandler<T, U>(taskType: string, module: string, method: string, stateChangeCallback?: (task: IBackgroundTask<T, U>) => Promise<void>): Promise<void>;
17
18
  /**
18
19
  * Unregister a handler for a task.
19
20
  * @param taskType The type of the task handler to remove.
@@ -0,0 +1,12 @@
1
+ import type { IScheduledTaskTime } from "./IScheduledTaskTime";
2
+ /**
3
+ * Interface describing a scheduled task information.
4
+ */
5
+ export interface IScheduledTaskInfo {
6
+ /**
7
+ * The information for the tasks.
8
+ */
9
+ tasks: {
10
+ [id: string]: IScheduledTaskTime[];
11
+ };
12
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Interface describing a scheduled task time.
3
+ */
4
+ export interface IScheduledTaskTime {
5
+ /**
6
+ * The date/time to start the task, if not provided defaults to first interval from now.
7
+ */
8
+ nextTriggerTime?: number;
9
+ /**
10
+ * The interval in days to repeat the task, if no intervals are set the task will not repeat.
11
+ */
12
+ intervalDays?: number;
13
+ /**
14
+ * The interval in hours to repeat the task, if no intervals are set the task will not repeat.
15
+ */
16
+ intervalHours?: number;
17
+ /**
18
+ * The interval in minutes to repeat the task, if no intervals are set the task will not repeat.
19
+ */
20
+ intervalMinutes?: number;
21
+ }
@@ -0,0 +1,27 @@
1
+ import type { IComponent } from "@twin.org/core";
2
+ import type { IScheduledTaskInfo } from "./IScheduledTaskInfo";
3
+ import type { IScheduledTaskTime } from "./IScheduledTaskTime";
4
+ /**
5
+ * Interface describing a task scheduler.
6
+ */
7
+ export interface ITaskSchedulerComponent extends IComponent {
8
+ /**
9
+ * Add a task to the scheduler.
10
+ * @param taskId The id of the task to add.
11
+ * @param times The times at which the task should be scheduled.
12
+ * @param taskCallback The callback to execute when the task is scheduled.
13
+ * @returns Nothing.
14
+ */
15
+ addTask(taskId: string, times: IScheduledTaskTime[], taskCallback: () => Promise<void>): Promise<void>;
16
+ /**
17
+ * Remove a task from the scheduler.
18
+ * @param taskId The id of the task to remove.
19
+ * @returns Nothing.
20
+ */
21
+ removeTask(taskId: string): Promise<void>;
22
+ /**
23
+ * Get the information about the tasks.
24
+ * @returns The tasks information.
25
+ */
26
+ tasksInfo(): Promise<IScheduledTaskInfo>;
27
+ }
package/docs/changelog.md CHANGED
@@ -1,5 +1,64 @@
1
1
  # @twin.org/background-task-models - Changelog
2
2
 
3
- ## v0.0.1-next.2
3
+ ## [0.0.1-next.20](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.19...background-task-models-v0.0.1-next.20) (2025-06-23)
4
+
5
+
6
+ ### Features
7
+
8
+ * add task scheduler ([754d973](https://github.com/twinfoundation/background-task/commit/754d973e7c8483e5e54e887c157661867d5a0375))
9
+ * improve comment ([f2b4ac2](https://github.com/twinfoundation/background-task/commit/f2b4ac22fb8f735d2bf4ce28583cf7c54acdf272))
10
+ * update dependencies ([8e65767](https://github.com/twinfoundation/background-task/commit/8e657679f5e4305dbcb15ac7bcb3ab8a4613a60b))
11
+ * use shared store mechanism ([#6](https://github.com/twinfoundation/background-task/issues/6)) ([27ed203](https://github.com/twinfoundation/background-task/commit/27ed20367d5ace7257bfa7a82b59ad70e5b5d209))
12
+
13
+ ## [0.0.1-next.19](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.18...background-task-models-v0.0.1-next.19) (2025-06-23)
14
+
15
+
16
+ ### Features
17
+
18
+ * add task scheduler ([754d973](https://github.com/twinfoundation/background-task/commit/754d973e7c8483e5e54e887c157661867d5a0375))
19
+
20
+ ## [0.0.1-next.18](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.17...background-task-models-v0.0.1-next.18) (2025-06-19)
21
+
22
+
23
+ ### Miscellaneous Chores
24
+
25
+ * **background-task-models:** Synchronize repo versions
26
+
27
+ ## [0.0.1-next.17](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.16...background-task-models-v0.0.1-next.17) (2025-06-12)
28
+
29
+
30
+ ### Features
31
+
32
+ * update dependencies ([8e65767](https://github.com/twinfoundation/background-task/commit/8e657679f5e4305dbcb15ac7bcb3ab8a4613a60b))
33
+
34
+ ## [0.0.1-next.16](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.15...background-task-models-v0.0.1-next.16) (2025-06-05)
35
+
36
+
37
+ ### Miscellaneous Chores
38
+
39
+ * **background-task-models:** Synchronize repo versions
40
+
41
+ ## [0.0.1-next.15](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.14...background-task-models-v0.0.1-next.15) (2025-04-17)
42
+
43
+
44
+ ### Features
45
+
46
+ * use shared store mechanism ([#6](https://github.com/twinfoundation/background-task/issues/6)) ([27ed203](https://github.com/twinfoundation/background-task/commit/27ed20367d5ace7257bfa7a82b59ad70e5b5d209))
47
+
48
+ ## [0.0.1-next.14](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.13...background-task-models-v0.0.1-next.14) (2025-04-11)
49
+
50
+
51
+ ### Miscellaneous Chores
52
+
53
+ * **background-task-models:** Synchronize repo versions
54
+
55
+ ## [0.0.1-next.13](https://github.com/twinfoundation/background-task/compare/background-task-models-v0.0.1-next.12...background-task-models-v0.0.1-next.13) (2025-03-28)
56
+
57
+
58
+ ### Features
59
+
60
+ * improve comment ([f2b4ac2](https://github.com/twinfoundation/background-task/commit/f2b4ac22fb8f735d2bf4ce28583cf7c54acdf272))
61
+
62
+ ## v0.0.1-next.12
4
63
 
5
64
  - Initial Release
@@ -4,10 +4,12 @@
4
4
 
5
5
  - [IBackgroundTask](interfaces/IBackgroundTask.md)
6
6
  - [IBackgroundTaskConnector](interfaces/IBackgroundTaskConnector.md)
7
+ - [IScheduledTaskInfo](interfaces/IScheduledTaskInfo.md)
8
+ - [IScheduledTaskTime](interfaces/IScheduledTaskTime.md)
9
+ - [ITaskSchedulerComponent](interfaces/ITaskSchedulerComponent.md)
7
10
 
8
11
  ## Type Aliases
9
12
 
10
- - [BackgroundTaskHandler](type-aliases/BackgroundTaskHandler.md)
11
13
  - [TaskStatus](type-aliases/TaskStatus.md)
12
14
 
13
15
  ## Variables
@@ -1,12 +1,16 @@
1
1
  # Interface: IBackgroundTask\<T, U\>
2
2
 
3
- Interface describing a background task connector.
3
+ Interface describing a background task.
4
4
 
5
5
  ## Type Parameters
6
6
 
7
- **T** = `any`
7
+ ### T
8
8
 
9
- **U** = `any`
9
+ `T` = `any`
10
+
11
+ ### U
12
+
13
+ `U` = `any`
10
14
 
11
15
  ## Properties
12
16
 
@@ -10,25 +10,45 @@ Interface describing a background task connector.
10
10
 
11
11
  ### registerHandler()
12
12
 
13
- > **registerHandler**\<`T`, `U`\>(`taskType`, `handler`): `Promise`\<`void`\>
13
+ > **registerHandler**\<`T`, `U`\>(`taskType`, `module`, `method`, `stateChangeCallback?`): `Promise`\<`void`\>
14
14
 
15
15
  Register a handler for a task.
16
16
 
17
17
  #### Type Parameters
18
18
 
19
- **T**
19
+ ##### T
20
20
 
21
- • **U**
21
+ `T`
22
+
23
+ ##### U
24
+
25
+ `U`
22
26
 
23
27
  #### Parameters
24
28
 
25
- **taskType**: `string`
29
+ ##### taskType
30
+
31
+ `string`
26
32
 
27
33
  The type of the task the handler can process.
28
34
 
29
- **handler**: [`BackgroundTaskHandler`](../type-aliases/BackgroundTaskHandler.md)\<`T`, `U`\>
35
+ ##### module
36
+
37
+ `string`
38
+
39
+ The module the handler is in.
40
+
41
+ ##### method
42
+
43
+ `string`
44
+
45
+ The method in the module to execute.
46
+
47
+ ##### stateChangeCallback?
30
48
 
31
- The handler for the task.
49
+ (`task`) => `Promise`\<`void`\>
50
+
51
+ The callback to execute when the task state is updated.
32
52
 
33
53
  #### Returns
34
54
 
@@ -46,7 +66,9 @@ Unregister a handler for a task.
46
66
 
47
67
  #### Parameters
48
68
 
49
- **taskType**: `string`
69
+ ##### taskType
70
+
71
+ `string`
50
72
 
51
73
  The type of the task handler to remove.
52
74
 
@@ -60,37 +82,49 @@ Nothing.
60
82
 
61
83
  ### create()
62
84
 
63
- > **create**\<`T`\>(`type`, `payload`?, `options`?): `Promise`\<`string`\>
85
+ > **create**\<`T`\>(`type`, `payload?`, `options?`): `Promise`\<`string`\>
64
86
 
65
87
  Create a new task.
66
88
 
67
89
  #### Type Parameters
68
90
 
69
- **T**
91
+ ##### T
92
+
93
+ `T`
70
94
 
71
95
  #### Parameters
72
96
 
73
- **type**: `string`
97
+ ##### type
98
+
99
+ `string`
74
100
 
75
101
  The type of the task.
76
102
 
77
- **payload?**: `T`
103
+ ##### payload?
104
+
105
+ `T`
78
106
 
79
107
  The payload for the task.
80
108
 
81
- **options?**
109
+ ##### options?
82
110
 
83
111
  Additional options for the task.
84
112
 
85
- **options.retryCount?**: `number`
113
+ ###### retryCount?
114
+
115
+ `number`
86
116
 
87
117
  The number of times to retry the task if it fails, leave undefined to retry forever.
88
118
 
89
- **options.retryInterval?**: `number`
119
+ ###### retryInterval?
120
+
121
+ `number`
90
122
 
91
123
  The interval in milliseconds to wait between retries, defaults to 5000, leave undefined for default scheduling.
92
124
 
93
- **options.retainFor?**: `number`
125
+ ###### retainFor?
126
+
127
+ `number`
94
128
 
95
129
  The amount of time in milliseconds to retain the result until removal, defaults to 0 for immediate removal, set to -1 to keep forever.
96
130
 
@@ -110,13 +144,19 @@ Get the task details.
110
144
 
111
145
  #### Type Parameters
112
146
 
113
- **T**
147
+ ##### T
148
+
149
+ `T`
114
150
 
115
- **U**
151
+ ##### U
152
+
153
+ `U`
116
154
 
117
155
  #### Parameters
118
156
 
119
- **taskId**: `string`
157
+ ##### taskId
158
+
159
+ `string`
120
160
 
121
161
  The id of the task to get the details for.
122
162
 
@@ -136,7 +176,9 @@ Retry a failed task immediately instead of waiting for it's next scheduled retry
136
176
 
137
177
  #### Parameters
138
178
 
139
- **taskId**: `string`
179
+ ##### taskId
180
+
181
+ `string`
140
182
 
141
183
  The id of the task to retry.
142
184
 
@@ -156,7 +198,9 @@ Remove a task ignoring any retain until date.
156
198
 
157
199
  #### Parameters
158
200
 
159
- **taskId**: `string`
201
+ ##### taskId
202
+
203
+ `string`
160
204
 
161
205
  The id of the task to remove.
162
206
 
@@ -176,7 +220,9 @@ Cancel a task, will only be actioned if the task is currently pending.
176
220
 
177
221
  #### Parameters
178
222
 
179
- **taskId**: `string`
223
+ ##### taskId
224
+
225
+ `string`
180
226
 
181
227
  The id of the task to cancel.
182
228
 
@@ -190,46 +236,50 @@ Nothing.
190
236
 
191
237
  ### query()
192
238
 
193
- > **query**(`taskType`?, `taskStatus`?, `sortProperty`?, `sortDirection`?, `cursor`?, `pageSize`?): `Promise`\<`object`\>
239
+ > **query**(`taskType?`, `taskStatus?`, `sortProperty?`, `sortDirection?`, `cursor?`, `pageSize?`): `Promise`\<\{ `entities`: [`IBackgroundTask`](IBackgroundTask.md)\<`any`, `any`\>[]; `cursor?`: `string`; \}\>
194
240
 
195
241
  Get a list of tasks.
196
242
 
197
243
  #### Parameters
198
244
 
199
- **taskType?**: `string`
245
+ ##### taskType?
246
+
247
+ `string`
200
248
 
201
249
  The type of the task to get.
202
250
 
203
- **taskStatus?**: [`TaskStatus`](../type-aliases/TaskStatus.md)
251
+ ##### taskStatus?
252
+
253
+ [`TaskStatus`](../type-aliases/TaskStatus.md)
204
254
 
205
255
  The status of the task to get.
206
256
 
207
- **sortProperty?**: `"status"` \| `"dateCreated"` \| `"dateModified"` \| `"dateCompleted"`
257
+ ##### sortProperty?
208
258
 
209
259
  The property to sort by, defaults to dateCreated.
210
260
 
211
- **sortDirection?**: `SortDirection`
261
+ `"dateCreated"` | `"dateModified"` | `"dateCompleted"` | `"status"`
262
+
263
+ ##### sortDirection?
264
+
265
+ `SortDirection`
212
266
 
213
267
  The order to sort by, defaults to ascending.
214
268
 
215
- **cursor?**: `string`
269
+ ##### cursor?
270
+
271
+ `string`
216
272
 
217
273
  The cursor to get the next page of tasks.
218
274
 
219
- **pageSize?**: `number`
275
+ ##### pageSize?
276
+
277
+ `number`
220
278
 
221
279
  The maximum number of entities in a page.
222
280
 
223
281
  #### Returns
224
282
 
225
- `Promise`\<`object`\>
283
+ `Promise`\<\{ `entities`: [`IBackgroundTask`](IBackgroundTask.md)\<`any`, `any`\>[]; `cursor?`: `string`; \}\>
226
284
 
227
285
  The list of tasks.
228
-
229
- ##### entities
230
-
231
- > **entities**: [`IBackgroundTask`](IBackgroundTask.md)\<`any`, `any`\>[]
232
-
233
- ##### cursor?
234
-
235
- > `optional` **cursor**: `string`
@@ -0,0 +1,15 @@
1
+ # Interface: IScheduledTaskInfo
2
+
3
+ Interface describing a scheduled task information.
4
+
5
+ ## Properties
6
+
7
+ ### tasks
8
+
9
+ > **tasks**: `object`
10
+
11
+ The information for the tasks.
12
+
13
+ #### Index Signature
14
+
15
+ \[`id`: `string`\]: [`IScheduledTaskTime`](IScheduledTaskTime.md)[]
@@ -0,0 +1,35 @@
1
+ # Interface: IScheduledTaskTime
2
+
3
+ Interface describing a scheduled task time.
4
+
5
+ ## Properties
6
+
7
+ ### nextTriggerTime?
8
+
9
+ > `optional` **nextTriggerTime**: `number`
10
+
11
+ The date/time to start the task, if not provided defaults to first interval from now.
12
+
13
+ ***
14
+
15
+ ### intervalDays?
16
+
17
+ > `optional` **intervalDays**: `number`
18
+
19
+ The interval in days to repeat the task, if no intervals are set the task will not repeat.
20
+
21
+ ***
22
+
23
+ ### intervalHours?
24
+
25
+ > `optional` **intervalHours**: `number`
26
+
27
+ The interval in hours to repeat the task, if no intervals are set the task will not repeat.
28
+
29
+ ***
30
+
31
+ ### intervalMinutes?
32
+
33
+ > `optional` **intervalMinutes**: `number`
34
+
35
+ The interval in minutes to repeat the task, if no intervals are set the task will not repeat.
@@ -0,0 +1,77 @@
1
+ # Interface: ITaskSchedulerComponent
2
+
3
+ Interface describing a task scheduler.
4
+
5
+ ## Extends
6
+
7
+ - `IComponent`
8
+
9
+ ## Methods
10
+
11
+ ### addTask()
12
+
13
+ > **addTask**(`taskId`, `times`, `taskCallback`): `Promise`\<`void`\>
14
+
15
+ Add a task to the scheduler.
16
+
17
+ #### Parameters
18
+
19
+ ##### taskId
20
+
21
+ `string`
22
+
23
+ The id of the task to add.
24
+
25
+ ##### times
26
+
27
+ [`IScheduledTaskTime`](IScheduledTaskTime.md)[]
28
+
29
+ The times at which the task should be scheduled.
30
+
31
+ ##### taskCallback
32
+
33
+ () => `Promise`\<`void`\>
34
+
35
+ The callback to execute when the task is scheduled.
36
+
37
+ #### Returns
38
+
39
+ `Promise`\<`void`\>
40
+
41
+ Nothing.
42
+
43
+ ***
44
+
45
+ ### removeTask()
46
+
47
+ > **removeTask**(`taskId`): `Promise`\<`void`\>
48
+
49
+ Remove a task from the scheduler.
50
+
51
+ #### Parameters
52
+
53
+ ##### taskId
54
+
55
+ `string`
56
+
57
+ The id of the task to remove.
58
+
59
+ #### Returns
60
+
61
+ `Promise`\<`void`\>
62
+
63
+ Nothing.
64
+
65
+ ***
66
+
67
+ ### tasksInfo()
68
+
69
+ > **tasksInfo**(): `Promise`\<[`IScheduledTaskInfo`](IScheduledTaskInfo.md)\>
70
+
71
+ Get the information about the tasks.
72
+
73
+ #### Returns
74
+
75
+ `Promise`\<[`IScheduledTaskInfo`](IScheduledTaskInfo.md)\>
76
+
77
+ The tasks information.
@@ -1,5 +1,5 @@
1
1
  # Type Alias: TaskStatus
2
2
 
3
- > **TaskStatus**: *typeof* [`TaskStatus`](../variables/TaskStatus.md)\[keyof *typeof* [`TaskStatus`](../variables/TaskStatus.md)\]
3
+ > **TaskStatus** = *typeof* [`TaskStatus`](../variables/TaskStatus.md)\[keyof *typeof* [`TaskStatus`](../variables/TaskStatus.md)\]
4
4
 
5
5
  Task statuses.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/background-task-models",
3
- "version": "0.0.1-next.2",
3
+ "version": "0.0.1-next.20",
4
4
  "description": "Models which define the structure of the background task contracts and connectors",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,11 +23,11 @@
23
23
  "types": "./dist/types/index.d.ts",
24
24
  "exports": {
25
25
  ".": {
26
+ "types": "./dist/types/index.d.ts",
26
27
  "require": "./dist/cjs/index.cjs",
27
- "import": "./dist/esm/index.mjs",
28
- "types": "./dist/types/index.d.ts"
28
+ "import": "./dist/esm/index.mjs"
29
29
  },
30
- "./locales": "./locales"
30
+ "./locales/*.json": "./locales/*.json"
31
31
  },
32
32
  "files": [
33
33
  "dist/cjs",
@@ -1,4 +0,0 @@
1
- /**
2
- * Interface describing a task handler, exceptions thrown in the handler will be caught.
3
- */
4
- export type BackgroundTaskHandler<T = any, U = any> = (payload: T) => Promise<U>;
@@ -1,19 +0,0 @@
1
- # Type Alias: BackgroundTaskHandler()\<T, U\>
2
-
3
- > **BackgroundTaskHandler**\<`T`, `U`\>: (`payload`) => `Promise`\<`U`\>
4
-
5
- Interface describing a task handler, exceptions thrown in the handler will be caught.
6
-
7
- ## Type Parameters
8
-
9
- • **T** = `any`
10
-
11
- • **U** = `any`
12
-
13
- ## Parameters
14
-
15
- • **payload**: `T`
16
-
17
- ## Returns
18
-
19
- `Promise`\<`U`\>