amqplib-init 1.1.3 → 1.1.5
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 +20 -33
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
const amqp = require('amqplib');
|
|
2
2
|
const shelljs = require('shelljs');
|
|
3
3
|
const chalk = require('chalk-style');
|
|
4
|
-
const Base64 = require('js-base64').Base64;
|
|
5
|
-
const querystring = require('querystring');
|
|
6
|
-
const shell = require('shelljs');
|
|
7
|
-
const url = require('url');
|
|
8
4
|
|
|
9
5
|
const sleep = (time) => {
|
|
10
6
|
return new Promise(resolve => {
|
|
@@ -21,13 +17,15 @@ module.exports = {
|
|
|
21
17
|
const {
|
|
22
18
|
channelName = 'node-test-channel',
|
|
23
19
|
prefetch = 1,
|
|
24
|
-
callback = ()=> {},
|
|
25
20
|
pmId = 0,
|
|
21
|
+
callback = ()=> {},
|
|
26
22
|
finish = ()=> {},
|
|
27
23
|
amqpLink = ``,
|
|
28
24
|
heartbeat = 5,
|
|
29
25
|
timeout = 120000,
|
|
30
26
|
delay = 0,
|
|
27
|
+
autoReload = 0,
|
|
28
|
+
queryHook = () => {},
|
|
31
29
|
init = () => {}
|
|
32
30
|
} = option;
|
|
33
31
|
const durable = true;
|
|
@@ -97,35 +95,24 @@ module.exports = {
|
|
|
97
95
|
amqpInit(true);
|
|
98
96
|
});
|
|
99
97
|
chalk.log(`✳️-队列已启动: finish~`);
|
|
98
|
+
if (autoReload > 0) {
|
|
99
|
+
setInterval(async _=> {
|
|
100
|
+
try {
|
|
101
|
+
const result = await channel.checkQueue(channelName);
|
|
102
|
+
const messageCount = result.messageCount;
|
|
103
|
+
console.log(`MQ队列中有 ${messageCount} 条消息`);
|
|
104
|
+
const length = queryHook ? !!queryHook() : 0;
|
|
105
|
+
console.log(`REDIS队列中有 ${messageCount} 条消息`);
|
|
106
|
+
if (messageCount <= 0 && length === 0) {
|
|
107
|
+
console.log(`即将重启此服务~`);
|
|
108
|
+
shelljs.exec(`pm2 reload ${pmId}`);
|
|
109
|
+
}
|
|
110
|
+
} catch (e) {
|
|
111
|
+
console.log(`重启失败~`);
|
|
112
|
+
}
|
|
113
|
+
}, autoReload)
|
|
114
|
+
}
|
|
100
115
|
finish && finish();
|
|
101
116
|
})
|
|
102
|
-
},
|
|
103
|
-
/**
|
|
104
|
-
* 执行控制器方法
|
|
105
|
-
* @param api 控制器
|
|
106
|
-
* @param data 携带数据
|
|
107
|
-
* @param dir 执行目录
|
|
108
|
-
* @returns {Promise<unknown>}
|
|
109
|
-
*/
|
|
110
|
-
toAction(api = '', data= {}, dir = ''){
|
|
111
|
-
const query = querystring.stringify(data);
|
|
112
|
-
const base = Base64.encode(query);
|
|
113
|
-
return new Promise((resolve, reject) => {
|
|
114
|
-
const sql = `php ${dir}/think action ${api} -o ${base}`;
|
|
115
|
-
chalk.warning(`即将执行命令: ${sql}`);
|
|
116
|
-
shell.exec(sql).code !== 0 ? reject('执行失败') : resolve('执行成功');
|
|
117
|
-
})
|
|
118
|
-
},
|
|
119
|
-
/**
|
|
120
|
-
* 获取绝对路径
|
|
121
|
-
* @param path 相对路径
|
|
122
|
-
* @returns {*}
|
|
123
|
-
*/
|
|
124
|
-
getRoot(path = ''){
|
|
125
|
-
// 获取当前模块的 URL
|
|
126
|
-
const __filename = url.fileURLToPath(import.meta.url);
|
|
127
|
-
// 转换为目录路径
|
|
128
|
-
const __dirname = path.dirname(__filename);
|
|
129
|
-
return path.resolve(__dirname, path);
|
|
130
117
|
}
|
|
131
118
|
}
|