redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
55 lines • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._transitQueueTo = _transitQueueTo;
const index_js_1 = require("../../queue-manager/index.js");
const index_js_2 = require("../types/index.js");
const redis_smq_common_1 = require("redis-smq-common");
const with_shared_pool_connection_js_1 = require("../../common/redis/redis-connection-pool/with-shared-pool-connection.js");
const _parse_queue_params_and_validate_js_1 = require("../../queue-manager/_/_parse-queue-params-and-validate.js");
const _get_queue_state_js_1 = require("./_get-queue-state.js");
const _is_allowed_transition_js_1 = require("./_is-allowed-transition.js");
const index_js_3 = require("../../errors/index.js");
const _set_queue_state_js_1 = require("./_set-queue-state.js");
const event_multiplexer_js_1 = require("../../event-bus/event-multiplexer.js");
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: ${index_js_1.EQueueOperationalState[newState]}`);
(0, with_shared_pool_connection_js_1.withSharedPoolConnection)((client, done) => {
(0, _parse_queue_params_and_validate_js_1._parseQueueParamsAndValidate)(client, queue, (err, queueParams) => {
if (err)
return done(err);
if (!queueParams)
return done(new redis_smq_common_1.CallbackEmptyReplyError());
(0, _get_queue_state_js_1._getQueueState)(client, queueParams, (err, currentState) => {
if (err)
return done(err);
if (!currentState)
return done(new redis_smq_common_1.CallbackEmptyReplyError());
if (!(0, _is_allowed_transition_js_1._isAllowedTransition)(currentState.to, newState)) {
const error = new index_js_3.QueueStateTransitionError({
message: `Cannot transition from ${index_js_1.EQueueOperationalState[currentState.to]} to ${index_js_1.EQueueOperationalState[newState]}`,
metadata: {
from: currentState.to,
to: newState,
},
});
logger.error(error.message, error);
return done(error);
}
const reason = (options === null || options === void 0 ? void 0 : options.reason) || index_js_2.EStateTransitionReason.MANUAL;
(0, _set_queue_state_js_1._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 redis_smq_common_1.CallbackEmptyReplyError());
logger.info(`Queue ${queueParams.name}@${queueParams.ns} state changed from ${index_js_1.EQueueOperationalState[currentState.to]} to ${index_js_1.EQueueOperationalState[newState]} (reason: ${reason})`);
event_multiplexer_js_1.EventMultiplexer.publish('queue.stateChanged', queueParams, transition);
done(null, transition);
});
});
});
}, cb);
}
//# sourceMappingURL=_transit-queue-to.js.map