UNPKG

redis-smq

Version:

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

83 lines 3.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BackgroundJobWorkerAbstract = void 0; const redis_smq_common_1 = require("redis-smq-common"); const worker_abstract_js_1 = require("../worker/worker-abstract.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 redis_keys_js_1 = require("../../redis/redis-keys/redis-keys.js"); const heartbeat_js_1 = require("../../heartbeat/heartbeat.js"); class BackgroundJobWorkerAbstract extends worker_abstract_js_1.WorkerAbstract { constructor(payload) { super(payload); this.redisClient = null; this.heartbeat = null; this.setUpHeartbeat = (cb) => { if (!this.redisClient) return cb(new redis_smq_common_1.PanicError({ message: 'Redis client is not initialized', })); try { const { keyWorkerHeartbeat } = redis_keys_js_1.redisKeys.getWorkerKeys(this.id); this.heartbeat = (0, heartbeat_js_1.HeartbeatFactory)(this.redisClient, this.logger, { heartbeatKey: keyWorkerHeartbeat, componentId: this.id, componentType: this.constructor.name, }); this.heartbeat.run(cb); } catch (error) { const err = error instanceof Error ? error : new Error(String(error)); cb(err); } }; this.shutdownHeartbeat = (cb) => { if (this.heartbeat) { return this.heartbeat.shutdown((err) => { this.heartbeat = null; cb(err); }); } cb(); }; this.logger = (0, redis_smq_common_1.createLogger)(payload.config.logger, [ ...payload.loggerContext.namespaces, this.constructor.name, ]); this.logger.debug(`Worker ${this.constructor.name} initialized.`); } goingUp() { return super.goingUp().concat([ (cb) => { redis_connection_pool_js_1.RedisConnectionPool.getInstance().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; cb(); }); }, this.setUpHeartbeat, ]); } goingDown() { return [ this.shutdownHeartbeat, (cb) => { if (this.redisClient) { redis_connection_pool_js_1.RedisConnectionPool.getInstance().release(this.redisClient); this.redisClient = null; } cb(); }, ].concat(super.goingDown()); } getRedisClient() { if (!this.redisClient) throw new redis_smq_common_1.PanicError({ message: 'A RedisClient instance is required.' }); return this.redisClient; } } exports.BackgroundJobWorkerAbstract = BackgroundJobWorkerAbstract; //# sourceMappingURL=background-job-worker-abstract.js.map