redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
58 lines • 1.73 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, `worker:${this.constructor.name.toLowerCase()}:${this.id}`);
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() {
return super.goingUp().concat([
this.redisClient.init,
(cb) => {
this.timer.setTimeout(this.onTick, 1000);
cb();
},
]);
}
goingDown() {
return [
(cb) => {
this.timer.reset();
cb();
},
this.redisClient.shutdown,
].concat(super.goingDown());
}
onTick = () => {
if (this.isRunning()) {
this.work((err) => {
if (err)
this.handleError(err);
else
this.timer.setTimeout(this.onTick, 1000);
});
}
};
handleError = (err) => {
if (this.isRunning()) {
throw err;
}
};
}
//# sourceMappingURL=worker.js.map