-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition-queue.js
More file actions
111 lines (104 loc) · 2.71 KB
/
partition-queue.js
File metadata and controls
111 lines (104 loc) · 2.71 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
const { EventEmitter } = require('events');
let dependencies;
class PartitionQueue extends EventEmitter {
constructor(options) {
super();
this.options = options || {};
this.concurrency = this.options.concurrency || 1;
this.timeout = this.options.timeout || 0;
this.autostart = this.options.autostart || false;
this.hashingFunction = this.options.hashingFunction || dependencies.hashString;
this.remaining = 0;
this.queues = [...new Array(this.concurrency)].map(() => []);
this.startPromiseResolve = null;
}
done() {
this.emit('done');
if (this.startPromiseResolve) {
this.startPromiseResolve();
this.startPromiseResolve = null;
}
}
/**
* Add a job
* @param {string} key The partition string to use. May be a non string when
* using a custom hashing function
* @param {function|promise} job A callback function, promise or async function.
*/
push(key, job) {
const {
concurrency,
queues,
autostart,
hashingFunction,
} = this;
const queueNumber = hashingFunction(key, concurrency);
const queue = queues[queueNumber];
queue.push(job);
this.remaining += 1;
if (!queue.running && autostart) {
this.next(queueNumber);
}
}
/** Start the queue/s */
start() {
const { queues } = this;
return new Promise((resolve) => {
this.startPromiseResolve = resolve;
if (!this.remaining) {
// empty queue
this.done();
return;
}
queues.forEach((queue, queueNumber) => {
if (!queue.running) {
this.next(queueNumber);
}
});
});
}
next(queueNumber) {
const { queues, timeout: timeoutMs } = this;
const queue = queues[queueNumber];
const job = queue.shift();
if (job) {
queue.running = true;
let timeout;
let doneCalled = false;
const done = (error, result) => {
if (error) { this.emit('error', error, job); }
if (timeout) { clearTimeout(timeout); }
if (doneCalled) return; // prevent a double call
doneCalled = true;
this.remaining -= 1;
if (!error) {
this.emit('success', result, job);
}
this.next(queueNumber);
};
timeout = timeoutMs ? (setTimeout(() => { this.emit('timeout'); done(new Error('Time Out')); }, timeoutMs)) : null;
// execute the job
try {
const promise = job(result => done(null, result), error => done(error));
if (promise) {
promise.then((result) => {
done(null, result);
}).catch((error) => {
done(error || new Error('Unknown Error when processing Job'));
});
}
} catch (error) {
done(error);
}
} else {
if (queue.running && this.remaining === 0) {
this.done();
}
queue.running = false;
}
}
}
module.exports = (dependenciesInjection) => {
dependencies = dependenciesInjection;
return PartitionQueue;
};