@betterinternship/core 2.2.3 → 2.3.0
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.
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import * as amqp from 'amqplib';
|
|
2
2
|
export declare const QueueName: (...args: string[]) => string;
|
|
3
|
-
export declare
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
export declare class BrokerAPI {
|
|
4
|
+
connection: amqp.ChannelModel;
|
|
5
|
+
channels: Record<string, amqp.Channel>;
|
|
6
|
+
private static __instance;
|
|
7
|
+
private constructor();
|
|
8
|
+
static instance(): Promise<BrokerAPI>;
|
|
9
|
+
assertQueue(queueId: string, options?: amqp.Options.AssertQueue): Promise<amqp.Replies.AssertQueue>;
|
|
10
|
+
sendToQueue(queueId: string, content: any): boolean;
|
|
11
|
+
readFromQueue<T>(queueId: string): Promise<null>;
|
|
12
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as amqp from 'amqplib';
|
|
2
2
|
import { ENV } from '../env.js';
|
|
3
|
+
const RABBITMQ_ENABLED = ENV.RABBITMQ_ENABLED;
|
|
3
4
|
const RABBITMQ_URL = ENV.RABBITMQ_URL;
|
|
4
|
-
if (!RABBITMQ_URL &&
|
|
5
|
+
if (!RABBITMQ_URL && RABBITMQ_ENABLED)
|
|
5
6
|
throw new Error('[ERROR:ENV] Missing RabbitMQ configuration.');
|
|
6
7
|
export const QueueName = (...args) => {
|
|
7
8
|
if (ENV.ENVIRONMENT === 'production')
|
|
@@ -9,30 +10,33 @@ export const QueueName = (...args) => {
|
|
|
9
10
|
else
|
|
10
11
|
return ['dev', ...args].join('.');
|
|
11
12
|
};
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
export class BrokerAPI {
|
|
14
|
+
connection;
|
|
15
|
+
channels;
|
|
16
|
+
static __instance;
|
|
17
|
+
constructor() { }
|
|
18
|
+
static async instance() {
|
|
19
|
+
if (BrokerAPI.__instance || !RABBITMQ_ENABLED)
|
|
20
|
+
return BrokerAPI.__instance;
|
|
21
|
+
BrokerAPI.__instance = new BrokerAPI();
|
|
22
|
+
BrokerAPI.__instance.connection = await amqp.connect(RABBITMQ_URL);
|
|
23
|
+
BrokerAPI.__instance.channels = {
|
|
24
|
+
asserter: await BrokerAPI.__instance.connection.createChannel(),
|
|
25
|
+
reader: await BrokerAPI.__instance.connection.createChannel(),
|
|
26
|
+
writer: await BrokerAPI.__instance.connection.createChannel(),
|
|
18
27
|
};
|
|
28
|
+
return BrokerAPI.__instance;
|
|
19
29
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
asserter: await connection.createChannel(),
|
|
23
|
-
reader: await connection.createChannel(),
|
|
24
|
-
writer: await connection.createChannel(),
|
|
25
|
-
};
|
|
26
|
-
const assertQueue = async (queueId, options) => {
|
|
27
|
-
const result = await channels.asserter.assertQueue(queueId, {
|
|
30
|
+
async assertQueue(queueId, options) {
|
|
31
|
+
const result = await this.channels.asserter.assertQueue(queueId, {
|
|
28
32
|
durable: true,
|
|
29
33
|
...options,
|
|
30
34
|
});
|
|
31
35
|
console.log(`[BROKER] Queue "${queueId}" has been asserted.`);
|
|
32
36
|
return result;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const result = channels.writer.sendToQueue(queueId, Buffer.from(JSON.stringify(content)));
|
|
37
|
+
}
|
|
38
|
+
sendToQueue(queueId, content) {
|
|
39
|
+
const result = this.channels.writer.sendToQueue(queueId, Buffer.from(JSON.stringify(content)));
|
|
36
40
|
if (result) {
|
|
37
41
|
console.log(`[BROKER] Message sent to queue "${queueId}": ${JSON.stringify(content)}.`);
|
|
38
42
|
}
|
|
@@ -40,18 +44,18 @@ export const BrokerAPI = await (async () => {
|
|
|
40
44
|
console.log(`[BROKER] Message could not sent to queue "${queueId}": ${JSON.stringify(content)}.`);
|
|
41
45
|
}
|
|
42
46
|
return result;
|
|
43
|
-
}
|
|
44
|
-
|
|
47
|
+
}
|
|
48
|
+
async readFromQueue(queueId) {
|
|
45
49
|
const resolveFromQueue = (message) => {
|
|
46
|
-
channels.writer.ack(message);
|
|
50
|
+
this.channels.writer.ack(message);
|
|
47
51
|
console.log(`[BROKER] Message "${message.properties.messageId}" resolved from queue "${queueId}".`);
|
|
48
52
|
};
|
|
49
53
|
const rejectFromQueue = (message) => {
|
|
50
|
-
channels.writer.nack(message);
|
|
54
|
+
this.channels.writer.nack(message);
|
|
51
55
|
console.log(`[BROKER] Message "${message.properties.messageId}" rejected from queue "${queueId}".`);
|
|
52
56
|
};
|
|
53
57
|
let result = null;
|
|
54
|
-
await channels.reader.consume(queueId, (message) => {
|
|
58
|
+
await this.channels.reader.consume(queueId, (message) => {
|
|
55
59
|
const content = message?.content?.toString?.();
|
|
56
60
|
result = {
|
|
57
61
|
content: content ? JSON.parse(content) : null,
|
|
@@ -61,11 +65,6 @@ export const BrokerAPI = await (async () => {
|
|
|
61
65
|
return message ? result : null;
|
|
62
66
|
});
|
|
63
67
|
return result;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
assertQueue,
|
|
67
|
-
sendToQueue,
|
|
68
|
-
readFromQueue,
|
|
69
|
-
};
|
|
70
|
-
})();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
71
70
|
//# sourceMappingURL=broker.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broker.js","sourceRoot":"","sources":["../../../lib/broker/broker.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,IAAI,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAEhC,MAAM,YAAY,GAAG,GAAG,CAAC,YAAY,CAAC;AACtC,IAAI,CAAC,YAAY,IAAI,
|
|
1
|
+
{"version":3,"file":"broker.js","sourceRoot":"","s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