redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
61 lines • 2.09 kB
JavaScript
import { CallbackEmptyReplyError, createRedisClient, EventEmitter, PanicError, } from 'redis-smq-common';
import { Configuration } from '../../config/index.js';
import { RedisClientInstanceLockError } from './errors/redis-client-instance-lock.error.js';
import { loadScriptFiles } from './scripts/scripts.js';
export class RedisClient extends EventEmitter {
instance = null;
locked = false;
createClient(config, cb) {
createRedisClient(config, (err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new CallbackEmptyReplyError());
loadScriptFiles(client, (err) => {
if (err)
return cb(err);
cb(null, client);
});
});
}
init = (cb) => {
this.getSetInstance((err) => cb(err));
};
getSetInstance = (cb) => {
if (!this.locked) {
if (!this.instance) {
this.locked = true;
this.createClient(Configuration.getSetConfig().redis, (err, client) => {
this.locked = false;
if (err)
return cb(err);
if (!client)
return cb(new CallbackEmptyReplyError());
this.instance = client;
this.instance.on('error', (err) => this.emit('error', err));
cb(null, this.instance);
});
}
else
cb(null, this.instance);
}
else
cb(new RedisClientInstanceLockError());
};
shutdown = (cb) => {
if (this.instance) {
this.instance.halt(() => {
this.instance = null;
cb();
});
}
else
cb();
};
getInstance() {
if (!this.instance)
return new PanicError(`Use first getSetInstance() to initialize the RedisClientInstance class`);
return this.instance;
}
}
//# sourceMappingURL=redis-client.js.map