-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (129 loc) · 4.9 KB
/
index.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
const crypto = require('crypto');
const lua = require('./lua.json');
function uuid(placeholder) {
if (placeholder) {
return (placeholder ^ (crypto.randomBytes(1)[0] % 16) >> placeholder / 4).toString(16);
} else {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid);
}
}
function BQueue(redisClient, queueName = 'bqueue', queueCount = 1) {
if (typeof queueName !== 'string') {
throw new Error('Queue name must be a string!');
} else if (!Number.isInteger(queueCount)) {
throw new Error('Queue count must be an integer!');
} else if (queueCount < 1 || queueCount > 1000) {
throw new Error('Queue count must be between 1 and 1000!');
}
this.redisClient = redisClient;
this.queueName = queueName;
this.queueCount = queueCount;
this.redisClient.defineCommand('getBatch', {
numberOfKeys: 1,
lua: lua.getBatch
});
this.redisClient.defineCommand('removeMessages', {
numberOfKeys: 1,
lua: lua.removeMessages
});
this.redisClient.defineCommand('reinsertUnprocessed', {
numberOfKeys: 1,
lua: lua.reinsertUnprocessed
});
}
BQueue.prototype.pushMessage = function(message = '', queueNumber = Math.floor(Math.random() * this.queueCount)) {
return new Promise((resolve, reject) => {
if (!Number.isInteger(queueNumber)) {
return reject(Error('Queue number must be an integer!'));
} else if (queueNumber < 0 || queueNumber >= this.queueCount) {
return reject(Error('Queue number must be between 0 and ' + this.queueCount - 1 + '!'));
}
const queue = this.queueName + ':' + queueNumber;
const id = uuid();
const record = JSON.stringify({
id: id,
message: message
});
const queueKey = '{' + queue + '}:messages';
const messageKey = queueKey + ':' + id;
this.redisClient.pipeline().set(messageKey, record).lpush(queueKey, id).exec(err => {
if (err) {
return reject(err);
} else {
return resolve({
name: this.queueName,
number: queueNumber,
id: id
});
}
});
});
};
BQueue.prototype.getBatch = function(maxBatchSize = 1, processingTimeout = 5000, queueNumber = Math.floor(Math.random() * this.queueCount)) {
return new Promise((resolve, reject) => {
if (!Number.isInteger(maxBatchSize)) {
return reject(Error('Max batch size must be an integer!'));
} else if (maxBatchSize < 1 || maxBatchSize > 10000) {
return reject(Error('Max batch sizee must be between 1 and 10000!'));
} else if (!Number.isInteger(processingTimeout)) {
return reject(Error('Processing timeout must be an integer!'));
} else if (processingTimeout < 1000 || processingTimeout > 604800000) {
return reject(Error('Processing timeout must be between 1000 and 604800000!'));
} else if (!Number.isInteger(queueNumber)) {
return reject(Error('Queue number must be an integer!'));
} else if (queueNumber < 0 || queueNumber >= this.queueCount) {
return reject(Error('Queue number must be between 0 and ' + this.queueCount - 1 + '!'));
}
const queue = this.queueName + ':' + queueNumber;
this.redisClient.getBatch(queue, maxBatchSize, processingTimeout, (err, results) => {
if (err) {
return reject(err);
} else {
const messages = results.map(value => JSON.parse(value));
return resolve({
name: this.queueName,
number: queueNumber,
messages: messages,
remove: () => {
return new Promise((resolve, reject) => {
const ids = messages.filter(message => message).map(message => message.id);
this.redisClient.removeMessages(queue, ...ids, (err, results) => {
if (err) {
return reject(err);
} else {
return resolve(results);
}
});
});
}
});
}
});
});
};
BQueue.prototype.reinsertUnprocessed = function(maxMessages = 1000, queueNumber = Math.floor(Math.random() * this.queueCount)) {
return new Promise((resolve, reject) => {
if (!Number.isInteger(maxMessages)) {
return reject(Error('Max messages must be an integer!'));
} else if (maxMessages < 1 || maxMessages > 10000) {
return reject(Error('Max messages must be between 1 and 10000!'));
} else if (!Number.isInteger(queueNumber)) {
return reject(Error('Queue number must be an integer!'));
} else if (queueNumber < 0 || queueNumber >= this.queueCount) {
return reject(Error('Queue number must be between 0 and ' + this.queueCount - 1 + '!'));
}
const queue = this.queueName + ':' + queueNumber;
this.redisClient.reinsertUnprocessed(queue, maxMessages, (err, result) => {
if (err) {
return reject(err);
} else {
return resolve({
name: this.queueName,
number: queueNumber,
ids: result
});
}
});
});
};
module.exports = BQueue;