-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathTaskSchedularSystem.js
More file actions
347 lines (297 loc) · 8.54 KB
/
Copy pathTaskSchedularSystem.js
File metadata and controls
347 lines (297 loc) · 8.54 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Advanced Task Scheduler with Priority Queue, Event System, and State Management
class PriorityQueue {
constructor(comparator = (a, b) => a.priority - b.priority) {
this.heap = [];
this.comparator = comparator;
}
push(item) {
this.heap.push(item);
this.bubbleUp(this.heap.length - 1);
}
pop() {
if (this.isEmpty()) return null;
const top = this.heap[0];
const bottom = this.heap.pop();
if (!this.isEmpty()) {
this.heap[0] = bottom;
this.bubbleDown(0);
}
return top;
}
peek() {
return this.isEmpty() ? null : this.heap[0];
}
isEmpty() {
return this.heap.length === 0;
}
bubbleUp(idx) {
while (idx > 0) {
const parent = Math.floor((idx - 1) / 2);
if (this.comparator(this.heap[idx], this.heap[parent]) >= 0) break;
[this.heap[idx], this.heap[parent]] = [this.heap[parent], this.heap[idx]];
idx = parent;
}
}
bubbleDown(idx) {
const len = this.heap.length;
while (true) {
let smallest = idx;
const left = 2 * idx + 1;
const right = 2 * idx + 2;
if (left < len && this.comparator(this.heap[left], this.heap[smallest]) < 0) {
smallest = left;
}
if (right < len && this.comparator(this.heap[right], this.heap[smallest]) < 0) {
smallest = right;
}
if (smallest === idx) break;
[this.heap[idx], this.heap[smallest]] = [this.heap[smallest], this.heap[idx]];
idx = smallest;
}
}
}
class EventEmitter {
constructor() {
this.events = new Map();
}
on(event, listener) {
if (!this.events.has(event)) {
this.events.set(event, []);
}
this.events.get(event).push(listener);
return () => this.off(event, listener);
}
off(event, listener) {
if (!this.events.has(event)) return;
const listeners = this.events.get(event);
const idx = listeners.indexOf(listener);
if (idx > -1) listeners.splice(idx, 1);
}
emit(event, ...args) {
if (!this.events.has(event)) return;
this.events.get(event).forEach(listener => listener(...args));
}
once(event, listener) {
const wrapper = (...args) => {
listener(...args);
this.off(event, wrapper);
};
this.on(event, wrapper);
}
}
class Task {
static idCounter = 0;
constructor(name, fn, options = {}) {
this.id = Task.idCounter++;
this.name = name;
this.fn = fn;
this.priority = options.priority || 0;
this.delay = options.delay || 0;
this.retries = options.retries || 0;
this.maxRetries = options.retries || 0;
this.timeout = options.timeout || 5000;
this.dependencies = options.dependencies || [];
this.status = 'pending';
this.result = null;
this.error = null;
this.startTime = null;
this.endTime = null;
}
async execute() {
this.status = 'running';
this.startTime = Date.now();
try {
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Task timeout')), this.timeout)
);
this.result = await Promise.race([
Promise.resolve(this.fn()),
timeoutPromise
]);
this.status = 'completed';
this.endTime = Date.now();
return this.result;
} catch (error) {
if (this.retries > 0) {
this.retries--;
this.status = 'retrying';
await new Promise(resolve => setTimeout(resolve, 1000));
return this.execute();
}
this.error = error;
this.status = 'failed';
this.endTime = Date.now();
throw error;
}
}
getDuration() {
return this.endTime && this.startTime ? this.endTime - this.startTime : null;
}
}
class TaskScheduler extends EventEmitter {
constructor(config = {}) {
super();
this.maxConcurrent = config.maxConcurrent || 3;
this.running = 0;
this.queue = new PriorityQueue();
this.completed = new Map();
this.failed = new Map();
this.taskGraph = new Map();
this.isProcessing = false;
this.stats = {
totalTasks: 0,
completedTasks: 0,
failedTasks: 0,
totalDuration: 0
};
}
addTask(name, fn, options = {}) {
const task = new Task(name, fn, options);
this.queue.push(task);
this.taskGraph.set(task.id, task);
this.stats.totalTasks++;
this.emit('taskAdded', task);
if (!this.isProcessing) {
this.process();
}
return task.id;
}
addBulkTasks(tasks) {
return tasks.map(({ name, fn, options }) => this.addTask(name, fn, options));
}
async process() {
if (this.isProcessing) return;
this.isProcessing = true;
while (!this.queue.isEmpty() || this.running > 0) {
while (this.running < this.maxConcurrent && !this.queue.isEmpty()) {
const task = this.queue.pop();
if (!this.checkDependencies(task)) {
this.queue.push(task);
await new Promise(resolve => setTimeout(resolve, 100));
continue;
}
this.running++;
this.emit('taskStarted', task);
if (task.delay > 0) {
await new Promise(resolve => setTimeout(resolve, task.delay));
}
this.executeTask(task);
}
await new Promise(resolve => setTimeout(resolve, 50));
}
this.isProcessing = false;
this.emit('allTasksCompleted', this.getStats());
}
checkDependencies(task) {
return task.dependencies.every(depId => {
const dep = this.taskGraph.get(depId);
return dep && dep.status === 'completed';
});
}
async executeTask(task) {
try {
const result = await task.execute();
this.completed.set(task.id, task);
this.stats.completedTasks++;
this.stats.totalDuration += task.getDuration();
this.emit('taskCompleted', task, result);
} catch (error) {
this.failed.set(task.id, task);
this.stats.failedTasks++;
this.emit('taskFailed', task, error);
} finally {
this.running--;
}
}
getTask(taskId) {
return this.taskGraph.get(taskId);
}
getStats() {
return {
...this.stats,
averageDuration: this.stats.completedTasks > 0
? this.stats.totalDuration / this.stats.completedTasks
: 0,
successRate: this.stats.totalTasks > 0
? (this.stats.completedTasks / this.stats.totalTasks) * 100
: 0
};
}
cancelTask(taskId) {
const task = this.taskGraph.get(taskId);
if (task && task.status === 'pending') {
task.status = 'cancelled';
this.emit('taskCancelled', task);
return true;
}
return false;
}
async waitForCompletion() {
return new Promise(resolve => {
if (!this.isProcessing && this.running === 0) {
resolve(this.getStats());
} else {
this.once('allTasksCompleted', resolve);
}
});
}
}
// Demo Usage
(async () => {
const scheduler = new TaskScheduler({ maxConcurrent: 2 });
// Event listeners
scheduler.on('taskCompleted', (task, result) => {
console.log(`✓ ${task.name} completed in ${task.getDuration()}ms:`, result);
});
scheduler.on('taskFailed', (task, error) => {
console.log(`✗ ${task.name} failed:`, error.message);
});
scheduler.on('allTasksCompleted', stats => {
console.log('\n=== Scheduler Stats ===');
console.log(`Total: ${stats.totalTasks}`);
console.log(`Completed: ${stats.completedTasks}`);
console.log(`Failed: ${stats.failedTasks}`);
console.log(`Success Rate: ${stats.successRate.toFixed(2)}%`);
console.log(`Average Duration: ${stats.averageDuration.toFixed(2)}ms`);
});
// Add complex tasks
const task1 = scheduler.addTask(
'Fetch User Data',
async () => {
await new Promise(r => setTimeout(r, 500));
return { id: 1, name: 'Alice' };
},
{ priority: 1 }
);
const task2 = scheduler.addTask(
'Process Analytics',
async () => {
await new Promise(r => setTimeout(r, 800));
return { views: 1000, clicks: 50 };
},
{ priority: 2, retries: 2 }
);
scheduler.addTask(
'Generate Report',
async () => {
await new Promise(r => setTimeout(r, 300));
return 'Report Generated';
},
{ priority: 3, dependencies: [task1, task2] }
);
scheduler.addTask(
'Flaky Task',
async () => {
if (Math.random() > 0.5) throw new Error('Random failure');
return 'Success';
},
{ retries: 3, timeout: 2000 }
);
// Bulk add
scheduler.addBulkTasks([
{ name: 'Task A', fn: async () => 'A done', options: { delay: 200 } },
{ name: 'Task B', fn: async () => 'B done', options: { delay: 150 } },
{ name: 'Task C', fn: async () => 'C done', options: { delay: 100 } }
]);
await scheduler.waitForCompletion();
})();