redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
75 lines • 3.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueueMessagesStorageList = void 0;
const redis_smq_common_1 = require("redis-smq-common");
const redis_keys_js_1 = require("../../../common/redis-keys/redis-keys.js");
const queue_messages_storage_js_1 = require("./queue-messages-storage.js");
class QueueMessagesStorageList extends queue_messages_storage_js_1.QueueMessagesStorage {
count(queue, redisKey, cb) {
const keys = redis_keys_js_1.redisKeys.getQueueKeys(queue.queueParams, queue.groupId);
this.redisClient.getSetInstance((err, client) => {
if (err)
cb(err);
else if (!client)
cb(new redis_smq_common_1.CallbackEmptyReplyError());
else {
client.llen(keys[redisKey], cb);
}
});
}
fetchItems(queue, redisKey, offset, limit, cb) {
const keys = redis_keys_js_1.redisKeys.getQueueKeys(queue.queueParams, queue.groupId);
this.redisClient.getSetInstance((err, client) => {
if (err)
cb(err);
else if (!client)
cb(new redis_smq_common_1.CallbackEmptyReplyError());
else {
client.lrange(keys[redisKey], offset, offset + limit - 1, (err, items) => {
if (err)
cb(err);
else
cb(null, items || []);
});
}
});
}
fetchAllItems(queue, redisKey, cb) {
const chunkSize = 100;
this.redisClient.getSetInstance((err, client) => {
if (err)
cb(err);
else if (!client)
cb(new redis_smq_common_1.CallbackEmptyReplyError());
else {
const keys = redis_keys_js_1.redisKeys.getQueueKeys(queue.queueParams, queue.groupId);
const key = keys[redisKey];
client.llen(key, (err, reply) => {
if (err)
return cb(err);
const totalLength = Number(reply);
if (totalLength === 0)
return cb(null, []);
const allItems = [];
const processChunk = (offset) => {
if (offset >= totalLength) {
return cb(null, allItems);
}
const end = Math.min(offset + chunkSize - 1, totalLength - 1);
client.lrange(key, offset, end, (err, items) => {
if (err)
return cb(err);
if (!items || items.length === 0)
return cb(null, allItems);
allItems.push(...items);
setImmediate(() => processChunk(offset + chunkSize));
});
};
processChunk(0);
});
}
});
}
}
exports.QueueMessagesStorageList = QueueMessagesStorageList;
//# sourceMappingURL=queue-message-storage-list.js.map
;