amqplib-init 1.1.8 → 1.1.10

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.
Files changed (2) hide show
  1. package/index.js +44 -12
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -2,10 +2,11 @@ const amqp = require('amqplib');
2
2
  const shelljs = require('shelljs');
3
3
  const log = require('chalk-style')
4
4
  const happy = require('happy-help');
5
+ const axios = require('axios'); // 新增 axios 依赖
5
6
 
6
7
  module.exports = {
7
8
  // 初始化函数
8
- init(option) {
9
+ async init(option) { // 改为 async 函数
9
10
  const {
10
11
  channelName = 'node-test-channel', // 频道名称,默认为'node-test-channel'
11
12
  prefetch = 1, // 预取计数,默认为1
@@ -13,6 +14,7 @@ module.exports = {
13
14
  callback = () => {}, // 消息处理回调,默认为空函数
14
15
  finish = () => {}, // 初始化完成回调,默认为空函数
15
16
  amqpLink = '', // RabbitMQ连接地址
17
+ amqpAutoLink = '', // 自动获取连接信息的HTTPS链接
16
18
  heartbeat = 5, // 心跳间隔,单位为秒,默认为2秒
17
19
  timeout = 2000, // 连接超时时间,单位为毫秒,默认为10000毫秒
18
20
  delay = 0, // 消息处理完成后延迟ack的时间,单位为毫秒,默认为0毫秒
@@ -24,11 +26,31 @@ module.exports = {
24
26
  let connection = null; // RabbitMQ连接
25
27
  let channel = null; // RabbitMQ频道
26
28
 
29
+ // 处理 amqpAutoLink 自动获取连接信息
30
+ let finalAmqpLink = amqpLink; // 最终使用的连接地址
31
+ if (amqpAutoLink) {
32
+ try {
33
+ log.log(`正在从 ${amqpAutoLink} 获取连接信息...`);
34
+ const response = await axios.post(amqpAutoLink);
35
+ const { info } = response.data;
36
+
37
+ if (info && info.AMQPLIB_USER && info.AMQPLIB_PWD && info.AMQPLIB_IP && info.AMQPLIB_PORT) {
38
+ finalAmqpLink = `amqp://${info.AMQPLIB_USER}:${info.AMQPLIB_PWD}@${info.AMQPLIB_IP}:${info.AMQPLIB_PORT}`;
39
+ log.log(`✅ 自动获取连接信息成功: ${finalAmqpLink}`);
40
+ } else {
41
+ log.error('❌ 获取的连接信息不完整,使用默认连接地址');
42
+ }
43
+ } catch (error) {
44
+ log.error('❌ 获取连接信息失败:', error.message);
45
+ log.log('使用默认连接地址继续...');
46
+ }
47
+ }
48
+
27
49
  // 重连函数
28
50
  const reconnect = async () => {
29
51
  log.error('连接丢失,正在尝试重连...');
30
52
  await happy.sleep(2); // 重连前等待2秒
31
- connection = await amqp.connect(amqpLink, { heartbeat, timeout });
53
+ connection = await amqp.connect(finalAmqpLink, { heartbeat, timeout });
32
54
  channel = await connection.createChannel();
33
55
  await channel.assertQueue(channelName, { durable });
34
56
  await channel.prefetch(prefetch);
@@ -44,18 +66,29 @@ module.exports = {
44
66
  if (msg !== null) {
45
67
  try {
46
68
  const content = JSON.parse(msg.content.toString());
47
- log.log(`收到消息: ${JSON.stringify(content)}`);
69
+ log.log(`🪴 队列收到消息: ${JSON.stringify(content)}`);
48
70
  const startTime = Date.now();
49
- await callback(content); // 执行消息处理回调
50
- const endTime = Date.now() - startTime;
51
- log.log(`消息处理完成,延迟: ${delay}ms,总时间: ${endTime}ms`);
52
- setTimeout(() => channel.ack(msg), delay); // 延迟ack
71
+ // 执行消息处理回调
72
+ callback(content).then(() => {
73
+ const endTime = Date.now() - startTime;
74
+ log.log(`☘️ 消息处理完成,延迟: ${delay}ms,总时间: ${endTime}ms`);
75
+ setTimeout(() => channel.ack(msg), delay); // 延迟ack
76
+ }).catch(async (e) => {
77
+ // 拒绝消息并重新排队
78
+ log.log('‼️ 处理消息返回错误:', e);
79
+ await happy.sleep(5);
80
+ channel.reject(msg, true);
81
+ });
53
82
  } catch (e) {
54
- log.error('处理消息时出错:', e);
55
- channel.reject(msg, true); // 拒绝消息并重新排队
83
+ // 拒绝消息并重新排队
84
+ log.log('‼️ 处理消息时出错:', e.message);
85
+ await happy.sleep(5);
86
+ channel.reject(msg, true);
56
87
  }
57
88
  } else {
58
- log.error('收到无效消息:', msg);
89
+ // 直接消息此消息;
90
+ channel.ack(msg);
91
+ log.error('收到无效消息(自动消费):', msg);
59
92
  }
60
93
  }, { noAck: false });
61
94
  };
@@ -63,7 +96,7 @@ module.exports = {
63
96
  // 启动函数
64
97
  const start = async () => {
65
98
  try {
66
- connection = await amqp.connect(amqpLink, { heartbeat, timeout });
99
+ connection = await amqp.connect(finalAmqpLink, { heartbeat, timeout });
67
100
  channel = await connection.createChannel();
68
101
  await channel.assertQueue(channelName, { durable });
69
102
  await channel.prefetch(prefetch);
@@ -98,7 +131,6 @@ module.exports = {
98
131
  }
99
132
  }, autoReload);
100
133
  }
101
-
102
134
  finish(); // 执行初始化完成回调
103
135
  } catch (e) {
104
136
  log.error('初始化RabbitMQ连接时出错:', e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amqplib-init",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "消息队列初始化",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,7 +10,8 @@
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
12
  "amqplib": "^0.10.4",
13
- "chalk-style": "^1.0.0",
13
+ "axios": "^1.6.0",
14
+ "chalk-style": "^1.0.3",
14
15
  "happy-help": "^1.0.1",
15
16
  "js-base64": "^3.7.7",
16
17
  "shelljs": "^0.8.5"