redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
96 lines • 5.03 kB
JavaScript
import { async, CallbackEmptyReplyError, createLogger, } from 'redis-smq-common';
import { EQueueOperationalState, } from '../queue-manager/index.js';
import { Configuration } from '../config-manager/configuration.js';
import { withSharedPoolConnection } from '../common/redis/redis-connection-pool/with-shared-pool-connection.js';
import { _parseQueueParamsAndValidate } from '../queue-manager/_/_parse-queue-params-and-validate.js';
import { redisKeys } from '../common/redis/redis-keys/redis-keys.js';
import { EStateTransitionReason, } from './types/index.js';
import { _getQueueState } from './_/_get-queue-state.js';
import { _transitQueueTo } from './_/_transit-queue-to.js';
export class QueueStateManager {
logger;
constructor() {
const config = Configuration.getConfig();
this.logger = createLogger(config.logger, this.constructor.name);
}
getState(queue, cb) {
return async.withOptionalCallback(cb, (callback) => {
const queueDesc = typeof queue === 'string' ? queue : `${queue.name}@${queue.ns}`;
this.logger.debug(`Getting operational state for queue: ${queueDesc}`);
withSharedPoolConnection((client, done) => {
_parseQueueParamsAndValidate(client, queue, (err, queueParams) => {
if (err)
return done(err);
if (!queueParams)
return done(new CallbackEmptyReplyError());
_getQueueState(client, queueParams, (err, stateTransition) => {
if (err) {
this.logger.error(`Error getting state for ${queueDesc}: ${err.message}`, err);
return done(err);
}
if (!stateTransition)
return done(new CallbackEmptyReplyError());
this.logger.debug(`Queue ${queueDesc} state: ${EQueueOperationalState[stateTransition.to]}`);
done(null, stateTransition);
});
});
}, callback);
});
}
pause(queue, options, cb) {
return async.withOptionalCallback(cb, (callback) => {
const queueDesc = typeof queue === 'string' ? queue : `${queue.name}@${queue.ns}`;
this.logger.debug(`Pausing queue: ${queueDesc}`);
_transitQueueTo(queue, EQueueOperationalState.PAUSED, {
...options,
reason: options?.reason || EStateTransitionReason.MANUAL,
description: options?.description || 'Manual pause',
}, this.logger, callback);
});
}
resume(queue, options, cb) {
return async.withOptionalCallback(cb, (callback) => {
const queueDesc = typeof queue === 'string' ? queue : `${queue.name}@${queue.ns}`;
this.logger.debug(`Resuming queue: ${queueDesc}`);
_transitQueueTo(queue, EQueueOperationalState.ACTIVE, {
...options,
reason: options?.reason || EStateTransitionReason.MANUAL,
description: options?.description || 'Manual resume',
}, this.logger, callback);
});
}
stop(queue, options, cb) {
return async.withOptionalCallback(cb, (callback) => {
const queueDesc = typeof queue === 'string' ? queue : `${queue.name}@${queue.ns}`;
this.logger.debug(`Stopping queue: ${queueDesc}`);
_transitQueueTo(queue, EQueueOperationalState.STOPPED, {
...options,
reason: options?.reason || EStateTransitionReason.MANUAL,
description: options?.description || 'Manual stop',
}, this.logger, callback);
});
}
getStateHistory(queue, cb) {
return async.withOptionalCallback(cb, (callback) => {
const queueDesc = typeof queue === 'string' ? queue : `${queue.name}@${queue.ns}`;
this.logger.debug(`Getting state history for queue: ${queueDesc}`);
withSharedPoolConnection((client, done) => {
_parseQueueParamsAndValidate(client, queue, (err, queueParams) => {
if (err)
return done(err);
if (!queueParams)
return done(new CallbackEmptyReplyError());
const { keyQueueStateHistory } = redisKeys.getQueueKeys(queueParams.ns, queueParams.name, null);
client.lrange(keyQueueStateHistory, 0, -1, (err, reply) => {
if (err)
return done(err);
const history = (reply || []).map((item) => JSON.parse(item));
this.logger.debug(`Retrieved ${history.length} state transitions for ${queueDesc}`);
done(null, history);
});
});
}, callback);
});
}
}
//# sourceMappingURL=queue-state-manager.js.map