forked from HowProgrammingWorks/ConcurrentQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-timeouts.js
More file actions
132 lines (115 loc) · 2.77 KB
/
2-timeouts.js
File metadata and controls
132 lines (115 loc) · 2.77 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
'use strict';
class Queue {
constructor(concurrency) {
this.concurrency = concurrency;
this.count = 0;
this.waiting = [];
this.onProcess = null;
this.onDone = null;
this.onSuccess = null;
this.onFailure = null;
this.onDrain = null;
this.waitTimeout = Infinity;
this.processTimeout = Infinity;
}
static channels(concurrency) {
return new Queue(concurrency);
}
wait(msec) {
this.waitTimeout = msec;
return this;
}
timeout(msec) {
this.processTimeout = msec;
return this;
}
add(task) {
const hasChannel = this.count < this.concurrency;
if (hasChannel) {
this.next(task);
return;
}
this.waiting.push({ task, start: Date.now() });
}
next(task) {
this.count++;
let timer = null;
let finished = false;
const { processTimeout, onProcess } = this;
const finish = (err, res) => {
if (finished) return;
finished = true;
if (timer) clearTimeout(timer);
this.count--;
this.finish(err, res);
if (this.waiting.length > 0) this.takeNext();
};
if (processTimeout !== Infinity) {
timer = setTimeout(() => {
timer = null;
const err = new Error('Process timed out');
finish(err, task);
}, processTimeout);
}
onProcess(task, finish);
}
takeNext() {
const { waiting, waitTimeout } = this;
const { task, start } = waiting.shift();
if (waitTimeout !== Infinity) {
const delay = Date.now() - start;
if (delay > waitTimeout) {
const err = new Error('Waiting timed out');
this.finish(err, task);
if (waiting.length > 0) this.takeNext();
return;
}
}
this.next(task);
return;
}
finish(err, res) {
const { onFailure, onSuccess, onDone, onDrain } = this;
if (err) {
if (onFailure) onFailure(err, res);
} else if (onSuccess) {
onSuccess(res);
}
if (onDone) onDone(err, res);
if (this.count === 0 && onDrain) onDrain();
}
process(listener) {
this.onProcess = listener;
return this;
}
done(listener) {
this.onDone = listener;
return this;
}
success(listener) {
this.onSuccess = listener;
return this;
}
failure(listener) {
this.onFailure = listener;
return this;
}
drain(listener) {
this.onDrain = listener;
return this;
}
}
// Usage
const job = (task, next) => {
setTimeout(next, task.interval, null, task);
};
const queue = Queue.channels(3)
.wait(4000)
.timeout(5000)
.process(job)
.success(task => console.log(`Success: ${task.name}`))
.failure((err, task) => console.log(`Failure: ${err} ${task.name}`))
.drain(() => console.log('Queue drain'));
for (let i = 0; i < 10; i++) {
queue.add({ name: `Task${i}`, interval: i * 1000 });
}