redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
139 lines • 6.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Queue = void 0;
const redis_smq_common_1 = require("redis-smq-common");
const redis_client_js_1 = require("../../common/redis-client/redis-client.js");
const scripts_js_1 = require("../../common/redis-client/scripts/scripts.js");
const redis_keys_js_1 = require("../../common/redis-keys/redis-keys.js");
const index_js_1 = require("../../config/index.js");
const index_js_2 = require("../event-bus/index.js");
const _delete_queue_js_1 = require("./_/_delete-queue.js");
const _get_queue_properties_js_1 = require("./_/_get-queue-properties.js");
const _get_queues_js_1 = require("./_/_get-queues.js");
const _parse_queue_params_js_1 = require("./_/_parse-queue-params.js");
const _queue_exists_js_1 = require("./_/_queue-exists.js");
const index_js_3 = require("./errors/index.js");
const index_js_4 = require("./types/index.js");
class Queue {
constructor() {
this.shutdown = (cb) => {
redis_smq_common_1.async.waterfall([this.redisClient.shutdown, this.eventBus.shutdown], cb);
};
this.logger = redis_smq_common_1.logger.getLogger(index_js_1.Configuration.getSetConfig().logger, `queue`);
this.eventBus = new index_js_2.EventBus();
this.eventBus.on('error', (err) => this.logger.error(err));
this.redisClient = new redis_client_js_1.RedisClient();
this.redisClient.on('error', (err) => this.logger.error(err));
}
save(queue, queueType, deliveryModel, cb) {
const queueParams = (0, _parse_queue_params_js_1._parseQueueParams)(queue);
if (queueParams instanceof Error)
return cb(queueParams);
this.redisClient.getSetInstance((err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
const { keyQueueProperties } = redis_keys_js_1.redisKeys.getQueueKeys(queueParams, null);
const { keyNamespaces, keyQueues } = redis_keys_js_1.redisKeys.getMainKeys();
const { keyNamespaceQueues } = redis_keys_js_1.redisKeys.getNamespaceKeys(queueParams.ns);
const queueParamsStr = JSON.stringify(queueParams);
client.runScript(scripts_js_1.ELuaScriptName.CREATE_QUEUE, [keyNamespaces, keyNamespaceQueues, keyQueues, keyQueueProperties], [
queueParams.ns,
queueParamsStr,
index_js_4.EQueueProperty.QUEUE_TYPE,
queueType,
index_js_4.EQueueProperty.DELIVERY_MODEL,
deliveryModel,
], (err, reply) => {
if (err)
return cb(err);
if (!reply)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
if (reply !== 'OK')
return cb(new index_js_3.QueueQueueExistsError());
this.getProperties(queueParams, (err, properties) => {
if (err)
return cb(err);
if (!properties)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
this.eventBus.getSetInstance((err, instance) => {
if (err)
return cb(err);
instance === null || instance === void 0 ? void 0 : instance.emit('queue.queueCreated', queueParams, properties);
cb(null, { queue: queueParams, properties });
});
});
});
});
}
exists(queue, cb) {
const queueParams = (0, _parse_queue_params_js_1._parseQueueParams)(queue);
if (queueParams instanceof Error)
cb(queueParams);
else {
this.redisClient.getSetInstance((err, client) => {
if (err)
cb(err);
else if (!client)
cb(new redis_smq_common_1.CallbackEmptyReplyError());
else
(0, _queue_exists_js_1._queueExists)(client, queueParams, cb);
});
}
}
delete(queue, cb) {
const queueParams = (0, _parse_queue_params_js_1._parseQueueParams)(queue);
if (queueParams instanceof Error)
return cb(queueParams);
this.redisClient.getSetInstance((err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
(0, _delete_queue_js_1._deleteQueue)(client, queueParams, undefined, (err, multi) => {
if (err)
return cb(err);
if (!multi)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
multi.exec((err) => {
if (err)
return cb(err);
this.eventBus.getSetInstance((err, instance) => {
if (err)
return cb(err);
instance === null || instance === void 0 ? void 0 : instance.emit('queue.queueDeleted', queueParams);
cb();
});
});
});
});
}
getProperties(queue, cb) {
const queueParams = (0, _parse_queue_params_js_1._parseQueueParams)(queue);
if (queueParams instanceof Error)
cb(queueParams);
else {
this.redisClient.getSetInstance((err, client) => {
if (err)
cb(err);
else if (!client)
cb(new redis_smq_common_1.CallbackEmptyReplyError());
else
(0, _get_queue_properties_js_1._getQueueProperties)(client, queueParams, cb);
});
}
}
getQueues(cb) {
this.redisClient.getSetInstance((err, client) => {
if (err)
cb(err);
else if (!client)
cb(new redis_smq_common_1.CallbackEmptyReplyError());
else
(0, _get_queues_js_1._getQueues)(client, cb);
});
}
}
exports.Queue = Queue;
//# sourceMappingURL=queue.js.map