@backstage/plugin-events-backend-module-aws-sqs 0.0.0-nightly-20221115024001
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +42 -0
- package/alpha/package.json +6 -0
- package/config.d.ts +78 -0
- package/dist/index.alpha.d.ts +53 -0
- package/dist/index.beta.d.ts +48 -0
- package/dist/index.cjs.js +219 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +48 -0
- package/package.json +48 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @backstage/plugin-events-backend-module-aws-sqs
|
|
2
|
+
|
|
3
|
+
## 0.0.0-nightly-20221115024001
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- d3ecb2382d: Adds a new module `aws-sqs` for plugin-events-backend.
|
|
8
|
+
|
|
9
|
+
The module provides an event publisher `AwsSqsConsumingEventPublisher`
|
|
10
|
+
which will allow you to receive events from
|
|
11
|
+
an AWS SQS queue and will publish these to the used event broker.
|
|
12
|
+
|
|
13
|
+
Please find more information at
|
|
14
|
+
https://github.com/backstage/backstage/tree/master/plugins/events-backend-module-aws-sqs/README.md.
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- Updated dependencies
|
|
19
|
+
- @backstage/backend-tasks@0.0.0-nightly-20221115024001
|
|
20
|
+
- @backstage/plugin-events-node@0.0.0-nightly-20221115024001
|
|
21
|
+
- @backstage/types@0.0.0-nightly-20221115024001
|
|
22
|
+
- @backstage/backend-plugin-api@0.0.0-nightly-20221115024001
|
|
23
|
+
- @backstage/config@0.0.0-nightly-20221115024001
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# events-backend-module-aws-sqs
|
|
2
|
+
|
|
3
|
+
Welcome to the `events-backend-module-aws-sqs` backend plugin!
|
|
4
|
+
|
|
5
|
+
This plugin is a module for the `events-backend` backend plugin
|
|
6
|
+
and extends it with an `AwsSqsConsumingEventPublisher`.
|
|
7
|
+
|
|
8
|
+
This event publisher will allow you to receive events from
|
|
9
|
+
an AWS SQS queue and will publish these to the used event broker.
|
|
10
|
+
|
|
11
|
+
## Configuration
|
|
12
|
+
|
|
13
|
+
The polled AWS SQS queues depend on your configuration:
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
events:
|
|
17
|
+
modules:
|
|
18
|
+
awsSqs:
|
|
19
|
+
awsSqsConsumingEventPublisher:
|
|
20
|
+
topics:
|
|
21
|
+
topicName1: # replace with actual topic name as expected by subscribers
|
|
22
|
+
queue:
|
|
23
|
+
url: 'https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue'
|
|
24
|
+
region: us-east-2
|
|
25
|
+
# visibilityTimeout - as HumanDuration; defaults to queue-based config
|
|
26
|
+
# waitTime - as HumanDuration; defaults to max of 20 seconds (long polling)
|
|
27
|
+
# timeout - as HumanDuration; timeout for the task execution
|
|
28
|
+
# waitTimeAfterEmptyReceive - as HumanDuration; time to wait before a retry when there was no message.
|
|
29
|
+
topicName2:
|
|
30
|
+
# [...]
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
1. Install the [`events-backend` plugin](../events-backend/README.md).
|
|
36
|
+
2. Install this module
|
|
37
|
+
3. Add your configuration.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# From your Backstage root directory
|
|
41
|
+
yarn add --cwd packages/backend @backstage/plugin-events-backend-module-aws-sqs
|
|
42
|
+
```
|
package/config.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 The Backstage Authors
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { HumanDuration } from '@backstage/types';
|
|
18
|
+
|
|
19
|
+
export interface Config {
|
|
20
|
+
events?: {
|
|
21
|
+
modules?: {
|
|
22
|
+
/**
|
|
23
|
+
* events-backend-module-aws-sqs plugin configuration.
|
|
24
|
+
*/
|
|
25
|
+
awsSqs?: {
|
|
26
|
+
/**
|
|
27
|
+
* Configuration for AwsSqsConsumingEventPublisher.
|
|
28
|
+
*/
|
|
29
|
+
awsSqsConsumingEventPublisher?: {
|
|
30
|
+
/**
|
|
31
|
+
* Contains a record per topic for which an AWS SQS queue
|
|
32
|
+
* should be used as source of events.
|
|
33
|
+
*/
|
|
34
|
+
topics: Record<
|
|
35
|
+
string,
|
|
36
|
+
{
|
|
37
|
+
/**
|
|
38
|
+
* (Required) Queue-related configuration.
|
|
39
|
+
*/
|
|
40
|
+
queue: {
|
|
41
|
+
/**
|
|
42
|
+
* (Required) The region of the AWS SQS queue.
|
|
43
|
+
*/
|
|
44
|
+
region: string;
|
|
45
|
+
/**
|
|
46
|
+
* (Required) The absolute URL for the AWS SQS queue to be used.
|
|
47
|
+
*/
|
|
48
|
+
url: string;
|
|
49
|
+
/**
|
|
50
|
+
* (Optional) Visibility timeout for messages in flight.
|
|
51
|
+
*/
|
|
52
|
+
visibilityTimeout: HumanDuration;
|
|
53
|
+
/**
|
|
54
|
+
* (Optional) Wait time when polling for available messages.
|
|
55
|
+
* Default: 20 seconds.
|
|
56
|
+
*/
|
|
57
|
+
waitTime: HumanDuration;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* (Optional) Timeout for the task execution which includes polling for messages
|
|
61
|
+
* and publishing the events to the event broker
|
|
62
|
+
* and the wait time after empty receives.
|
|
63
|
+
*
|
|
64
|
+
* Must be greater than `queue.waitTime` + `waitTimeAfterEmptyReceive`.
|
|
65
|
+
*/
|
|
66
|
+
timeout: HumanDuration;
|
|
67
|
+
/**
|
|
68
|
+
* (Optional) Wait time before polling again if no message was received.
|
|
69
|
+
* Default: 1 minute.
|
|
70
|
+
*/
|
|
71
|
+
waitTimeAfterEmptyReceive: HumanDuration;
|
|
72
|
+
}
|
|
73
|
+
>;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The module "sqs" for the Backstage backend plugin "events"
|
|
3
|
+
* adding an AWS SQS-based publisher,
|
|
4
|
+
* receiving events from an AWS SQS queue and passing it to the
|
|
5
|
+
* internal event broker.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { BackendFeature } from '@backstage/backend-plugin-api';
|
|
11
|
+
import { Config } from '@backstage/config';
|
|
12
|
+
import { EventBroker } from '@backstage/plugin-events-node';
|
|
13
|
+
import { EventPublisher } from '@backstage/plugin-events-node';
|
|
14
|
+
import { Logger } from 'winston';
|
|
15
|
+
import { PluginTaskScheduler } from '@backstage/backend-tasks';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Publishes events received from an AWS SQS queue.
|
|
19
|
+
* The message payload will be used as event payload and passed to registered subscribers.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare class AwsSqsConsumingEventPublisher implements EventPublisher {
|
|
24
|
+
private readonly logger;
|
|
25
|
+
private readonly scheduler;
|
|
26
|
+
private readonly topic;
|
|
27
|
+
private readonly receiveParams;
|
|
28
|
+
private readonly sqs;
|
|
29
|
+
private readonly queueUrl;
|
|
30
|
+
private readonly taskTimeoutSeconds;
|
|
31
|
+
private readonly waitTimeAfterEmptyReceiveMs;
|
|
32
|
+
private eventBroker?;
|
|
33
|
+
static fromConfig(env: {
|
|
34
|
+
config: Config;
|
|
35
|
+
logger: Logger;
|
|
36
|
+
scheduler: PluginTaskScheduler;
|
|
37
|
+
}): AwsSqsConsumingEventPublisher[];
|
|
38
|
+
private constructor();
|
|
39
|
+
setEventBroker(eventBroker: EventBroker): Promise<void>;
|
|
40
|
+
private start;
|
|
41
|
+
private deleteMessages;
|
|
42
|
+
private consumeMessages;
|
|
43
|
+
private sleep;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* AWS SQS module for the Events plugin.
|
|
48
|
+
*
|
|
49
|
+
* @alpha
|
|
50
|
+
*/
|
|
51
|
+
export declare const awsSqsConsumingEventPublisherEventsModule: (options?: undefined) => BackendFeature;
|
|
52
|
+
|
|
53
|
+
export { }
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The module "sqs" for the Backstage backend plugin "events"
|
|
3
|
+
* adding an AWS SQS-based publisher,
|
|
4
|
+
* receiving events from an AWS SQS queue and passing it to the
|
|
5
|
+
* internal event broker.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { BackendFeature } from '@backstage/backend-plugin-api';
|
|
11
|
+
import { Config } from '@backstage/config';
|
|
12
|
+
import { EventBroker } from '@backstage/plugin-events-node';
|
|
13
|
+
import { EventPublisher } from '@backstage/plugin-events-node';
|
|
14
|
+
import { Logger } from 'winston';
|
|
15
|
+
import { PluginTaskScheduler } from '@backstage/backend-tasks';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Publishes events received from an AWS SQS queue.
|
|
19
|
+
* The message payload will be used as event payload and passed to registered subscribers.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare class AwsSqsConsumingEventPublisher implements EventPublisher {
|
|
24
|
+
private readonly logger;
|
|
25
|
+
private readonly scheduler;
|
|
26
|
+
private readonly topic;
|
|
27
|
+
private readonly receiveParams;
|
|
28
|
+
private readonly sqs;
|
|
29
|
+
private readonly queueUrl;
|
|
30
|
+
private readonly taskTimeoutSeconds;
|
|
31
|
+
private readonly waitTimeAfterEmptyReceiveMs;
|
|
32
|
+
private eventBroker?;
|
|
33
|
+
static fromConfig(env: {
|
|
34
|
+
config: Config;
|
|
35
|
+
logger: Logger;
|
|
36
|
+
scheduler: PluginTaskScheduler;
|
|
37
|
+
}): AwsSqsConsumingEventPublisher[];
|
|
38
|
+
private constructor();
|
|
39
|
+
setEventBroker(eventBroker: EventBroker): Promise<void>;
|
|
40
|
+
private start;
|
|
41
|
+
private deleteMessages;
|
|
42
|
+
private consumeMessages;
|
|
43
|
+
private sleep;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* Excluded from this release type: awsSqsConsumingEventPublisherEventsModule */
|
|
47
|
+
|
|
48
|
+
export { }
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var clientSqs = require('@aws-sdk/client-sqs');
|
|
6
|
+
var luxon = require('luxon');
|
|
7
|
+
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
8
|
+
var pluginEventsNode = require('@backstage/plugin-events-node');
|
|
9
|
+
|
|
10
|
+
const CONFIG_PREFIX_MODULE = "events.modules.awsSqs.";
|
|
11
|
+
const CONFIG_PREFIX_PUBLISHER = `${CONFIG_PREFIX_MODULE}awsSqsConsumingEventPublisher.`;
|
|
12
|
+
const DEFAULT_WAIT_TIME_AFTER_EMPTY_RECEIVE = { minutes: 1 };
|
|
13
|
+
const MAX_WAIT_SECONDS = 20;
|
|
14
|
+
function readOptionalHumanDuration(config, key) {
|
|
15
|
+
return config.getOptional(key);
|
|
16
|
+
}
|
|
17
|
+
function readOptionalDuration(config, key) {
|
|
18
|
+
const duration = readOptionalHumanDuration(config, key);
|
|
19
|
+
return duration ? luxon.Duration.fromObject(duration) : void 0;
|
|
20
|
+
}
|
|
21
|
+
function readConfig(config) {
|
|
22
|
+
var _a, _b;
|
|
23
|
+
const key = `${CONFIG_PREFIX_PUBLISHER}topics`;
|
|
24
|
+
const topics = config.getOptionalConfig(key);
|
|
25
|
+
return (_b = (_a = topics == null ? void 0 : topics.keys()) == null ? void 0 : _a.map((topic) => {
|
|
26
|
+
var _a2, _b2, _c;
|
|
27
|
+
const topicConfig = topics.getConfig(topic);
|
|
28
|
+
const keyPrefix = `${key}.${topic}.`;
|
|
29
|
+
const pollingWaitTime = luxon.Duration.fromObject(
|
|
30
|
+
(_a2 = readOptionalHumanDuration(topicConfig, "queue.waitTime")) != null ? _a2 : {
|
|
31
|
+
seconds: MAX_WAIT_SECONDS
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
if (pollingWaitTime.valueOf() < 0 || pollingWaitTime.as("seconds") > MAX_WAIT_SECONDS) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`${keyPrefix}queue.waitTime must be within 0..${MAX_WAIT_SECONDS} seconds.`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const queueUrl = topicConfig.getString("queue.url");
|
|
40
|
+
const region = topicConfig.getString("queue.region");
|
|
41
|
+
const visibilityTimeout = readOptionalDuration(
|
|
42
|
+
topicConfig,
|
|
43
|
+
"queue.visibilityTimeout"
|
|
44
|
+
);
|
|
45
|
+
const waitTimeAfterEmptyReceive = luxon.Duration.fromObject(
|
|
46
|
+
(_b2 = readOptionalHumanDuration(topicConfig, "waitTimeAfterEmptyReceive")) != null ? _b2 : DEFAULT_WAIT_TIME_AFTER_EMPTY_RECEIVE
|
|
47
|
+
);
|
|
48
|
+
if (waitTimeAfterEmptyReceive.valueOf() < 0) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`The ${keyPrefix}waitTimeAfterEmptyReceive must not be negative.`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const timeout = (_c = readOptionalDuration(topicConfig, "timeout")) != null ? _c : pollingWaitTime.plus(waitTimeAfterEmptyReceive).plus(luxon.Duration.fromObject({ seconds: 180 }));
|
|
54
|
+
if (timeout.valueOf() <= pollingWaitTime.valueOf() + waitTimeAfterEmptyReceive.valueOf()) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`The ${keyPrefix}timeout must be greater than ${keyPrefix}queue.waitTime + ${keyPrefix}waitTimeAfterEmptyReceive.`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
pollingWaitTime,
|
|
61
|
+
queueUrl,
|
|
62
|
+
region,
|
|
63
|
+
timeout,
|
|
64
|
+
topic,
|
|
65
|
+
visibilityTimeout,
|
|
66
|
+
waitTimeAfterEmptyReceive
|
|
67
|
+
};
|
|
68
|
+
})) != null ? _b : [];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
class AwsSqsConsumingEventPublisher {
|
|
72
|
+
constructor(logger, scheduler, config) {
|
|
73
|
+
this.logger = logger;
|
|
74
|
+
this.scheduler = scheduler;
|
|
75
|
+
var _a;
|
|
76
|
+
this.topic = config.topic;
|
|
77
|
+
this.receiveParams = {
|
|
78
|
+
MaxNumberOfMessages: 10,
|
|
79
|
+
MessageAttributeNames: ["All"],
|
|
80
|
+
QueueUrl: config.queueUrl,
|
|
81
|
+
VisibilityTimeout: (_a = config.visibilityTimeout) == null ? void 0 : _a.as("seconds"),
|
|
82
|
+
WaitTimeSeconds: config.pollingWaitTime.as("seconds")
|
|
83
|
+
};
|
|
84
|
+
this.sqs = new clientSqs.SQSClient({ region: config.region });
|
|
85
|
+
this.queueUrl = config.queueUrl;
|
|
86
|
+
this.taskTimeoutSeconds = config.timeout.as("seconds");
|
|
87
|
+
this.waitTimeAfterEmptyReceiveMs = config.waitTimeAfterEmptyReceive.as("milliseconds");
|
|
88
|
+
}
|
|
89
|
+
static fromConfig(env) {
|
|
90
|
+
return readConfig(env.config).map(
|
|
91
|
+
(config) => new AwsSqsConsumingEventPublisher(env.logger, env.scheduler, config)
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
async setEventBroker(eventBroker) {
|
|
95
|
+
this.eventBroker = eventBroker;
|
|
96
|
+
return this.start();
|
|
97
|
+
}
|
|
98
|
+
async start() {
|
|
99
|
+
const id = `events.awsSqs.publisher:${this.topic}`;
|
|
100
|
+
const logger = this.logger.child({
|
|
101
|
+
class: AwsSqsConsumingEventPublisher.prototype.constructor.name,
|
|
102
|
+
taskId: id
|
|
103
|
+
});
|
|
104
|
+
await this.scheduler.scheduleTask({
|
|
105
|
+
id,
|
|
106
|
+
frequency: { seconds: 0 },
|
|
107
|
+
timeout: { seconds: this.taskTimeoutSeconds },
|
|
108
|
+
scope: "local",
|
|
109
|
+
fn: async () => {
|
|
110
|
+
try {
|
|
111
|
+
const numMessages = await this.consumeMessages();
|
|
112
|
+
if (numMessages === 0) {
|
|
113
|
+
await this.sleep(this.waitTimeAfterEmptyReceiveMs);
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
logger.error(error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
async deleteMessages(messages) {
|
|
122
|
+
if (!messages) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const deleteParams = {
|
|
126
|
+
QueueUrl: this.queueUrl,
|
|
127
|
+
Entries: messages.map((message, index) => {
|
|
128
|
+
var _a;
|
|
129
|
+
return {
|
|
130
|
+
Id: (_a = message.MessageId) != null ? _a : `message-${index}`,
|
|
131
|
+
ReceiptHandle: message.ReceiptHandle
|
|
132
|
+
};
|
|
133
|
+
})
|
|
134
|
+
};
|
|
135
|
+
try {
|
|
136
|
+
const result = await this.sqs.send(
|
|
137
|
+
new clientSqs.DeleteMessageBatchCommand(deleteParams)
|
|
138
|
+
);
|
|
139
|
+
if (result.Failed) {
|
|
140
|
+
this.logger.error(
|
|
141
|
+
`Failed to delete ${result.Failed.length} of ${messages.length} messages from AWS SQS ${this.queueUrl}. First: ${result.Failed[0].Message}`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
} catch (error) {
|
|
145
|
+
this.logger.error(
|
|
146
|
+
`Failed to delete message from AWS SQS ${this.queueUrl}`,
|
|
147
|
+
error
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async consumeMessages() {
|
|
152
|
+
var _a, _b, _c;
|
|
153
|
+
try {
|
|
154
|
+
const data = await this.sqs.send(
|
|
155
|
+
new clientSqs.ReceiveMessageCommand(this.receiveParams)
|
|
156
|
+
);
|
|
157
|
+
(_a = data.Messages) == null ? void 0 : _a.forEach((message) => {
|
|
158
|
+
var _a2;
|
|
159
|
+
const eventPayload = JSON.parse(message.Body);
|
|
160
|
+
const metadata = {};
|
|
161
|
+
Object.keys((_a2 = message.MessageAttributes) != null ? _a2 : {}).forEach((key) => {
|
|
162
|
+
var _a3;
|
|
163
|
+
const attrValue = message.MessageAttributes[key];
|
|
164
|
+
if (!attrValue || !attrValue.DataType || !["String", "Number"].includes(attrValue.DataType)) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const value = (_a3 = attrValue.StringListValues) != null ? _a3 : attrValue.StringValue;
|
|
168
|
+
if (value !== void 0) {
|
|
169
|
+
metadata[key] = value;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
this.eventBroker.publish({
|
|
173
|
+
topic: this.topic,
|
|
174
|
+
eventPayload,
|
|
175
|
+
metadata
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
await this.deleteMessages(data.Messages);
|
|
179
|
+
return (_c = (_b = data.Messages) == null ? void 0 : _b.length) != null ? _c : 0;
|
|
180
|
+
} catch (error) {
|
|
181
|
+
this.logger.error(
|
|
182
|
+
`Failed to receive events from AWS SQS ${this.queueUrl}`,
|
|
183
|
+
error
|
|
184
|
+
);
|
|
185
|
+
return 0;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
sleep(ms) {
|
|
189
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const awsSqsConsumingEventPublisherEventsModule = backendPluginApi.createBackendModule({
|
|
194
|
+
pluginId: "events",
|
|
195
|
+
moduleId: "awsSqsConsumingEventPublisherEventsModule",
|
|
196
|
+
register(env) {
|
|
197
|
+
env.registerInit({
|
|
198
|
+
deps: {
|
|
199
|
+
config: backendPluginApi.configServiceRef,
|
|
200
|
+
events: pluginEventsNode.eventsExtensionPoint,
|
|
201
|
+
logger: backendPluginApi.loggerServiceRef,
|
|
202
|
+
scheduler: backendPluginApi.schedulerServiceRef
|
|
203
|
+
},
|
|
204
|
+
async init({ config, events, logger, scheduler }) {
|
|
205
|
+
const winstonLogger = backendPluginApi.loggerToWinstonLogger(logger);
|
|
206
|
+
const sqs = AwsSqsConsumingEventPublisher.fromConfig({
|
|
207
|
+
config,
|
|
208
|
+
logger: winstonLogger,
|
|
209
|
+
scheduler
|
|
210
|
+
});
|
|
211
|
+
events.addPublishers(sqs);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
exports.AwsSqsConsumingEventPublisher = AwsSqsConsumingEventPublisher;
|
|
218
|
+
exports.awsSqsConsumingEventPublisherEventsModule = awsSqsConsumingEventPublisherEventsModule;
|
|
219
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/publisher/config.ts","../src/publisher/AwsSqsConsumingEventPublisher.ts","../src/service/AwsSqsConsumingEventPublisherEventsModule.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { HumanDuration, JsonObject } from '@backstage/types';\nimport { Duration } from 'luxon';\n\nconst CONFIG_PREFIX_MODULE = 'events.modules.awsSqs.';\nconst CONFIG_PREFIX_PUBLISHER = `${CONFIG_PREFIX_MODULE}awsSqsConsumingEventPublisher.`;\nconst DEFAULT_WAIT_TIME_AFTER_EMPTY_RECEIVE = { minutes: 1 };\nconst MAX_WAIT_SECONDS = 20;\n\nexport interface AwsSqsEventSourceConfig {\n pollingWaitTime: Duration;\n queueUrl: string;\n region: string;\n timeout: Duration;\n topic: string;\n visibilityTimeout?: Duration;\n waitTimeAfterEmptyReceive: Duration;\n}\n\n// TODO(pjungermann): validation could be improved similar to `convertToHumanDuration` at @backstage/backend-tasks\nfunction readOptionalHumanDuration(\n config: Config,\n key: string,\n): HumanDuration | undefined {\n return config.getOptional<JsonObject>(key) as HumanDuration;\n}\n\nfunction readOptionalDuration(\n config: Config,\n key: string,\n): Duration | undefined {\n const duration = readOptionalHumanDuration(config, key);\n return duration ? Duration.fromObject(duration) : undefined;\n}\n\nexport function readConfig(config: Config): AwsSqsEventSourceConfig[] {\n const key = `${CONFIG_PREFIX_PUBLISHER}topics`;\n const topics = config.getOptionalConfig(key);\n\n return (\n topics?.keys()?.map(topic => {\n const topicConfig = topics.getConfig(topic);\n const keyPrefix = `${key}.${topic}.`;\n\n // queue config:\n const pollingWaitTime = Duration.fromObject(\n readOptionalHumanDuration(topicConfig, 'queue.waitTime') ?? {\n seconds: MAX_WAIT_SECONDS,\n },\n );\n if (\n pollingWaitTime.valueOf() < 0 ||\n pollingWaitTime.as('seconds') > MAX_WAIT_SECONDS\n ) {\n throw new Error(\n `${keyPrefix}queue.waitTime must be within 0..${MAX_WAIT_SECONDS} seconds.`,\n );\n }\n const queueUrl = topicConfig.getString('queue.url');\n const region = topicConfig.getString('queue.region');\n const visibilityTimeout = readOptionalDuration(\n topicConfig,\n 'queue.visibilityTimeout',\n );\n\n // task:\n const waitTimeAfterEmptyReceive = Duration.fromObject(\n readOptionalHumanDuration(topicConfig, 'waitTimeAfterEmptyReceive') ??\n DEFAULT_WAIT_TIME_AFTER_EMPTY_RECEIVE,\n );\n if (waitTimeAfterEmptyReceive.valueOf() < 0) {\n throw new Error(\n `The ${keyPrefix}waitTimeAfterEmptyReceive must not be negative.`,\n );\n }\n const timeout =\n readOptionalDuration(topicConfig, 'timeout') ??\n pollingWaitTime\n .plus(waitTimeAfterEmptyReceive)\n .plus(Duration.fromObject({ seconds: 180 }));\n if (\n timeout.valueOf() <=\n pollingWaitTime.valueOf() + waitTimeAfterEmptyReceive.valueOf()\n ) {\n throw new Error(\n `The ${keyPrefix}timeout must be greater than ${keyPrefix}queue.waitTime + ${keyPrefix}waitTimeAfterEmptyReceive.`,\n );\n }\n\n return {\n pollingWaitTime,\n queueUrl,\n region,\n timeout,\n topic,\n visibilityTimeout,\n waitTimeAfterEmptyReceive,\n };\n }) ?? []\n );\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n DeleteMessageBatchCommand,\n Message,\n ReceiveMessageCommand,\n ReceiveMessageCommandInput,\n SQSClient,\n} from '@aws-sdk/client-sqs';\nimport { PluginTaskScheduler } from '@backstage/backend-tasks';\nimport { Config } from '@backstage/config';\nimport { EventBroker, EventPublisher } from '@backstage/plugin-events-node';\nimport { Logger } from 'winston';\nimport { AwsSqsEventSourceConfig, readConfig } from './config';\n\n/**\n * Publishes events received from an AWS SQS queue.\n * The message payload will be used as event payload and passed to registered subscribers.\n *\n * @public\n */\n// TODO(pjungermann): add prom metrics? (see plugins/catalog-backend/src/util/metrics.ts, etc.)\nexport class AwsSqsConsumingEventPublisher implements EventPublisher {\n private readonly topic: string;\n private readonly receiveParams: ReceiveMessageCommandInput;\n private readonly sqs: SQSClient;\n private readonly queueUrl: string;\n private readonly taskTimeoutSeconds: number;\n private readonly waitTimeAfterEmptyReceiveMs;\n private eventBroker?: EventBroker;\n\n static fromConfig(env: {\n config: Config;\n logger: Logger;\n scheduler: PluginTaskScheduler;\n }): AwsSqsConsumingEventPublisher[] {\n return readConfig(env.config).map(\n config =>\n new AwsSqsConsumingEventPublisher(env.logger, env.scheduler, config),\n );\n }\n\n private constructor(\n private readonly logger: Logger,\n private readonly scheduler: PluginTaskScheduler,\n config: AwsSqsEventSourceConfig,\n ) {\n this.topic = config.topic;\n\n this.receiveParams = {\n MaxNumberOfMessages: 10,\n MessageAttributeNames: ['All'],\n QueueUrl: config.queueUrl,\n VisibilityTimeout: config.visibilityTimeout?.as('seconds'),\n WaitTimeSeconds: config.pollingWaitTime.as('seconds'),\n };\n this.sqs = new SQSClient({ region: config.region });\n this.queueUrl = config.queueUrl;\n\n this.taskTimeoutSeconds = config.timeout.as('seconds');\n this.waitTimeAfterEmptyReceiveMs =\n config.waitTimeAfterEmptyReceive.as('milliseconds');\n }\n\n async setEventBroker(eventBroker: EventBroker): Promise<void> {\n this.eventBroker = eventBroker;\n return this.start();\n }\n\n private async start(): Promise<void> {\n const id = `events.awsSqs.publisher:${this.topic}`;\n const logger = this.logger.child({\n class: AwsSqsConsumingEventPublisher.prototype.constructor.name,\n taskId: id,\n });\n\n await this.scheduler.scheduleTask({\n id: id,\n frequency: { seconds: 0 },\n timeout: { seconds: this.taskTimeoutSeconds },\n scope: 'local',\n fn: async () => {\n try {\n const numMessages = await this.consumeMessages();\n if (numMessages === 0) {\n await this.sleep(this.waitTimeAfterEmptyReceiveMs);\n }\n } catch (error) {\n logger.error(error);\n }\n },\n });\n }\n\n private async deleteMessages(messages?: Message[]): Promise<void> {\n if (!messages) {\n return;\n }\n\n const deleteParams = {\n QueueUrl: this.queueUrl,\n Entries: messages.map((message, index) => {\n return {\n Id: message.MessageId ?? `message-${index}`,\n ReceiptHandle: message.ReceiptHandle,\n };\n }),\n };\n\n try {\n const result = await this.sqs.send(\n new DeleteMessageBatchCommand(deleteParams),\n );\n if (result.Failed) {\n this.logger.error(\n `Failed to delete ${result.Failed!.length} of ${\n messages.length\n } messages from AWS SQS ${this.queueUrl}. First: ${\n result.Failed[0].Message\n }`,\n );\n }\n } catch (error) {\n this.logger.error(\n `Failed to delete message from AWS SQS ${this.queueUrl}`,\n error,\n );\n }\n }\n\n private async consumeMessages(): Promise<number> {\n try {\n const data = await this.sqs.send(\n new ReceiveMessageCommand(this.receiveParams),\n );\n\n data.Messages?.forEach(message => {\n const eventPayload = JSON.parse(message.Body!);\n\n const metadata: Record<string, string | string[]> = {};\n Object.keys(message.MessageAttributes ?? {}).forEach(key => {\n const attrValue = message.MessageAttributes![key];\n if (\n !attrValue ||\n !attrValue.DataType ||\n !['String', 'Number'].includes(attrValue.DataType)\n ) {\n return;\n }\n\n const value = attrValue.StringListValues ?? attrValue.StringValue;\n if (value !== undefined) {\n metadata[key] = value;\n }\n });\n\n this.eventBroker!.publish({\n topic: this.topic,\n eventPayload,\n metadata,\n });\n });\n await this.deleteMessages(data.Messages);\n return data.Messages?.length ?? 0;\n } catch (error) {\n this.logger.error(\n `Failed to receive events from AWS SQS ${this.queueUrl}`,\n error,\n );\n return 0;\n }\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise<void>(resolve => setTimeout(resolve, ms));\n }\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n configServiceRef,\n createBackendModule,\n loggerServiceRef,\n loggerToWinstonLogger,\n schedulerServiceRef,\n} from '@backstage/backend-plugin-api';\nimport { eventsExtensionPoint } from '@backstage/plugin-events-node';\nimport { AwsSqsConsumingEventPublisher } from '../publisher/AwsSqsConsumingEventPublisher';\n\n/**\n * AWS SQS module for the Events plugin.\n *\n * @alpha\n */\nexport const awsSqsConsumingEventPublisherEventsModule = createBackendModule({\n pluginId: 'events',\n moduleId: 'awsSqsConsumingEventPublisherEventsModule',\n register(env) {\n env.registerInit({\n deps: {\n config: configServiceRef,\n events: eventsExtensionPoint,\n logger: loggerServiceRef,\n scheduler: schedulerServiceRef,\n },\n async init({ config, events, logger, scheduler }) {\n const winstonLogger = loggerToWinstonLogger(logger);\n const sqs = AwsSqsConsumingEventPublisher.fromConfig({\n config: config,\n logger: winstonLogger,\n scheduler: scheduler,\n });\n\n events.addPublishers(sqs);\n },\n });\n },\n});\n"],"names":["Duration","_a","_b","SQSClient","DeleteMessageBatchCommand","ReceiveMessageCommand","createBackendModule","configServiceRef","eventsExtensionPoint","loggerServiceRef","schedulerServiceRef","loggerToWinstonLogger"],"mappings":";;;;;;;;;AAoBA,MAAM,oBAAuB,GAAA,wBAAA,CAAA;AAC7B,MAAM,0BAA0B,CAAG,EAAA,oBAAA,CAAA,8BAAA,CAAA,CAAA;AACnC,MAAM,qCAAA,GAAwC,EAAE,OAAA,EAAS,CAAE,EAAA,CAAA;AAC3D,MAAM,gBAAmB,GAAA,EAAA,CAAA;AAazB,SAAS,yBAAA,CACP,QACA,GAC2B,E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;AAAA,GACH;AACF,CAAC;;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The module "sqs" for the Backstage backend plugin "events"
|
|
3
|
+
* adding an AWS SQS-based publisher,
|
|
4
|
+
* receiving events from an AWS SQS queue and passing it to the
|
|
5
|
+
* internal event broker.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { BackendFeature } from '@backstage/backend-plugin-api';
|
|
11
|
+
import { Config } from '@backstage/config';
|
|
12
|
+
import { EventBroker } from '@backstage/plugin-events-node';
|
|
13
|
+
import { EventPublisher } from '@backstage/plugin-events-node';
|
|
14
|
+
import { Logger } from 'winston';
|
|
15
|
+
import { PluginTaskScheduler } from '@backstage/backend-tasks';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Publishes events received from an AWS SQS queue.
|
|
19
|
+
* The message payload will be used as event payload and passed to registered subscribers.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare class AwsSqsConsumingEventPublisher implements EventPublisher {
|
|
24
|
+
private readonly logger;
|
|
25
|
+
private readonly scheduler;
|
|
26
|
+
private readonly topic;
|
|
27
|
+
private readonly receiveParams;
|
|
28
|
+
private readonly sqs;
|
|
29
|
+
private readonly queueUrl;
|
|
30
|
+
private readonly taskTimeoutSeconds;
|
|
31
|
+
private readonly waitTimeAfterEmptyReceiveMs;
|
|
32
|
+
private eventBroker?;
|
|
33
|
+
static fromConfig(env: {
|
|
34
|
+
config: Config;
|
|
35
|
+
logger: Logger;
|
|
36
|
+
scheduler: PluginTaskScheduler;
|
|
37
|
+
}): AwsSqsConsumingEventPublisher[];
|
|
38
|
+
private constructor();
|
|
39
|
+
setEventBroker(eventBroker: EventBroker): Promise<void>;
|
|
40
|
+
private start;
|
|
41
|
+
private deleteMessages;
|
|
42
|
+
private consumeMessages;
|
|
43
|
+
private sleep;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* Excluded from this release type: awsSqsConsumingEventPublisherEventsModule */
|
|
47
|
+
|
|
48
|
+
export { }
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@backstage/plugin-events-backend-module-aws-sqs",
|
|
3
|
+
"version": "0.0.0-nightly-20221115024001",
|
|
4
|
+
"main": "dist/index.cjs.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public",
|
|
9
|
+
"alphaTypes": "dist/index.alpha.d.ts",
|
|
10
|
+
"main": "dist/index.cjs.js",
|
|
11
|
+
"types": "dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"backstage": {
|
|
14
|
+
"role": "backend-plugin-module"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"start": "backstage-cli package start",
|
|
18
|
+
"build": "backstage-cli package build --experimental-type-build",
|
|
19
|
+
"lint": "backstage-cli package lint",
|
|
20
|
+
"test": "backstage-cli package test",
|
|
21
|
+
"clean": "backstage-cli package clean",
|
|
22
|
+
"prepack": "backstage-cli package prepack",
|
|
23
|
+
"postpack": "backstage-cli package postpack"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@aws-sdk/client-sqs": "^3.0.0",
|
|
27
|
+
"@backstage/backend-plugin-api": "^0.0.0-nightly-20221115024001",
|
|
28
|
+
"@backstage/backend-tasks": "^0.0.0-nightly-20221115024001",
|
|
29
|
+
"@backstage/config": "^0.0.0-nightly-20221115024001",
|
|
30
|
+
"@backstage/plugin-events-node": "^0.0.0-nightly-20221115024001",
|
|
31
|
+
"@backstage/types": "^0.0.0-nightly-20221115024001",
|
|
32
|
+
"luxon": "^3.0.0",
|
|
33
|
+
"winston": "^3.2.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@backstage/backend-common": "^0.0.0-nightly-20221115024001",
|
|
37
|
+
"@backstage/backend-test-utils": "^0.0.0-nightly-20221115024001",
|
|
38
|
+
"@backstage/cli": "^0.0.0-nightly-20221115024001",
|
|
39
|
+
"@backstage/plugin-events-backend-test-utils": "^0.0.0-nightly-20221115024001",
|
|
40
|
+
"aws-sdk-client-mock": "^2.0.0"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"alpha",
|
|
44
|
+
"config.d.ts",
|
|
45
|
+
"dist"
|
|
46
|
+
],
|
|
47
|
+
"configSchema": "config.d.ts"
|
|
48
|
+
}
|