1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const SqsConsumer = require("sqs-consumer");
|
4 | const LogManager_1 = require("../log/LogManager");
|
5 | const log = LogManager_1.LogManager.getLogger(__filename);
|
6 | const defaultAwsRegion = 'ap-southeast-2';
|
7 | class SqsHandler {
|
8 | }
|
9 | exports.SqsHandler = SqsHandler;
|
10 | class SqsWorker {
|
11 | constructor(configuration, name) {
|
12 | this.maxRetries = 5;
|
13 | this.name = name;
|
14 |
|
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 |
|
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 | }
|
55 | SqsWorker.startMethod = 'initialise';
|
56 | SqsWorker.stopMethod = 'shutdown';
|
57 | SqsWorker.lazy = false;
|
58 | exports.SqsWorker = SqsWorker;
|
59 |
|
\ | No newline at end of file |