UNPKG

redis-smq

Version:

A high-performance, reliable, and scalable message queue for Node.js.

61 lines 2.49 kB
import { EQueueOperationalState, EQueueProperty, } from '../../queue-manager/index.js'; import { redisKeys } from '../../common/redis/redis-keys/redis-keys.js'; import { _createQueueStateTransition } from './_create-queue-state-transition.js'; import { ERedisScriptName } from '../../common/redis/scripts.js'; import { QueueNotFoundError, QueueStateTransitionError, UnexpectedScriptReplyError, } from '../../errors/index.js'; export const maxQueueStateHistorySize = 50; export function _setQueueState(client, queueParams, from, to, reason, options, cb) { const { keyQueueProperties, keyQueueStateHistory } = redisKeys.getQueueKeys(queueParams.ns, queueParams.name, null); const lockId = options?.lockId ?? ''; const transition = _createQueueStateTransition(from, to, reason, options ?? {}); const keys = [keyQueueProperties, keyQueueStateHistory]; const args = [ EQueueProperty.OPERATIONAL_STATE, to, JSON.stringify(transition), from, EQueueOperationalState.ACTIVE, maxQueueStateHistorySize, EQueueOperationalState.LOCKED, lockId, EQueueProperty.LAST_STATE_CHANGE_AT, transition.timestamp, EQueueProperty.LOCK_ID, ]; client.runScript(ERedisScriptName.SET_QUEUE_STATE, keys, args, (err, reply) => { if (err) return cb(err); if (reply !== 'OK') { if (reply === 'QUEUE_NOT_FOUND') { return cb(new QueueNotFoundError({ metadata: { queue: queueParams, }, })); } if (reply === 'INVALID_STATE_TRANSITION') { return cb(new QueueStateTransitionError({ message: `Invalid state transition: queue state changed since validation`, metadata: { from, to, }, })); } if (reply === 'INVALID_LOCK') { return cb(new QueueStateTransitionError({ message: `Invalid lock ID provided`, metadata: { from, to, }, })); } return cb(new UnexpectedScriptReplyError({ metadata: { reply }, })); } cb(null, transition); }); } //# sourceMappingURL=_set-queue-state.js.map