UNPKG

redis-smq

Version:

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

63 lines 1.86 kB
import { Runnable, Timer } from 'redis-smq-common'; import { RedisSMQ } from '../../../redis-smq/index.js'; export class WorkerAbstract extends Runnable { timer = null; initialized = false; config; redisConfig; constructor(payload) { super(); const { redisConfig, config } = payload; this.redisConfig = redisConfig; this.config = config; } finalizeUp() { this.timer?.schedule(this.onTick, 1000); super.finalizeUp(); } goingUp() { return super.goingUp().concat([ (cb) => { if (RedisSMQ.isRunning()) return cb(); RedisSMQ.initialize(this.redisConfig, cb); }, (cb) => { this.logger.debug('Setting up worker timer'); this.timer = new Timer(this.logger); this.timer.run(cb); }, ]); } goingDown() { return [ (cb) => { this.logger.debug('Resetting worker timer'); if (this.timer) { this.timer.shutdown(cb); return; } cb(); }, ].concat(super.goingDown()); } onTick = () => { if (this.isOperational()) { this.work((err) => { if (err) { this.logger.error('Error during worker execution', err); this.handleError(err); return; } this.timer?.schedule(this.onTick, 1000); }); } }; handleError = (err) => { if (this.isOperational()) { this.logger.error(`Fatal error in worker ${this.constructor.name}`, err); throw err; } }; } //# sourceMappingURL=worker-abstract.js.map