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