event-message-broker
Version:
A Message Bus that uses AWS Stack and RabbitMQ
58 lines (57 loc) • 2.6 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { startSpan } from "../../application/utils/o11y";
import { SNSInfrastructure } from "./SNSInfrastructure";
import { SQSInfrastructure } from "./SQSInfrastructure";
import { SpanKind, SpanStatusCode } from '@opentelemetry/api';
export class AWSInfrastructure {
constructor() {
this.sns = new SNSInfrastructure();
this.sqs = new SQSInfrastructure();
}
createQueue(queueName) {
return __awaiter(this, void 0, void 0, function* () {
const span = startSpan(this.createQueue.name, SpanKind.SERVER);
try {
yield this.sqs.create(queueName, span);
}
catch (error) {
span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
span.recordException(error);
throw error;
}
finally {
span.end();
}
});
}
bindTopic(binder) {
return __awaiter(this, void 0, void 0, function* () {
const span = startSpan(this.createQueue.name, SpanKind.SERVER);
try {
const topicOutput = yield this.sns.create(binder.TopicName, span);
if (topicOutput.Created) {
const policy = yield this.sqs.linkTopicQueuePolicy(binder.QueueName, binder.TopicName);
span.addEvent('queue-policy-linked', { policy: JSON.stringify(policy) });
const arn = yield this.sns.subscribeEndpoints(binder.TopicName, binder.QueueName);
span.addEvent('endpoint-subscribed', { arn });
}
}
catch (error) {
span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
span.recordException(error);
throw error;
}
finally {
span.end();
}
});
}
}