@twin.org/background-task-scheduler 0.0.2-next.9 → 0.0.3-next.10

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.
@@ -4,9 +4,9 @@ Options for the task scheduler constructor.
4
4
 
5
5
  ## Properties
6
6
 
7
- ### loggingComponentType?
7
+ ### loggingComponentType? {#loggingcomponenttype}
8
8
 
9
- > `optional` **loggingComponentType**: `string`
9
+ > `optional` **loggingComponentType?**: `string`
10
10
 
11
11
  The logging component type.
12
12
 
@@ -18,8 +18,22 @@ logging
18
18
 
19
19
  ***
20
20
 
21
- ### config?
21
+ ### scheduledTaskEntityStorageType? {#scheduledtaskentitystoragetype}
22
22
 
23
- > `optional` **config**: [`ITaskSchedulerConfig`](ITaskSchedulerConfig.md)
23
+ > `optional` **scheduledTaskEntityStorageType?**: `string`
24
+
25
+ The scheduled task entity storage connector type.
26
+
27
+ #### Default
28
+
29
+ ```ts
30
+ scheduled-task
31
+ ```
32
+
33
+ ***
34
+
35
+ ### config? {#config}
36
+
37
+ > `optional` **config?**: [`ITaskSchedulerConfig`](ITaskSchedulerConfig.md)
24
38
 
25
39
  The configuration for the task scheduler.
package/locales/en.json CHANGED
@@ -10,5 +10,10 @@
10
10
  "taskRemoved": "Task with id \"{id}\" removed",
11
11
  "taskTriggered": "Task with id \"{id}\" triggered at time \"{time}\""
12
12
  }
13
+ },
14
+ "warn": {
15
+ "taskSchedulerService": {
16
+ "taskStalled": "Task with id \"{id}\" is stalled, last run at \"{lastRunTime}\" and exceeded timeout \"{stalledTaskTimeoutMs}\" ms"
17
+ }
13
18
  }
14
19
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@twin.org/background-task-scheduler",
3
- "version": "0.0.2-next.9",
4
- "description": "Background task scheduler",
3
+ "version": "0.0.3-next.10",
4
+ "description": "Schedules background tasks for one-off or recurring execution windows",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "git+https://github.com/twinfoundation/background-task.git",
7
+ "url": "git+https://github.com/iotaledger/twin-background-task.git",
8
8
  "directory": "packages/background-task-scheduler"
9
9
  },
10
10
  "author": "martyn.janes@iota.org",
@@ -14,25 +14,24 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/background-task-models": "0.0.2-next.9",
17
+ "@twin.org/background-task-models": "0.0.3-next.10",
18
18
  "@twin.org/core": "next",
19
+ "@twin.org/entity-storage-models": "next",
19
20
  "@twin.org/logging-models": "next",
20
21
  "@twin.org/nameof": "next"
21
22
  },
22
- "main": "./dist/cjs/index.cjs",
23
- "module": "./dist/esm/index.mjs",
23
+ "main": "./dist/es/index.js",
24
24
  "types": "./dist/types/index.d.ts",
25
25
  "exports": {
26
26
  ".": {
27
27
  "types": "./dist/types/index.d.ts",
28
- "require": "./dist/cjs/index.cjs",
29
- "import": "./dist/esm/index.mjs"
28
+ "import": "./dist/es/index.js",
29
+ "default": "./dist/es/index.js"
30
30
  },
31
31
  "./locales/*.json": "./locales/*.json"
32
32
  },
33
33
  "files": [
34
- "dist/cjs",
35
- "dist/esm",
34
+ "dist/es",
36
35
  "dist/types",
37
36
  "locales",
38
37
  "docs"
@@ -50,7 +49,7 @@
50
49
  "async"
51
50
  ],
52
51
  "bugs": {
53
- "url": "git+https://github.com/twinfoundation/background-task/issues"
52
+ "url": "git+https://github.com/iotaledger/twin-background-task/issues"
54
53
  },
55
54
  "homepage": "https://twindev.org"
56
55
  }
