redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
52 lines • 3.05 kB
JavaScript
import { EQueueOperationalState, } from '../../queue-manager/index.js';
import { EStateTransitionReason, } from '../types/index.js';
import { CallbackEmptyReplyError } from 'redis-smq-common';
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 { _getQueueState } from './_get-queue-state.js';
import { _isAllowedTransition } from './_is-allowed-transition.js';
import { QueueStateTransitionError } from '../../errors/index.js';
import { _setQueueState } from './_set-queue-state.js';
import { EventMultiplexer } from '../../event-bus/event-multiplexer.js';
export function _transitQueueTo(queue, newState, options, logger, cb) {
const queueDesc = typeof queue === 'string' ? queue : `${queue.name}@${queue.ns}`;
logger.debug(`Setting operational state for queue ${queueDesc} to: ${EQueueOperationalState[newState]}`);
withSharedPoolConnection((client, done) => {
_parseQueueParamsAndValidate(client, queue, (err, queueParams) => {
if (err)
return done(err);
if (!queueParams)
return done(new CallbackEmptyReplyError());
_getQueueState(client, queueParams, (err, currentState) => {
if (err)
return done(err);
if (!currentState)
return done(new CallbackEmptyReplyError());
if (!_isAllowedTransition(currentState.to, newState)) {
const error = new QueueStateTransitionError({
message: `Cannot transition from ${EQueueOperationalState[currentState.to]} to ${EQueueOperationalState[newState]}`,
metadata: {
from: currentState.to,
to: newState,
},
});
logger.error(error.message, error);
return done(error);
}
const reason = options?.reason || EStateTransitionReason.MANUAL;
_setQueueState(client, queueParams, currentState.to, newState, reason, options, (err, transition) => {
if (err) {
logger.error(`Error setting state for ${queueParams.name}@${queueParams.ns}: ${err.message}`, err);
return done(err);
}
if (!transition)
return done(new CallbackEmptyReplyError());
logger.info(`Queue ${queueParams.name}@${queueParams.ns} state changed from ${EQueueOperationalState[currentState.to]} to ${EQueueOperationalState[newState]} (reason: ${reason})`);
EventMultiplexer.publish('queue.stateChanged', queueParams, transition);
done(null, transition);
});
});
});
}, cb);
}
//# sourceMappingURL=_transit-queue-to.js.map