redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
112 lines • 4.94 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Namespace = void 0;
const redis_smq_common_1 = require("redis-smq-common");
const redis_client_js_1 = require("../../common/redis-client/redis-client.js");
const redis_keys_js_1 = require("../../common/redis-keys/redis-keys.js");
const index_js_1 = require("../../config/index.js");
const _delete_queue_js_1 = require("../queue/_/_delete-queue.js");
const _get_queues_js_1 = require("../queue/_/_get-queues.js");
const index_js_2 = require("./errors/index.js");
class Namespace {
constructor() {
this.shutdown = (cb) => {
this.redisClient.shutdown(cb);
};
this.logger = redis_smq_common_1.logger.getLogger(index_js_1.Configuration.getSetConfig().logger, `exchange-fan-out-manager`);
this.redisClient = new redis_client_js_1.RedisClient();
this.redisClient.on('error', (err) => this.logger.error(err));
}
getNamespaces(cb) {
this.redisClient.getSetInstance((err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
const { keyNamespaces } = redis_keys_js_1.redisKeys.getMainKeys();
client.smembers(keyNamespaces, (err, reply) => {
if (err)
return cb(err);
if (!reply)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
cb(null, reply);
});
});
}
getNamespaceQueues(namespace, cb) {
const ns = redis_keys_js_1.redisKeys.validateRedisKey(namespace);
if (ns instanceof Error)
return cb(new index_js_2.NamespaceInvalidNamespaceError());
this.redisClient.getSetInstance((err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
const { keyNamespaces } = redis_keys_js_1.redisKeys.getMainKeys();
const { keyNamespaceQueues } = redis_keys_js_1.redisKeys.getNamespaceKeys(ns);
redis_smq_common_1.async.waterfall([
(cb) => {
client.sismember(keyNamespaces, ns, (err, reply) => {
if (err)
return cb(err);
if (!reply)
return cb(new index_js_2.NamespaceNotFoundError());
cb();
});
},
(cb) => {
client.smembers(keyNamespaceQueues, (err, reply) => {
if (err)
return cb(err);
if (!reply)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
const messageQueues = reply.map((i) => JSON.parse(i));
cb(null, messageQueues);
});
},
], cb);
});
}
delete(namespace, cb) {
const ns = redis_keys_js_1.redisKeys.validateRedisKey(namespace);
if (ns instanceof Error)
return cb(new index_js_2.NamespaceInvalidNamespaceError());
this.redisClient.getSetInstance((err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
const { keyNamespaces } = redis_keys_js_1.redisKeys.getMainKeys();
redis_smq_common_1.async.waterfall([
(cb) => {
client.sismember(keyNamespaces, ns, (err, isMember) => {
if (err)
return cb(err);
if (!isMember)
return cb(new index_js_2.NamespaceNotFoundError());
cb();
});
},
], (err) => {
if (err)
return cb(err);
(0, _get_queues_js_1._getQueues)(client, (err, reply) => {
if (err)
return cb(err);
const queues = reply !== null && reply !== void 0 ? reply : [];
const multi = client.multi();
multi.srem(keyNamespaces, ns);
redis_smq_common_1.async.eachOf(queues, (queueParams, _, done) => {
(0, _delete_queue_js_1._deleteQueue)(client, queueParams, multi, (err) => done(err));
}, (err) => {
if (err)
return cb(err);
multi.exec((err) => cb(err));
});
});
});
});
}
}
exports.Namespace = Namespace;
//# sourceMappingURL=namespace.js.map
;