Skip to content

Commit

Permalink
fix: reconection
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos committed Mar 8, 2024
1 parent 57bc088 commit 296a521
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vtru-studio-websocket",
"version": "1.1.17",
"version": "1.1.18",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const logger = debug('controllers');
export const controllersStart = async () => {
const rabbitmqStatus = await getConnection();
if (!rabbitmqStatus.isConnected || !rabbitmqStatus.connection) {
logger('RabbitMQ connection failed, retrying in 10 seconds...');
console.log('RabbitMQ connection failed, retrying in 10 seconds...');
setTimeout(controllersStart, 10000);
return;
}

rabbitmqStatus.connection.on('close', () => {
logger('RabbitMQ connection closed, restarting in 10 seconds...');
console.log('RabbitMQ connection closed, restarting in 10 seconds...');
setTimeout(controllersStart, 10000);
});

Expand Down
10 changes: 10 additions & 0 deletions src/services/rabbitmq/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ export const getConnection = async () => {
try {
if (!status.connection) {
status.connection = await rabbitmq.connect(RABBITMQ_URL);
status.connection.on('close', () => {
status.isConnected = false;
status.connection = null;
console.error('RabbitMQ connection closed');
});
status.connection.on('error', (error) => {
status.isConnected = false;
status.connection = null;
console.error('Error occurred in RabbitMQ connection:', error);
});
status.isConnected = true;
}
} catch (err) {
Expand Down

0 comments on commit 296a521

Please sign in to comment.