UNPKG

redis-smq

Version:

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

96 lines 4.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BackgroundJobCluster = void 0; const redis_smq_common_1 = require("redis-smq-common"); const configuration_js_1 = require("../../config-manager/configuration.js"); const redis_connection_pool_js_1 = require("../redis/redis-connection-pool/redis-connection-pool.js"); const connection_pool_js_1 = require("../redis/redis-connection-pool/types/connection-pool.js"); const path_1 = __importDefault(require("path")); const node_worker_threads_1 = require("node:worker_threads"); const redis_config_js_1 = require("../redis/redis-config.js"); const curDir = redis_smq_common_1.env.getCurrentDir(); const workersPath = path_1.default.resolve(curDir, 'jobs'); class BackgroundJobCluster extends redis_smq_common_1.Runnable { constructor() { super(); this.workerCluster = null; this.redisClient = null; this.config = configuration_js_1.Configuration.getConfig(); this.logger = (0, redis_smq_common_1.createLogger)(this.config.logger, [ `${this.constructor.name}-${this.getId()}`, ]); } static run(cb) { if (!node_worker_threads_1.isMainThread) { return cb(); } if (!BackgroundJobCluster.instance) { BackgroundJobCluster.instance = new BackgroundJobCluster(); } BackgroundJobCluster.instance.run(cb); } static shutdown(cb) { if (BackgroundJobCluster.instance) { BackgroundJobCluster.instance.shutdown(() => { BackgroundJobCluster.instance = null; cb(); }); return; } cb(); } goingUp() { return super.goingUp().concat([ (cb) => { const redisConnectionPool = redis_connection_pool_js_1.RedisConnectionPool.getInstance(); redisConnectionPool.acquire(connection_pool_js_1.ERedisConnectionAcquisitionMode.SHARED, (err, redisClient) => { if (err) return cb(err); if (!redisClient) return cb(new redis_smq_common_1.CallbackEmptyReplyError()); this.redisClient = redisClient; this.workerCluster = new redis_smq_common_1.WorkerCluster(redisClient, this.logger, null, '.job.js'); this.workerCluster.on('workerCluster.error', (err) => { this.logger.error(err); }); this.workerCluster.loadFromDir(workersPath, { config: this.config, redisConfig: redis_config_js_1.RedisConfig.getConfig(), loggerContext: { namespaces: this.logger.getNamespaces() }, }, (err) => { var _a; if (err) return cb(err); (_a = this.workerCluster) === null || _a === void 0 ? void 0 : _a.run(() => void 0); cb(); }); }); }, ]); } goingDown() { return [ (cb) => { if (this.workerCluster) { this.workerCluster.removeAllListeners('workerCluster.error'); this.workerCluster.shutdown(cb); } else cb(); }, (cb) => { if (this.redisClient) { redis_connection_pool_js_1.RedisConnectionPool.getInstance().release(this.redisClient); this.redisClient = null; } cb(); }, ].concat(super.goingDown()); } } exports.BackgroundJobCluster = BackgroundJobCluster; BackgroundJobCluster.instance = null; //# sourceMappingURL=background-job-cluster.js.map