amqplib-init 1.0.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.
- package/index.js +87 -0
- package/package.json +15 -0
package/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const amqp = require('amqplib');
|
|
2
|
+
const shelljs = require('shelljs');
|
|
3
|
+
const sleep = (time) => {
|
|
4
|
+
return new Promise(resolve => {
|
|
5
|
+
setTimeout(_ => resolve(), time * 1000)
|
|
6
|
+
})
|
|
7
|
+
};
|
|
8
|
+
module.exports = {
|
|
9
|
+
/**
|
|
10
|
+
* 消息队列设置
|
|
11
|
+
* @returns {Promise<unknown>}
|
|
12
|
+
* @param option
|
|
13
|
+
*/
|
|
14
|
+
init (option){
|
|
15
|
+
const {
|
|
16
|
+
channelName = 'node-test-channel',
|
|
17
|
+
prefetch = 1,
|
|
18
|
+
callback = Function,
|
|
19
|
+
pmId = 0,
|
|
20
|
+
finish = Function,
|
|
21
|
+
amqpLink = ``,
|
|
22
|
+
heartbeat = 5,
|
|
23
|
+
timeout = 120000,
|
|
24
|
+
} = option;
|
|
25
|
+
const durable = true;
|
|
26
|
+
console.log(`🚀 队列已执行...`);
|
|
27
|
+
let connect = null;
|
|
28
|
+
const amqpInit = async (reload = false) => {
|
|
29
|
+
await sleep(1); // 等待一秒钟
|
|
30
|
+
connect = await amqp.connect(amqpLink, {
|
|
31
|
+
heartbeat, // 心跳检测5秒
|
|
32
|
+
timeout, // 设置连接超时时间为120秒
|
|
33
|
+
});
|
|
34
|
+
if (pmId > 0 && reload){
|
|
35
|
+
console.log(`‼️ 服务即将重启`);
|
|
36
|
+
shelljs.exec(`pm2 reload ${pmId}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return new Promise(async (resolve) => {
|
|
40
|
+
await amqpInit();
|
|
41
|
+
console.log(`✳️->RabbitMQ链接成功`);
|
|
42
|
+
const channel = await connect.createChannel();
|
|
43
|
+
console.log(`✳️->数据队列名称`, channelName);
|
|
44
|
+
console.log(`✳️->是否持久化`, durable);
|
|
45
|
+
const { queue } = await channel.assertQueue(channelName, { durable });
|
|
46
|
+
// 一次性读取一条;
|
|
47
|
+
await channel.prefetch(prefetch);
|
|
48
|
+
console.log(`✳️->一次取多少条`, prefetch);
|
|
49
|
+
console.log(`✳️->消息队列: ${channelName} 正在运行中...`);
|
|
50
|
+
await channel.consume(queue, async (msg) => {
|
|
51
|
+
if (msg !== null) {
|
|
52
|
+
try {
|
|
53
|
+
await channel.assertQueue(channelName, { durable });
|
|
54
|
+
const content = JSON.parse(msg?.content?.toString());
|
|
55
|
+
console.log(`✳️->队列收到新消息: ${channelName}`);
|
|
56
|
+
const startTime = new Date().getTime();
|
|
57
|
+
callback(content).then(()=> {
|
|
58
|
+
try {
|
|
59
|
+
channel.ack(msg);
|
|
60
|
+
const endTime = (new Date().getTime() - startTime);
|
|
61
|
+
console.log(`✳️->消息处理完成, 共耗时: ${endTime}ms`);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
console.log(`队列消费遇到异常`, e);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
} catch (e) {
|
|
67
|
+
console.log(`队列遇到异常`, e);
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
console.log(`消息异常`, msg);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
// 队列遇到错误
|
|
74
|
+
connect.on('error', (err) => {
|
|
75
|
+
console.log('Connection error', err.message);
|
|
76
|
+
amqpInit(true);
|
|
77
|
+
});
|
|
78
|
+
// 队列被关闭
|
|
79
|
+
connect.on('close', function() {
|
|
80
|
+
console.log('Connection to RabbitMQ is closed');
|
|
81
|
+
amqpInit(true);
|
|
82
|
+
});
|
|
83
|
+
console.log(`✳️-队列已启动: finish~`);
|
|
84
|
+
finish && finish();
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "amqplib-init",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "消息队列初始化",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node index.js"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"amqplib": "^0.10.4",
|
|
13
|
+
"shelljs": "^0.8.5"
|
|
14
|
+
}
|
|
15
|
+
}
|