UNPKG

2.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const SqsConsumer = require("sqs-consumer");
4const LogManager_1 = require("../log/LogManager");
5const log = LogManager_1.LogManager.getLogger(__filename);
6const defaultAwsRegion = 'ap-southeast-2';
7class SqsHandler {
8}
9exports.SqsHandler = SqsHandler;
10class SqsWorker {
11 constructor(configuration, name) {
12 this.maxRetries = 5;
13 this.name = name;
14 // tslint:disable-next-line
15 this.configuration = Object.assign({}, {
16 attributeNames: ['All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount'],
17 region: defaultAwsRegion,
18 }, configuration);
19 this.consumerCreator = (config) => SqsConsumer.create(config);
20 }
21 initialise() {
22 // tslint:disable-next-line
23 const conf = Object.assign({}, this.configuration, {
24 handleMessage: (m, done) => {
25 try {
26 if (m.Attributes.ApproximateReceiveCount > this.getMaxRetries()) {
27 m.skipped = 1;
28 log.error(m, `Reached maximum number of retries ${m.Attributes.ApproximateReceiveCount} > ${this.getMaxRetries()}`);
29 done();
30 }
31 else {
32 this.handler.handle(m, done).then(done, done);
33 }
34 }
35 catch (err) {
36 done(err);
37 }
38 },
39 });
40 this.instance = this.consumerCreator(conf);
41 this.instance.on('error', (err) => {
42 log.error(err, `Unexpected SQS error`);
43 });
44 log.info(`Starting SQS Worker: ${this.name}`);
45 this.instance.start();
46 }
47 shutdown() {
48 log.info(`Stoping SQS Worker: ${this.name}`);
49 this.instance.stop();
50 }
51 getMaxRetries() {
52 return this.maxRetries;
53 }
54}
55SqsWorker.startMethod = 'initialise';
56SqsWorker.stopMethod = 'shutdown';
57SqsWorker.lazy = false;
58exports.SqsWorker = SqsWorker;
59//# sourceMappingURL=SqsWorker.js.map
\No newline at end of file