UNPKG

inceptum

Version:

hipages take on the foundational library for enterprise-grade apps written in NodeJS

57 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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; this.configuration = Object.assign({}, { attributeNames: ['All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount'], region: defaultAwsRegion, }, configuration); this.consumerCreator = (config) => SqsConsumer.create(config); } initialise() { 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; } } SqsWorker.startMethod = 'initialise'; SqsWorker.stopMethod = 'shutdown'; SqsWorker.lazy = false; exports.SqsWorker = SqsWorker; //# sourceMappingURL=SqsWorker.js.map