-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (166 loc) · 5.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
*
*/
require('dotenv').config();
const amqp = require('amqplib/callback_api');
const queue = 'tasks';
const RETRY_INTERVAL = 2000;
const SEND_INTERVAL = 500;
const EXIT_TIMEOUT = 5 * 60 * 1000;
const CONNECTION_OPTS = {
protocol: 'amqps',
hostname: process.env.RABBIT_HOSTNAME,
port: parseInt(process.env.RABBIT_PORT),
username: process.env.RABBIT_USERNAME,
password: process.env.RABBIT_PASSWORD,
locale: 'en_US',
heartbeat: 60,
vhost: '/',
};
let state = {
connection: null,
channel: {
listener: null,
sender: null,
}
};
let interval = null;
function initRabbitMq() {
amqp.connect(CONNECTION_OPTS, (err, conn) => {
if (err) {
console.log('RabbitMQ is down. Will retry after 2 seconds.', err);
cleanUpAndRetry(state);
return;
}
state.connection = conn;
// Listener
if (!state.channel.listener) {
state.connection.createChannel((err, ch2) => {
if (err) {
console.log('Error in creating listener channel. Will retry after 2 seconds.');
cleanUpAndRetry(state);
return;
}
state.channel.listener = ch2;
state.channel.listener.assertQueue(queue);
state.channel.listener.on('error', (err) => {
console.log(err);
cleanUpAndRetry(state);
});
state.channel.listener.recover((err, ok) => {
if (err) {
console.log('Error recovering listener channel.', err);
return;
}
console.log('All ok with recover.', ok);
});
state.channel.listener.consume(queue, (msg) => {
if (msg != null) {
let dt = new Date();
console.log('[' + dt.toISOString() + '] Received msg: ' + msg.content.toString());
ch2.ack(msg);
} else {
state.channel.listener = null;
console.log('Consumer cancelled.');
}
});
});
}
// Sender
if (!state.channel.sender) {
state.connection.createChannel((err, ch1) => {
if (err) {
console.log('Error in creating sender channel. Will retry after 2 seconds.');
cleanUpAndRetry(state);
return;
}
state.channel.sender = ch1;
state.channel.sender.assertQueue(queue);
state.channel.sender.on('error', (err) => {
console.log('Error in sending channel.', err);
cleanUpAndRetry(state);
});
interval = setInterval(() => {
if (!!state.channel.sender) {
let dt = new Date();
try {
state.channel.sender.sendToQueue(queue, Buffer.from('Did something at [' + dt.toISOString() + '].'));
} catch (err1) {
console.log('Error in sendToQueue.', err1);
console.log('Clearing sender interval.');
clearInterval(interval);
cleanUpAndRetry(state);
}
} else {
console.log("Can't send to queue since sender is null.");
}
}, SEND_INTERVAL);
});
}
state.connection.on('error', (err) => {
console.log('Connection error.', err);
cleanUpAndRetry(state);
});
/*
setTimeout(() => {
conn.close();
process.exit(0);
}, EXIT_TIMEOUT);
*/
});
}
initRabbitMq();
function cleanUpAndRetry(state) {
console.log('Executing clean up and retry...');
closeSender(state);
closeListener(state);
closeConnection(state);
setTimeout(() => {
initRabbitMq();
}, RETRY_INTERVAL);
}
function closeSender(state) {
console.log('Closing sender channel...');
if (!!state.channel.sender) {
try {
state.channel.sender.close();
console.log('Sender channel closed.');
} catch (errSender) {
console.log('Error while closing sender channel.', errSender);
}
state.channel.sender = null;
} else {
console.log('Sender channel not found.');
}
}
function closeListener(state) {
console.log('Closing listener channel...');
if (!!state.channel.listener) {
try {
state.channel.listener.close();
console.log('Listener channel closed.');
} catch (errListener) {
console.log('Error in closing listener channel.', errListener);
}
state.channel.listener = null;
} else {
console.log('Listener channel not found.');
}
}
function closeConnection(state) {
console.log('Closing connection...');
if (!!state.connection) {
try {
state.connection.close();
console.log('Connection closed.');
} catch (errConn) {
console.log('Error in closing connection.', errConn);
}
state.connection = null;
} else {
console.log('Connection not found.');
}
}