-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
63 lines (63 loc) · 5.22 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rabbitmq_1 = require("./rabbitmq");
const loader_1 = require("./loader");
const assert = require("assert");
const qs = require("querystring");
exports.default = app => {
if (app.config.rabbitmq) {
app.addSingleton('rabbitmq', createRabbitMQ);
}
};
function registerConsumer(app, client, consumers) {
for (const key in consumers) {
const consumer = consumers[key];
const { queue, routingKey } = consumer.consumer;
if (consumer.consumer.disable) {
continue;
}
app.logger.info('[egg-rabbitmq]: register consumer %s', consumer.key);
const ctx = app.createAnonymousContext({
method: 'CONSUMER',
url: `/__consumer?path=${key}&${qs.stringify(consumer.consumer)}`,
});
const subscribe = consumer.subscribe(ctx);
client.registerSubscribe(queue, routingKey, subscribe);
}
}
function createRabbitMQ(config, app) {
assert(config.url && config.exchanges && config.queues);
const client = new rabbitmq_1.default(config);
const consumers = loader_1.default(app);
client.on('connect', connection => {
app.logger.info('[egg-rabbitmq] server connected! connection=' +
connection.connection.stream.localAddress +
':' +
connection.connection.stream.localPort);
});
client.on('close', error => {
app.logger.info('[egg-rabbitmq] connection closed due to error:', error);
});
client.on('error', error => {
app.logger.error('[egg-rabbitmq] connection error:', error);
});
client.on('ch_open', _channel => {
app.logger.info('[egg-rabbitmq] channel opened!');
});
client.on('ch_close', () => {
app.logger.info('[egg-rabbitmq] channel closed');
});
client.on('ch_error', error => {
app.logger.error('[egg-rabbitmq] channel error:', error);
});
client.on('ch_drain', () => {
app.logger.error('[egg-rabbitmq] channel drain event');
});
app.beforeStart(async () => {
// 注册消费者
registerConsumer(app, client, consumers);
await client.init();
});
return client;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZX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