redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
75 lines • 2.55 kB
JavaScript
import { logger, Runnable, Timer } from 'redis-smq-common';
import { RedisClient } from '../../../common/redis-client/redis-client.js';
import { Configuration } from '../../../config/index.js';
export class Worker extends Runnable {
redisClient;
logger;
timer;
config;
queueParsedParams;
constructor({ config, queueParsedParams, }) {
super();
this.config = config;
Configuration.getSetConfig(config);
this.queueParsedParams = queueParsedParams;
this.logger = logger.getLogger(config.logger, this.constructor.name.toLowerCase());
this.logger.info(`Initializing worker: ${this.constructor.name}`);
this.redisClient = new RedisClient();
this.redisClient.on('error', (err) => this.handleError(err));
this.timer = new Timer();
this.timer.on('error', (err) => this.handleError(err));
}
getLogger() {
return this.logger;
}
goingUp() {
this.logger.debug('Worker going up');
return super.goingUp().concat([
(cb) => {
this.logger.debug('Initializing Redis client');
this.redisClient.init(cb);
},
(cb) => {
this.logger.debug('Setting up worker timer');
this.timer.setTimeout(this.onTick, 1000);
cb();
},
]);
}
goingDown() {
this.logger.debug('Worker going down');
return [
(cb) => {
this.logger.debug('Resetting worker timer');
this.timer.reset();
cb();
},
(cb) => {
this.logger.debug('Shutting down Redis client');
this.redisClient.shutdown(cb);
},
].concat(super.goingDown());
}
onTick = () => {
if (this.isRunning()) {
this.logger.debug('Worker tick triggered');
this.work((err) => {
if (err) {
this.logger.error('Error during worker execution', err);
this.handleError(err);
}
else {
this.logger.debug('Scheduling next worker tick');
this.timer.setTimeout(this.onTick, 1000);
}
});
}
};
handleError = (err) => {
if (this.isRunning()) {
this.logger.error(`Fatal error in worker ${this.constructor.name}`, err);
throw err;
}
};
}
//# sourceMappingURL=worker.js.map