@@ -1,209 +0,0 @@
1
- 'use strict';
2
-
3
- var core = require('@twin.org/core');
4
-
5
- /**
6
- * Class for scheduling tasks.
7
- */
8
- class TaskSchedulerService {
9
- /**
10
- * Runtime name for the class.
11
- */
12
- static CLASS_NAME = "TaskSchedulerService";
13
- /**
14
- * The logger for the task service.
15
- * @internal
16
- */
17
- _logging;
18
- /**
19
- * The interval in milliseconds at which the tasks are checked.
20
- * @internal
21
- */
22
- _tickInterval;
23
- /**
24
- * The tasks that are scheduled.
25
- * @internal
26
- */
27
- _tasks;
28
- /**
29
- * The timer for running scheduled tasks.
30
- * @internal
31
- */
32
- _timer;
33
- /**
34
- * Create a new instance of TaskSchedulerComponent.
35
- * @param options The options for the scheduler.
36
- */
37
- constructor(options) {
38
- this._logging = core.ComponentFactory.getIfExists(options?.loggingComponentType ?? "logging");
39
- this._tasks = {};
40
- this._tickInterval = options?.config?.overrideInterval ?? 60 * 1000; // Default to 1 minute
41
- }
42
- /**
43
- * The component needs to be stopped when the node is closed.
44
- * @param nodeIdentity The identity of the node stopping the component.
45
- * @param nodeLoggingComponentType The node logging component type.
46
- * @returns Nothing.
47
- */
48
- async stop(nodeIdentity, nodeLoggingComponentType) {
49
- this.stopTimer();
50
- }
51
- /**
52
- * Add a task to the scheduler.
53
- * @param taskId The id of the task to add.
54
- * @param times The times at which the task should be scheduled.
55
- * @param taskCallback The callback to execute when the task is scheduled.
56
- * @returns Nothing.
57
- */
58
- async addTask(taskId, times, taskCallback) {
59
- this._tasks[taskId] = {
60
- times: times.map(time => ({
61
- ...time,
62
- nextTriggerTime: core.Is.empty(time.nextTriggerTime)
63
- ? this.calculateNextTriggerTime(time)
64
- : time.nextTriggerTime
65
- })),
66
- taskCallback
67
- };
68
- this._logging?.log({
69
- level: "info",
70
- source: TaskSchedulerService.CLASS_NAME,
71
- ts: Date.now(),
72
- message: "taskAdded",
73
- data: {
74
- id: taskId
75
- }
76
- });
77
- await this.startTimer();
78
- }
79
- /**
80
- * Remove a task from the scheduler.
81
- * @param taskId The id of the task to remove.
82
- * @returns Nothing.
83
- */
84
- async removeTask(taskId) {
85
- if (!core.Is.empty(this._tasks[taskId])) {
86
- this._logging?.log({
87
- level: "info",
88
- source: TaskSchedulerService.CLASS_NAME,
89
- ts: Date.now(),
90
- message: "taskRemoved",
91
- data: {
92
- id: taskId
93
- }
94
- });
95
- delete this._tasks[taskId];
96
- if (Object.keys(this._tasks).length === 0) {
97
- this.stopTimer();
98
- }
99
- }
100
- }
101
- /**
102
- * Get the information about the tasks.
103
- * @returns The tasks information.
104
- */
105
- async tasksInfo() {
106
- const tasksInfo = {
107
- tasks: {}
108
- };
109
- for (const taskId in this._tasks) {
110
- tasksInfo.tasks[taskId] = this._tasks[taskId].times;
111
- }
112
- return tasksInfo;
113
- }
114
- /**
115
- * Calculate the next run time for a task based on its scheduled times.
116
- * @param time The times at which the task should be scheduled.
117
- * @returns The update time with the next run.
118
- * @internal
119
- */
120
- calculateNextTriggerTime(time) {
121
- let nextTriggerTime = time.nextTriggerTime;
122
- if (core.Is.empty(nextTriggerTime)) {
123
- nextTriggerTime = Date.now();
124
- }
125
- if (!core.Is.empty(time.intervalDays)) {
126
- nextTriggerTime += time.intervalDays * 24 * 60 * 60 * 1000;
127
- }
128
- if (!core.Is.empty(time.intervalHours)) {
129
- nextTriggerTime += time.intervalHours * 60 * 60 * 1000;
130
- }
131
- if (!core.Is.empty(time.intervalMinutes)) {
132
- nextTriggerTime += time.intervalMinutes * 60 * 1000;
133
- }
134
- return nextTriggerTime;
135
- }
136
- /**
137
- * Start the timer for running scheduled tasks.
138
- * @internal
139
- */
140
- async startTimer() {
141
- if (core.Is.empty(this._timer)) {
142
- // Trigger immediately to catch up on any missed tasks
143
- await this.triggerScheduledTasks();
144
- // Set the timer to run at the specified interval
145
- this._timer = setInterval(async () => this.triggerScheduledTasks(), this._tickInterval);
146
- }
147
- }
148
- /**
149
- * Stop the timer for running scheduled tasks.
150
- * @internal
151
- */
152
- stopTimer() {
153
- if (!core.Is.empty(this._timer)) {
154
- clearInterval(this._timer);
155
- this._timer = undefined;
156
- }
157
- }
158
- /**
159
- * Trigger scheduled tasks based on their next run times.
160
- * @internal
161
- */
162
- async triggerScheduledTasks() {
163
- const now = Date.now();
164
- for (const taskId in this._tasks) {
165
- const task = this._tasks[taskId];
166
- for (const taskTime of task.times) {
167
- if (!core.Is.empty(taskTime.nextTriggerTime) && taskTime.nextTriggerTime <= now) {
168
- this._logging?.log({
169
- level: "info",
170
- source: TaskSchedulerService.CLASS_NAME,
171
- ts: Date.now(),
172
- message: "taskTriggered",
173
- data: {
174
- id: taskId,
175
- time: new Date(taskTime.nextTriggerTime).toISOString()
176
- }
177
- });
178
- try {
179
- await task.taskCallback();
180
- }
181
- catch (error) {
182
- this._logging?.log({
183
- level: "error",
184
- source: TaskSchedulerService.CLASS_NAME,
185
- ts: Date.now(),
186
- message: "taskFailed",
187
- data: {
188
- id: taskId
189
- },
190
- error: core.BaseError.fromError(error)
191
- });
192
- }
193
- // If the intervals are empty, we do not recalculate a next run time
194
- if (core.Is.empty(taskTime.intervalDays) &&
195
- core.Is.empty(taskTime.intervalHours) &&
196
- core.Is.empty(taskTime.intervalMinutes)) {
197
- taskTime.nextTriggerTime = undefined;
198
- }
199
- else {
200
- // Recalculate the next run time based on the current time and the intervals
201
- taskTime.nextTriggerTime = this.calculateNextTriggerTime(taskTime);
202
- }
203
- }
204
- }
205
- }
206
- }
207
- }
208
-
209
- exports.TaskSchedulerService = TaskSchedulerService;