/
queue.js
168 lines (150 loc) · 3.86 KB
/
queue.js
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
"use strict";
const logger = require("./logger");
function _backoff(retryNumber, delay) {
return new Promise(function(resolve) {
setTimeout(resolve, delay * Math.pow(2, retryNumber));
});
}
class Queue {
constructor(options) {
this.name = options.name || "queue";
this.concurrency = options.concurrency || 1;
this.handler =
options.handler ||
function(task) {
return task();
};
this.cursor = 0;
this.active = 0;
this.complete = 0;
this.success = 0;
this.errored = 0;
this.retried = 0;
this.tasks = [];
this.waits = [];
this.min = 9999999999;
this.max = 0;
this.avg = 0;
this.retries = options.retries || 0;
this.backoff = typeof options.backoff == "number" ? options.backoff : 200;
this.retryCounts = {};
this.closed = false;
this.finished = false;
}
taskName(cursorIndex) {
const task = this.tasks[cursorIndex];
return typeof task === "string" ? task : "index " + cursorIndex;
}
wait() {
const self = this;
const p = new Promise(function(resolve, reject) {
self.waits.push({ resolve: resolve, reject: reject });
});
return p;
}
add(task) {
if (!this.startTime) {
this.startTime = Date.now();
}
if (this.closed) {
throw new Error("Cannot add a task to a closed queue.");
}
this.tasks.push(task);
this.process();
}
close() {
this.closed = true;
this._finishIfIdle();
}
process() {
if (
this._finishIfIdle() ||
this.active >= this.concurrency ||
this.cursor === this.tasks.length
) {
return;
}
this.cursor++;
this.active++;
this.handle(this.cursor - 1);
}
handle(cursorIndex) {
const task = this.tasks[cursorIndex];
const t0 = Date.now();
const self = this;
this.handler(task)
.then(function() {
const dt = Date.now() - t0;
if (dt < self.min) {
self.min = dt;
}
if (dt > self.max) {
self.max = dt;
}
self.avg = (self.avg * self.complete + dt) / (self.complete + 1);
self.success++;
self.complete++;
self.active--;
self.process();
})
.catch(function(err) {
if (self.retries > 0) {
self.retryCounts[cursorIndex] = self.retryCounts[cursorIndex] || 0;
if (self.retryCounts[cursorIndex] < self.retries) {
self.retryCounts[cursorIndex]++;
self.retried++;
return _backoff(self.retryCounts[cursorIndex], self.backoff).then(function() {
logger.debug("[" + self.name + "] Retrying task", self.taskName(cursorIndex));
return self.handle(cursorIndex);
});
}
}
self.errored++;
self.complete++;
self.active--;
if (self.retryCounts[cursorIndex] > 0) {
logger.debug(
"[" + self.name + "] Retries exhausted for task",
self.taskName(cursorIndex),
":",
err
);
} else {
logger.debug("[" + self.name + "] Error on task", self.taskName(cursorIndex), ":", err);
}
self._finish(err);
});
}
stats() {
return {
max: this.max,
min: this.min,
avg: this.avg,
active: this.active,
complete: this.complete,
success: this.success,
errored: this.errored,
retried: this.retried,
total: this.tasks.length,
elapsed: Date.now() - this.startTime,
};
}
_finishIfIdle() {
if (this.closed && this.cursor == this.tasks.length && this.active === 0) {
this._finish();
return true;
}
return false;
}
_finish(err) {
var self = this;
this.waits.forEach(function(p) {
if (err) {
return p.reject(err);
}
self.finished = true;
return p.resolve();
});
}
}
module.exports = Queue;