UNPKG

redis-smq

Version:

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

132 lines 6.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PurgeQueueJobManager = void 0; const background_job_manager_abstract_js_1 = require("../../../abstract/background-job/background-job-manager-abstract.js"); const redis_smq_common_1 = require("redis-smq-common"); const redis_keys_js_1 = require("../../../redis/redis-keys/redis-keys.js"); const index_js_1 = require("../../../../queue-state-manager/index.js"); const index_js_2 = require("../../../index.js"); const node_crypto_1 = require("node:crypto"); const index_js_3 = require("../../../../errors/index.js"); const _lock_queue_js_1 = require("../../../../queue-state-manager/_/_lock-queue.js"); const _unlock_queue_js_1 = require("../../../../queue-state-manager/_/_unlock-queue.js"); class PurgeQueueJobManager extends background_job_manager_abstract_js_1.BackgroundJobManagerAbstract { constructor(redisClient, logger) { const keys = redis_keys_js_1.redisKeys.getMainKeys(); super(redisClient, { keyBackgroundJobs: keys.keyPurgeJobs, keyBackgroundJobsPending: keys.keyPurgeJobsPending, keyBackgroundJobsProcessing: keys.keyPurgeJobsProcessing, }, logger); } create(payload, options = {}, cb) { const jobId = options.id || (0, node_crypto_1.randomUUID)(); redis_smq_common_1.async.waterfall([ (next) => { this.lockQueue(payload.queue.queueParams, jobId, next); }, (_, next) => { super.create(payload, Object.assign(Object.assign({}, options), { id: jobId }), (err, job) => { if (err) { (0, _unlock_queue_js_1._unlockQueue)(payload.queue.queueParams, index_js_1.EQueueStateLockOwner.PURGE_JOB, jobId, { reason: index_js_1.ESystemStateTransitionReason.PURGE_QUEUE_FAIL, description: `Purge job failed`, metadata: { jobId }, }, this.logger, () => next(err)); } next(null, job); }); }, ], cb); } complete(jobId, options, cb) { redis_smq_common_1.async.waterfall([ (next) => super.complete(jobId, options, next), (job, next) => { (0, _unlock_queue_js_1._unlockQueue)(job.payload.queue.queueParams, index_js_1.EQueueStateLockOwner.PURGE_JOB, jobId, { reason: index_js_1.ESystemStateTransitionReason.PURGE_QUEUE_COMPLETE, description: `Purge job complete`, metadata: { jobId }, }, this.logger, (unlockErr) => { if (unlockErr) { this.logger.error(`Job ${jobId} completed but failed to unlock queue: ${unlockErr.message}`); } next(null, job); }); }, ], cb); } fail(jobId, error, cb) { redis_smq_common_1.async.waterfall([ (next) => super.fail(jobId, error, next), (job, next) => { (0, _unlock_queue_js_1._unlockQueue)(job.payload.queue.queueParams, index_js_1.EQueueStateLockOwner.PURGE_JOB, jobId, { reason: index_js_1.ESystemStateTransitionReason.PURGE_QUEUE_FAIL, description: `Purge job failed`, metadata: { jobId }, }, this.logger, (unlockErr) => { if (unlockErr) { this.logger.error(`Job ${jobId} failed but failed to unlock queue: ${unlockErr.message}`); } next(null, job); }); }, ], cb); } cancel(jobId, cb) { redis_smq_common_1.async.waterfall([ (next) => super.cancel(jobId, next), (job, next) => { (0, _unlock_queue_js_1._unlockQueue)(job.payload.queue.queueParams, index_js_1.EQueueStateLockOwner.PURGE_JOB, jobId, { reason: index_js_1.ESystemStateTransitionReason.PURGE_QUEUE_CANCEL, description: `Purge job cancelled`, metadata: { jobId }, }, this.logger, (unlockErr) => { if (unlockErr) { this.logger.error(`Job ${jobId} cancelled but failed to unlock queue: ${unlockErr.message}`); } next(null, job); }); }, ], cb); } lockQueue(queueParams, jobId, cb) { (0, _lock_queue_js_1._lockQueuelock)(queueParams, index_js_1.EQueueStateLockOwner.PURGE_JOB, jobId, { reason: index_js_1.ESystemStateTransitionReason.PURGE_QUEUE_START, description: `Queue is being purged`, }, this.logger, (err) => cb(err)); } hasTerminationStatus(jobId, cb) { redis_smq_common_1.async.waterfall([ (next) => this.get(jobId, next), (job, next) => { const r = job.status === index_js_2.EBackgroundJobStatus.COMPLETED || job.status === index_js_2.EBackgroundJobStatus.FAILED || job.status === index_js_2.EBackgroundJobStatus.CANCELED; next(null, r); }, ], (err, result) => { if (err) { if (err instanceof index_js_3.BackgroundJobNotFoundError) { return cb(null, false); } } cb(err, result); }); } updateProgress(jobId, totalPurged, totalItems) { if (totalItems > 0 && totalPurged % Math.max(1, Math.floor(totalPurged / 10)) === 0) { this.update(jobId, { meta: { purged: totalPurged } }, (updateErr) => { if (updateErr) { this.logger.error(`Failed to update progress for job ${jobId}:`, updateErr); } else { this.logger.debug(`Job ${jobId}: Purged ${totalPurged} messages so far`); } }); } } } exports.PurgeQueueJobManager = PurgeQueueJobManager; //# sourceMappingURL=purge-queue-job-manager.js.map