inceptum
Version:
hipages take on the foundational library for enterprise-grade apps written in NodeJS
60 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqsWorker = exports.SqsHandler = void 0;
const SqsConsumer = require("sqs-consumer");
const LogManager_1 = require("../log/LogManager");
const log = LogManager_1.LogManager.getLogger(__filename);
const defaultAwsRegion = 'ap-southeast-2';
class SqsHandler {
}
exports.SqsHandler = SqsHandler;
class SqsWorker {
constructor(configuration, name) {
this.maxRetries = 5;
this.name = name;
// tslint:disable-next-line
this.configuration = Object.assign({}, {
attributeNames: ['All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount'],
region: defaultAwsRegion,
}, configuration);
this.consumerCreator = (config) => SqsConsumer.create(config);
}
initialise() {
// tslint:disable-next-line
const conf = Object.assign({}, this.configuration, {
handleMessage: (m, done) => {
try {
if (m.Attributes.ApproximateReceiveCount > this.getMaxRetries()) {
m.skipped = 1;
log.error(m, `Reached maximum number of retries ${m.Attributes.ApproximateReceiveCount} > ${this.getMaxRetries()}`);
done();
}
else {
this.handler.handle(m, done).then(done, done);
}
}
catch (err) {
done(err);
}
},
});
this.instance = this.consumerCreator(conf);
this.instance.on('error', (err) => {
log.error(err, `Unexpected SQS error`);
});
log.info(`Starting SQS Worker: ${this.name}`);
this.instance.start();
}
shutdown() {
log.info(`Stoping SQS Worker: ${this.name}`);
this.instance.stop();
}
getMaxRetries() {
return this.maxRetries;
}
}
exports.SqsWorker = SqsWorker;
SqsWorker.startMethod = 'initialise';
SqsWorker.stopMethod = 'shutdown';
SqsWorker.lazy = false;
//# sourceMappingURL=SqsWorker.js.map