amqplib-init 1.0.1 → 1.1.1

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 +37 -1
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -1,6 +1,11 @@
1
+ import url from "url";
1
2
  const amqp = require('amqplib');
2
3
  const shelljs = require('shelljs');
3
4
  const chalk = require('chalk-style');
5
+ import { Base64 } from 'js-base64';
6
+ import querystring from 'querystring';
7
+ import shell from 'shelljs';
8
+
4
9
  const sleep = (time) => {
5
10
  return new Promise(resolve => {
6
11
  setTimeout(_ => resolve(), time * 1000)
@@ -22,7 +27,8 @@ module.exports = {
22
27
  amqpLink = ``,
23
28
  heartbeat = 5,
24
29
  timeout = 120000,
25
- delay = 0
30
+ delay = 0,
31
+ init = () => {}
26
32
  } = option;
27
33
  const durable = true;
28
34
  chalk.log(`🚀 队列已执行...`);
@@ -45,6 +51,8 @@ module.exports = {
45
51
  chalk.log(`✳️->数据队列名称`, channelName);
46
52
  chalk.log(`✳️->是否持久化`, durable);
47
53
  const { queue } = await channel.assertQueue(channelName, { durable });
54
+ // 从这里绑定交换机;
55
+ await init && init({ channel, connect });
48
56
  // 一次性读取一条;
49
57
  await channel.prefetch(prefetch);
50
58
  chalk.log(`✳️->一次取多少条`, prefetch);
@@ -91,5 +99,33 @@ module.exports = {
91
99
  chalk.log(`✳️-队列已启动: finish~`);
92
100
  finish && finish();
93
101
  })
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);
94
130
  }
95
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amqplib-init",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "消息队列初始化",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,6 +11,7 @@
11
11
  "dependencies": {
12
12
  "amqplib": "^0.10.4",
13
13
  "chalk-style": "^1.0.0",
14
+ "js-base64": "^3.7.7",
14
15
  "shelljs": "^0.8.5"
15
16
  }
16
17
  }