amqplib-init 1.2.0 → 1.2.2
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/module/ConfigResolver.js
CHANGED
|
@@ -15,13 +15,17 @@ class ConfigResolver {
|
|
|
15
15
|
finish: () => {},
|
|
16
16
|
amqpLink: '',
|
|
17
17
|
amqpAutoLink: '',
|
|
18
|
-
heartbeat:
|
|
19
|
-
timeout:
|
|
18
|
+
heartbeat: 60, // 增加心跳间隔到60秒,避免长时间处理时连接断开
|
|
19
|
+
timeout: 300000, // 增加连接超时到5分钟(300秒)
|
|
20
20
|
delay: 0,
|
|
21
21
|
autoReload: 0,
|
|
22
22
|
queryHook: () => {},
|
|
23
23
|
initHook: () => {},
|
|
24
|
-
durable: true
|
|
24
|
+
durable: true,
|
|
25
|
+
// 新增配置项 - 支持15分钟长任务
|
|
26
|
+
messageTimeout: 900000, // 消息处理超时15分钟(900秒)
|
|
27
|
+
reconnectDelay: 5000, // 重连延迟5秒
|
|
28
|
+
maxReconnectAttempts: 10 // 最大重连次数
|
|
25
29
|
};
|
|
26
30
|
}
|
|
27
31
|
|
|
@@ -28,7 +28,7 @@ class ConnectionManager {
|
|
|
28
28
|
* @returns {Promise<Object>} 返回频道对象
|
|
29
29
|
*/
|
|
30
30
|
async initialize() {
|
|
31
|
-
const { channelName, durable, prefetch, heartbeat, timeout, initHook } = this.config;
|
|
31
|
+
const { channelName, durable, prefetch, heartbeat, timeout, initHook, reconnectDelay } = this.config;
|
|
32
32
|
|
|
33
33
|
try {
|
|
34
34
|
// 建立连接
|
|
@@ -74,8 +74,9 @@ class ConnectionManager {
|
|
|
74
74
|
// 清理旧的channel状态
|
|
75
75
|
await this._cleanupOldChannel();
|
|
76
76
|
|
|
77
|
-
//
|
|
78
|
-
|
|
77
|
+
// 等待重连间隔,使用配置的延迟时间
|
|
78
|
+
const delaySeconds = (this.config.reconnectDelay || 5000) / 1000;
|
|
79
|
+
await happy.sleep(delaySeconds);
|
|
79
80
|
|
|
80
81
|
// 重新初始化连接
|
|
81
82
|
await this.initialize();
|
|
@@ -86,10 +87,11 @@ class ConnectionManager {
|
|
|
86
87
|
} catch (error) {
|
|
87
88
|
log.error('重连失败:', error.message);
|
|
88
89
|
// 如果重连失败,等待一段时间后重试
|
|
90
|
+
const retryDelay = this.config.reconnectDelay || 5000;
|
|
89
91
|
setTimeout(() => {
|
|
90
92
|
this.reconnectInProgress = false;
|
|
91
93
|
this.reconnect();
|
|
92
|
-
},
|
|
94
|
+
}, retryDelay);
|
|
93
95
|
throw error;
|
|
94
96
|
} finally {
|
|
95
97
|
this.reconnectInProgress = false;
|
|
@@ -62,12 +62,35 @@ class MessageProcessor {
|
|
|
62
62
|
log.log(`🪴 队列收到消息: ${JSON.stringify(content)}`);
|
|
63
63
|
const startTime = Date.now();
|
|
64
64
|
|
|
65
|
+
// 🔥 兼容性检测:检查callback函数的参数数量
|
|
66
|
+
const callbackParams = callback.length;
|
|
67
|
+
let callbackPromise;
|
|
68
|
+
|
|
69
|
+
if (callbackParams >= 3) {
|
|
70
|
+
// 新版API:支持手动ACK (content, channel, msg)
|
|
71
|
+
log.log(`📋 使用新版手动ACK API (${callbackParams}个参数)`);
|
|
72
|
+
callbackPromise = callback(content, channel, msg);
|
|
73
|
+
} else {
|
|
74
|
+
// 旧版API:自动ACK (content)
|
|
75
|
+
log.log(`📋 使用旧版自动ACK API (${callbackParams}个参数)`);
|
|
76
|
+
callbackPromise = callback(content);
|
|
77
|
+
}
|
|
78
|
+
|
|
65
79
|
// 执行消息处理回调
|
|
66
|
-
|
|
67
|
-
.then(() => {
|
|
80
|
+
callbackPromise
|
|
81
|
+
.then((result) => {
|
|
68
82
|
const endTime = Date.now() - startTime;
|
|
69
83
|
log.log(`☘️ 消息处理完成,延迟: ${delay}ms,总时间: ${endTime}ms`);
|
|
70
|
-
|
|
84
|
+
|
|
85
|
+
// 🔥 检查返回值决定是否自动ACK
|
|
86
|
+
if (result === 'MANUAL_ACK') {
|
|
87
|
+
// 手动ACK模式:从处理集合中移除但不ACK,等待用户手动调用
|
|
88
|
+
this.processingMessages.delete(deliveryTag);
|
|
89
|
+
log.log(`📋 消息 ${deliveryTag} 进入手动ACK模式,等待手动确认`);
|
|
90
|
+
} else {
|
|
91
|
+
// 自动ACK模式:按原有逻辑处理
|
|
92
|
+
this._acknowledgeMessage(msg, channel, deliveryTag, delay);
|
|
93
|
+
}
|
|
71
94
|
})
|
|
72
95
|
.catch(async (e) => {
|
|
73
96
|
log.log('‼️ 处理消息返回错误:', e);
|