Repository navigation
Expand file tree
/
Copy pathextra.js
More file actions
187 lines (168 loc) · 4.16 KB
/
Copy pathextra.js
File metadata and controls
187 lines (168 loc) · 4.16 KB
1
2
3
4
5
6
7
8
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Represents a scheduled task.
*/
class Schedule {
constructor(schedule){
this.schedule = schedule;
}
/**
* Returns the schedule ID.
* @returns {number}
*/
get id(){
return this.schedule;
}
/**
* Clear a scheduled task.
* @returns {boolean}
*/
delete(){
clearInterval(this.schedule);
return true;
}
}
class Queue {
constructor (){
this.queue = [];
this.standby = [];
this.running = false;
}
/**
* Get queue length.
* @returns {number}
*/
get length() {
return this.queue.length;
}
/**
* Queue a task to run sequentially.
*
* For example:
*
* ```
* const queue = new Queue();
* queue.addTask(async () => {
* const response = await fetch(...);
* return response.ok ? "ok" : "error";
* });
* ```
*
* @param {() => Promise<void>} task
*/
addTask(task){
this.queue.push(task);
return this;
}
/**
* Queue an array of tasks to run sequentially.
*
* @param {(() => Promise<void>)[]} tasks
*/
addTasks(tasks){
for (const task of tasks){
this.queue.push(task);
}
return this;
}
/**
* Put a task on standby for later processing.
* @param {() => Promise<void>} task
* @returns
*/
addStandby(task){
this.standby.push(task);
return this;
}
/**
* Approve one task on standby. This method pushes the standby task to the main queue.
* @param {number} index
* @returns
*/
approveTaskById(index = 0){
if (this.standby.length === 0) return;
if (index < 0 || index >= this.standby.length) return;
const task = this.standby.splice(index, 1)[0];
this.queue.push(task);
console.log("Standby task pushed to main queue...");
return this;
}
/**
* Approve all tasks on standby. This method pushes the standby tasks to the main queue.
* @returns
*/
approveAllTasks(){
if (this.standby.length === 0) return;
this.queue.push(...this.standby);
this.standby = [];
console.log("All standby tasks pushed to main queue...");
return this;
}
/**
* Remove a standby task by index.
* @param {number} index
* @returns
*/
clearStandby(index = 0){
if (this.standby.length === 0) return;
if (index < 0 || index >= this.standby.length) return;
this.standby.splice(index, 1)[0];
console.log(`Standby task ${index} removed...`);
return this;
}
/**
* Remove all standby tasks.
* @returns
*/
clearAllStandby(){
this.standby = [];
return this;
}
/**
* Start running tasks sequentially.
*/
async processQueue() {
console.log({
running: this.running,
queue: this.queue.length,
standby: this.standby.length
});
if (this.running || this.queue.length === 0) return;
this.running = true;
try {
while (this.queue.length > 0) {
console.log(`Processing task. Remaining: ${this.queue.length}`);
const task = this.queue.shift();
try {
await this.withTimeout(task(), 30000);
} catch (error) {
console.error("Queue task failed:", error);
}
}
} finally {
this.running = false;
if (this.queue.length > 0) {
this.processQueue();
}
}
}
async withTimeout(promise, timeout = 30000) {
let timer;
const timeoutPromise = new Promise((_, reject) => {
timer = setTimeout(() => {
reject(new Error("Task timeout"));
}, timeout);
});
try {
return await Promise.race([
promise,
timeoutPromise
]);
} finally {
clearTimeout(timer);
}
}
}
export {
Schedule,
Queue
}