@kyo-services/schedulewise 2.0.2 โ 2.2.0
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/README.md +49 -0
- package/dist/TaskScheduler.d.ts +28 -2
- package/dist/TaskScheduler.js +50 -3
- package/dist/TaskScheduler.js.map +1 -1
- package/package.json +1 -1
- package/src/TaskScheduler.ts +177 -123
package/README.md
CHANGED
|
@@ -20,10 +20,23 @@ A smart and efficient task scheduler for managing periodic jobs with precision t
|
|
|
20
20
|
|
|
21
21
|
## ๐ฆ Installation
|
|
22
22
|
|
|
23
|
+
You can install ScheduleWise using your preferred package manager:
|
|
24
|
+
|
|
25
|
+
Using npm:
|
|
23
26
|
```bash
|
|
24
27
|
npm install @kyo-services/schedulewise
|
|
25
28
|
```
|
|
26
29
|
|
|
30
|
+
Using yarn:
|
|
31
|
+
```bash
|
|
32
|
+
yarn add @kyo-services/schedulewise
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Using pnpm:
|
|
36
|
+
```bash
|
|
37
|
+
pnpm add @kyo-services/schedulewise
|
|
38
|
+
```
|
|
39
|
+
|
|
27
40
|
## ๐ง Usage
|
|
28
41
|
|
|
29
42
|
### Basic Task Scheduling
|
|
@@ -126,6 +139,42 @@ interface TaskOptions {
|
|
|
126
139
|
| `removeTask(identifier)` | Removes a specific task |
|
|
127
140
|
| `clearAllTasks()` | Removes all tasks |
|
|
128
141
|
| `getAllTasks()` | Gets all scheduled tasks |
|
|
142
|
+
| `pause()` | Pauses all task executions |
|
|
143
|
+
| `resume()` | Resumes all task executions |
|
|
144
|
+
| `isPaused()` | Checks if scheduler is paused |
|
|
145
|
+
| `once(callback, options)` | Creates a one-time task with simplified API |
|
|
146
|
+
| `changeProcessorInterval(newInterval)` | Changes the scheduler's processor interval |
|
|
147
|
+
|
|
148
|
+
### Scheduler Control
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// Pause all task executions
|
|
152
|
+
sw.pause();
|
|
153
|
+
|
|
154
|
+
// Check if scheduler is paused
|
|
155
|
+
console.log('Scheduler paused:', sw.isPaused()); // true
|
|
156
|
+
|
|
157
|
+
// Resume all task executions
|
|
158
|
+
sw.resume();
|
|
159
|
+
|
|
160
|
+
// Adjust processor interval (default: 100ms)
|
|
161
|
+
sw.changeProcessorInterval(500);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### One-Time Task (Simplified API)
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
// Using the simplified 'once' method
|
|
168
|
+
sw.once(
|
|
169
|
+
() => {
|
|
170
|
+
console.log('This is a one-time task');
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
interval: 5000, // Execute after 5 seconds
|
|
174
|
+
name: 'simpleOneTimeTask' // Optional name
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
```
|
|
129
178
|
|
|
130
179
|
## ๐งช Testing
|
|
131
180
|
|
package/dist/TaskScheduler.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Task } from
|
|
2
|
-
import { TaskCallback, TaskOptions } from
|
|
1
|
+
import { Task } from "./Task.js";
|
|
2
|
+
import { TaskCallback, TaskOptions } from "./types.js";
|
|
3
3
|
/**
|
|
4
4
|
* A singleton class that manages scheduled tasks in the system
|
|
5
5
|
* @class TaskScheduler
|
|
@@ -9,6 +9,7 @@ export declare class TaskScheduler {
|
|
|
9
9
|
private currentTimestamp;
|
|
10
10
|
private scheduledTasks;
|
|
11
11
|
private schedulerInterval;
|
|
12
|
+
private processorInterval;
|
|
12
13
|
private constructor();
|
|
13
14
|
/**
|
|
14
15
|
* Gets the singleton instance of TaskScheduler
|
|
@@ -42,11 +43,36 @@ export declare class TaskScheduler {
|
|
|
42
43
|
* Removes all tasks and stops the scheduler
|
|
43
44
|
*/
|
|
44
45
|
clearAllTasks(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Pauses the scheduler
|
|
48
|
+
*/
|
|
49
|
+
pause(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Resumes the scheduler
|
|
52
|
+
*/
|
|
53
|
+
resume(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the scheduler is paused
|
|
56
|
+
* @returns {boolean} True if the scheduler is paused, false otherwise
|
|
57
|
+
*/
|
|
58
|
+
isPaused(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Executes a callback once
|
|
61
|
+
* @param {() => void} callback - The callback to execute
|
|
62
|
+
* @param {TaskOptions} options - Configuration options for the task
|
|
63
|
+
* @returns {Task} The created task instance
|
|
64
|
+
*/
|
|
65
|
+
once(callback: () => void, options: TaskOptions): Task;
|
|
45
66
|
/**
|
|
46
67
|
* Updates current timestamp and processes tasks
|
|
47
68
|
* @internal For testing purposes
|
|
48
69
|
*/
|
|
49
70
|
processTasksNow(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Changes the processor interval
|
|
73
|
+
* @param {number} newInterval - The new interval in milliseconds
|
|
74
|
+
*/
|
|
75
|
+
changeProcessorInterval(newInterval: number): void;
|
|
50
76
|
/**
|
|
51
77
|
* Starts the task processor that executes scheduled tasks
|
|
52
78
|
* @private
|
package/dist/TaskScheduler.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Task } from
|
|
1
|
+
import { Task } from "./Task.js";
|
|
2
2
|
/**
|
|
3
3
|
* A singleton class that manages scheduled tasks in the system
|
|
4
4
|
* @class TaskScheduler
|
|
@@ -8,6 +8,7 @@ export class TaskScheduler {
|
|
|
8
8
|
currentTimestamp;
|
|
9
9
|
scheduledTasks = new Map();
|
|
10
10
|
schedulerInterval = null;
|
|
11
|
+
processorInterval = 100;
|
|
11
12
|
constructor() {
|
|
12
13
|
this.currentTimestamp = new Date();
|
|
13
14
|
this.startTaskProcessor();
|
|
@@ -76,6 +77,40 @@ export class TaskScheduler {
|
|
|
76
77
|
}
|
|
77
78
|
this.scheduledTasks.clear();
|
|
78
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Pauses the scheduler
|
|
82
|
+
*/
|
|
83
|
+
pause() {
|
|
84
|
+
if (this.schedulerInterval) {
|
|
85
|
+
clearInterval(this.schedulerInterval);
|
|
86
|
+
this.schedulerInterval = null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Resumes the scheduler
|
|
91
|
+
*/
|
|
92
|
+
resume() {
|
|
93
|
+
if (!this.schedulerInterval) {
|
|
94
|
+
this.startTaskProcessor();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Checks if the scheduler is paused
|
|
99
|
+
* @returns {boolean} True if the scheduler is paused, false otherwise
|
|
100
|
+
*/
|
|
101
|
+
isPaused() {
|
|
102
|
+
return this.schedulerInterval === null;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Executes a callback once
|
|
106
|
+
* @param {() => void} callback - The callback to execute
|
|
107
|
+
* @param {TaskOptions} options - Configuration options for the task
|
|
108
|
+
* @returns {Task} The created task instance
|
|
109
|
+
*/
|
|
110
|
+
once(callback, options) {
|
|
111
|
+
const task = this.scheduleTask(callback, { ...options, once: true });
|
|
112
|
+
return task;
|
|
113
|
+
}
|
|
79
114
|
/**
|
|
80
115
|
* Updates current timestamp and processes tasks
|
|
81
116
|
* @internal For testing purposes
|
|
@@ -85,7 +120,8 @@ export class TaskScheduler {
|
|
|
85
120
|
this.currentTimestamp = new Date();
|
|
86
121
|
for (const task of this.scheduledTasks.values()) {
|
|
87
122
|
const elapsedTime = task.getInterval();
|
|
88
|
-
const shouldRun = task.shouldExecute(elapsedTime) &&
|
|
123
|
+
const shouldRun = task.shouldExecute(elapsedTime) &&
|
|
124
|
+
task.lastExecutionTime.getTime() <= previousTimestamp.getTime();
|
|
89
125
|
if (!shouldRun)
|
|
90
126
|
continue;
|
|
91
127
|
task.execute(this.currentTimestamp);
|
|
@@ -94,6 +130,17 @@ export class TaskScheduler {
|
|
|
94
130
|
}
|
|
95
131
|
}
|
|
96
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Changes the processor interval
|
|
135
|
+
* @param {number} newInterval - The new interval in milliseconds
|
|
136
|
+
*/
|
|
137
|
+
changeProcessorInterval(newInterval) {
|
|
138
|
+
if (newInterval <= 0)
|
|
139
|
+
throw new Error('Interval must be greater than 0');
|
|
140
|
+
this.processorInterval = newInterval;
|
|
141
|
+
if (this.schedulerInterval !== null)
|
|
142
|
+
this.startTaskProcessor();
|
|
143
|
+
}
|
|
97
144
|
/**
|
|
98
145
|
* Starts the task processor that executes scheduled tasks
|
|
99
146
|
* @private
|
|
@@ -112,7 +159,7 @@ export class TaskScheduler {
|
|
|
112
159
|
if (task.shouldRemove())
|
|
113
160
|
this.removeTask(task.id);
|
|
114
161
|
}
|
|
115
|
-
},
|
|
162
|
+
}, this.processorInterval);
|
|
116
163
|
}
|
|
117
164
|
}
|
|
118
165
|
//# sourceMappingURL=TaskScheduler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskScheduler.js","sourceRoot":"","sources":["../src/TaskScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC;;;GAGG;AACH,MAAM,OAAO,aAAa;
|
|
1
|
+
{"version":3,"file":"TaskScheduler.js","sourceRoot":"","sources":["../src/TaskScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC;;;GAGG;AACH,MAAM,OAAO,aAAa;IACjB,MAAM,CAAC,QAAQ,CAAgB;IAC/B,gBAAgB,CAAO;IACvB,cAAc,GAA+B,IAAI,GAAG,EAAE,CAAC;IACvD,iBAAiB,GAA0C,IAAI,CAAC;IAChE,iBAAiB,GAAW,GAAG,CAAC;IAExC;QACC,IAAI,CAAC,gBAAgB,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC3B,CAAC;IAED;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CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;gBAElD,IAAI,CAAC,SAAS;oBAAE,SAAS;gBAEzB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;gBACpC,IAAI,IAAI,CAAC,YAAY,EAAE;oBAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACnD,CAAC;QACF,CAAC,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAC5B,CAAC;CACD"}
|
package/package.json
CHANGED
package/src/TaskScheduler.ts
CHANGED
|
@@ -1,130 +1,184 @@
|
|
|
1
|
-
import { Task } from
|
|
2
|
-
import { TaskCallback, TaskOptions } from
|
|
1
|
+
import { Task } from "./Task.js";
|
|
2
|
+
import { TaskCallback, TaskOptions } from "./types.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* A singleton class that manages scheduled tasks in the system
|
|
6
6
|
* @class TaskScheduler
|
|
7
7
|
*/
|
|
8
8
|
export class TaskScheduler {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
9
|
+
private static instance: TaskScheduler;
|
|
10
|
+
private currentTimestamp: Date;
|
|
11
|
+
private scheduledTasks: Map<number | string, Task> = new Map();
|
|
12
|
+
private schedulerInterval: ReturnType<typeof setInterval> | null = null;
|
|
13
|
+
private processorInterval: number = 100;
|
|
14
|
+
|
|
15
|
+
private constructor() {
|
|
16
|
+
this.currentTimestamp = new Date();
|
|
17
|
+
this.startTaskProcessor();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Gets the singleton instance of TaskScheduler
|
|
22
|
+
* @returns {TaskScheduler} The singleton instance
|
|
23
|
+
*/
|
|
24
|
+
public static getInstance(): TaskScheduler {
|
|
25
|
+
if (!TaskScheduler.instance)
|
|
26
|
+
TaskScheduler.instance = new TaskScheduler();
|
|
27
|
+
return TaskScheduler.instance;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Finds a task by its identifier (id or name)
|
|
32
|
+
* @param {number | string} identifier - The task's ID or name
|
|
33
|
+
* @returns {Task | undefined} The found task or undefined
|
|
34
|
+
*/
|
|
35
|
+
public findTask(identifier: number | string): Task | undefined {
|
|
36
|
+
return this.scheduledTasks.get(identifier);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Gets all scheduled tasks in the system
|
|
41
|
+
* @returns {Task[]} Array of all scheduled tasks
|
|
42
|
+
*/
|
|
43
|
+
public getAllTasks(): Task[] {
|
|
44
|
+
return Array.from(this.scheduledTasks.values());
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Creates and schedules a new task
|
|
49
|
+
* @param {TaskCallback} callback - Function to be executed
|
|
50
|
+
* @param {TaskOptions} options - Configuration options for the task
|
|
51
|
+
* @returns {Task} The created task instance
|
|
52
|
+
*/
|
|
53
|
+
public scheduleTask(callback: TaskCallback, options: TaskOptions): Task {
|
|
54
|
+
const taskId = Math.floor(Math.random() * 1000000);
|
|
55
|
+
const task = new Task(
|
|
56
|
+
this,
|
|
57
|
+
options.name,
|
|
58
|
+
taskId,
|
|
59
|
+
callback,
|
|
60
|
+
options.interval,
|
|
61
|
+
false,
|
|
62
|
+
options.once || false
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
this.scheduledTasks.set(taskId, task);
|
|
66
|
+
if (options.name) this.scheduledTasks.set(options.name, task);
|
|
67
|
+
|
|
68
|
+
if (options.immediate) {
|
|
69
|
+
task.execute(this.currentTimestamp);
|
|
70
|
+
task.lastExecutionTime = this.currentTimestamp;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return task;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Removes a task from the scheduler
|
|
78
|
+
* @param {number | string} identifier - The task's ID or name
|
|
79
|
+
*/
|
|
80
|
+
public removeTask(identifier: number | string): void {
|
|
81
|
+
const task = this.findTask(identifier);
|
|
82
|
+
if (!task) return;
|
|
83
|
+
|
|
84
|
+
this.scheduledTasks.delete(task.id);
|
|
85
|
+
if (task.name) this.scheduledTasks.delete(task.name);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Removes all tasks and stops the scheduler
|
|
90
|
+
*/
|
|
91
|
+
public clearAllTasks(): void {
|
|
92
|
+
if (this.schedulerInterval) {
|
|
93
|
+
clearInterval(this.schedulerInterval);
|
|
94
|
+
this.schedulerInterval = null;
|
|
95
|
+
}
|
|
96
|
+
this.scheduledTasks.clear();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Pauses the scheduler
|
|
101
|
+
*/
|
|
102
|
+
public pause(): void {
|
|
103
|
+
if (this.schedulerInterval) {
|
|
104
|
+
clearInterval(this.schedulerInterval);
|
|
105
|
+
this.schedulerInterval = null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Resumes the scheduler
|
|
111
|
+
*/
|
|
112
|
+
public resume(): void {
|
|
113
|
+
if (!this.schedulerInterval) {
|
|
114
|
+
this.startTaskProcessor();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Checks if the scheduler is paused
|
|
120
|
+
* @returns {boolean} True if the scheduler is paused, false otherwise
|
|
121
|
+
*/
|
|
122
|
+
public isPaused(): boolean {
|
|
123
|
+
return this.schedulerInterval === null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Executes a callback once
|
|
128
|
+
* @param {() => void} callback - The callback to execute
|
|
129
|
+
* @param {TaskOptions} options - Configuration options for the task
|
|
130
|
+
* @returns {Task} The created task instance
|
|
131
|
+
*/
|
|
132
|
+
public once(callback: () => void, options: TaskOptions): Task {
|
|
133
|
+
const task = this.scheduleTask(callback, { ...options, once: true });
|
|
134
|
+
return task;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Updates current timestamp and processes tasks
|
|
139
|
+
* @internal For testing purposes
|
|
140
|
+
*/
|
|
141
|
+
public processTasksNow(): void {
|
|
142
|
+
const previousTimestamp = this.currentTimestamp;
|
|
143
|
+
this.currentTimestamp = new Date();
|
|
144
|
+
|
|
145
|
+
for (const task of this.scheduledTasks.values()) {
|
|
146
|
+
const elapsedTime = task.getInterval();
|
|
147
|
+
const shouldRun =
|
|
148
|
+
task.shouldExecute(elapsedTime) &&
|
|
149
|
+
task.lastExecutionTime.getTime() <= previousTimestamp.getTime();
|
|
150
|
+
|
|
151
|
+
if (!shouldRun) continue;
|
|
152
|
+
|
|
153
|
+
task.execute(this.currentTimestamp);
|
|
154
|
+
if (task.shouldRemove()) {
|
|
155
|
+
this.removeTask(task.id);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Changes the processor interval
|
|
162
|
+
* @param {number} newInterval - The new interval in milliseconds
|
|
163
|
+
*/
|
|
164
|
+
public changeProcessorInterval(newInterval: number): void {
|
|
165
|
+
if (newInterval <= 0) throw new Error('Interval must be greater than 0');
|
|
166
|
+
|
|
167
|
+
this.processorInterval = newInterval;
|
|
168
|
+
|
|
169
|
+
if (this.schedulerInterval !== null) this.startTaskProcessor();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Starts the task processor that executes scheduled tasks
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
private startTaskProcessor(): void {
|
|
177
|
+
if (this.schedulerInterval) clearInterval(this.schedulerInterval);
|
|
178
|
+
|
|
179
|
+
this.schedulerInterval = setInterval(() => {
|
|
126
180
|
this.currentTimestamp = new Date();
|
|
127
|
-
|
|
181
|
+
|
|
128
182
|
for (const task of this.scheduledTasks.values()) {
|
|
129
183
|
const elapsedTime = this.currentTimestamp.getTime() - task.lastExecutionTime.getTime();
|
|
130
184
|
const shouldRun = task.shouldExecute(elapsedTime);
|
|
@@ -134,6 +188,6 @@ export class TaskScheduler {
|
|
|
134
188
|
task.execute(this.currentTimestamp);
|
|
135
189
|
if (task.shouldRemove()) this.removeTask(task.id);
|
|
136
190
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
191
|
+
}, this.processorInterval);
|
|
192
|
+
}
|
|
193
|
+
}
|