exodus-framework 2.0.718 → 2.0.720
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,6 +1,6 @@
|
|
1
|
+
import client, { Channel, Connection } from 'amqplib';
|
1
2
|
import Service from '../app/service';
|
2
|
-
import { TMessageQueueDataMap, TMessagingQueueEventList
|
3
|
-
import { Channel, Connection } from 'amqplib';
|
3
|
+
import { TMessageQueueDataMap, TMessagingQueueEventList } from '../contracts';
|
4
4
|
import { IService } from '../contracts/service';
|
5
5
|
declare class RabbitMQService extends Service implements IService {
|
6
6
|
connection: Connection;
|
@@ -9,10 +9,10 @@ declare class RabbitMQService extends Service implements IService {
|
|
9
9
|
private mainRouter;
|
10
10
|
constructor();
|
11
11
|
init(): Promise<void>;
|
12
|
-
registerHandles(): void
|
12
|
+
registerHandles(): Promise<void>;
|
13
13
|
connect(): Promise<void>;
|
14
|
-
sendToQueue<K extends keyof TMessageQueueDataMap>(
|
15
|
-
consumeFromQueue
|
14
|
+
sendToQueue<K extends keyof TMessageQueueDataMap>(topic: K, data: TMessageQueueDataMap[K]): Promise<void>;
|
15
|
+
consumeFromQueue(msg: client.ConsumeMessage): Promise<void>;
|
16
16
|
registerRouter(router: TMessagingQueueEventList): void;
|
17
17
|
}
|
18
18
|
export default RabbitMQService;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rabitmq.d.ts","sourceRoot":"","sources":["../../src/services/rabitmq.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"rabitmq.d.ts","sourceRoot":"","sources":["../../src/services/rabitmq.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEtD,OAAO,OAAO,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAWhD,cAAM,eAAgB,SAAQ,OAAQ,YAAW,QAAQ;IACvD,UAAU,EAAG,UAAU,CAAC;IACxB,OAAO,EAAG,OAAO,CAAC;IAClB,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,UAAU,CAA2B;;IAMvC,IAAI;IAMJ,eAAe;IAiBf,OAAO;IAwBP,WAAW,CAAC,CAAC,SAAS,MAAM,oBAAoB,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,oBAAoB,CAAC,CAAC,CAAC;IAuBzF,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,cAAc;IAgBjD,cAAc,CAAC,MAAM,EAAE,wBAAwB;CAGhD;AAED,eAAe,eAAe,CAAC"}
|
package/lib/services/rabitmq.js
CHANGED
@@ -4,11 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
var _service = _interopRequireDefault(require("../app/service"));
|
8
7
|
var _amqplib = _interopRequireDefault(require("amqplib"));
|
9
|
-
var _error = require("./error");
|
10
8
|
var _app = require("../app");
|
9
|
+
var _service = _interopRequireDefault(require("../app/service"));
|
11
10
|
var _messaging = _interopRequireDefault(require("../routes/messaging"));
|
11
|
+
var _error = require("./error");
|
12
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
13
13
|
/**
|
14
14
|
* Serviço de Mensageria
|
@@ -36,10 +36,13 @@ class RabbitMQService extends _service.default {
|
|
36
36
|
}; // # Register native router
|
37
37
|
await this.connect();
|
38
38
|
}
|
39
|
-
registerHandles() {
|
40
|
-
for (const
|
41
|
-
this.
|
39
|
+
async registerHandles() {
|
40
|
+
for (const topic of Object.keys(this.mainRouter)) {
|
41
|
+
await this.channel.bindQueue(_app.Core.settings.getAppication().appId, _app.Core.settings.getMessaging().exchanges.main, topic);
|
42
42
|
}
|
43
|
+
await this.channel.consume(_app.Core.settings.getAppication().appId, this.consumeFromQueue.bind(this), {
|
44
|
+
noAck: false
|
45
|
+
});
|
43
46
|
}
|
44
47
|
async connect() {
|
45
48
|
if (this.connected && this.channel) return;else this.connected = true;
|
@@ -48,7 +51,10 @@ class RabbitMQService extends _service.default {
|
|
48
51
|
this.connection = await _amqplib.default.connect(`amqp://${_app.Core.settings.getMessaging().user}:${_app.Core.settings.getMessaging().pass}@${_app.Core.settings.getMessaging().host}:5672`);
|
49
52
|
this.log(`✅ Rabbit MQ Connection is ready`);
|
50
53
|
this.channel = await this.connection.createChannel();
|
51
|
-
this.channel.assertExchange(_app.Core.settings.getMessaging().exchanges.main, 'topic', {
|
54
|
+
await this.channel.assertExchange(_app.Core.settings.getMessaging().exchanges.main, 'topic', {
|
55
|
+
durable: true
|
56
|
+
});
|
57
|
+
await this.channel.assertQueue(_app.Core.settings.getAppication().appId, {
|
52
58
|
durable: true
|
53
59
|
});
|
54
60
|
this.log(`🛸 Created RabbitMQ Channel successfully`);
|
@@ -59,12 +65,12 @@ class RabbitMQService extends _service.default {
|
|
59
65
|
}
|
60
66
|
|
61
67
|
//# Common
|
62
|
-
async sendToQueue(
|
68
|
+
async sendToQueue(topic, data) {
|
63
69
|
try {
|
64
70
|
if (!this.channel) {
|
65
71
|
await this.connect();
|
66
72
|
}
|
67
|
-
this.channel.publish(_app.Core.settings.getMessaging().exchanges.main,
|
73
|
+
this.channel.publish(_app.Core.settings.getMessaging().exchanges.main, topic, Buffer.from(JSON.stringify(data)), {
|
68
74
|
messageId: new Date().toDateString(),
|
69
75
|
persistent: true
|
70
76
|
});
|
@@ -77,26 +83,19 @@ class RabbitMQService extends _service.default {
|
|
77
83
|
new _error.ApplicationException('Houve um erro no envio de uma mensagem para o rabitmq', error);
|
78
84
|
}
|
79
85
|
}
|
80
|
-
async consumeFromQueue(
|
81
|
-
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
this.channel.ack(msg);
|
94
|
-
new _error.ApplicationException('Error on parsing message from queue', error);
|
95
|
-
}
|
96
|
-
}
|
97
|
-
}, {
|
98
|
-
noAck: false
|
99
|
-
});
|
86
|
+
async consumeFromQueue(msg) {
|
87
|
+
if (!msg) {
|
88
|
+
return this.log(`Invalid incoming message in`);
|
89
|
+
}
|
90
|
+
try {
|
91
|
+
const topic = msg.fields.routingKey;
|
92
|
+
const content = JSON.parse(msg.content.toString());
|
93
|
+
const callBack = this.mainRouter[topic];
|
94
|
+
callBack(content, () => this.channel.ack(msg));
|
95
|
+
} catch (error) {
|
96
|
+
this.channel.ack(msg);
|
97
|
+
new _error.ApplicationException('Error on parsing message from queue', error);
|
98
|
+
}
|
100
99
|
}
|
101
100
|
registerRouter(router) {
|
102
101
|
this.mainRouter = router;
|