Skip to content

Commit 20352e7

Browse files
Merge pull request #1878 from LuisSantosJS/reconnect-rabbitmq
Reconnect rabbitmq
2 parents 78f7618 + b325500 commit 20352e7

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/api/integrations/event/rabbitmq/rabbitmq.controller.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class RabbitmqController extends EventController implements EventControll
4545
heartbeat: 30, // Add heartbeat of 30 seconds
4646
};
4747

48-
amqp.connect(connectionOptions, (error, connection) => {
48+
amqp.connect(connectionOptions, (error: Error, connection: amqp.Connection) => {
4949
if (error) {
5050
this.logger.error({
5151
local: 'RabbitmqController.connect',
@@ -57,7 +57,7 @@ export class RabbitmqController extends EventController implements EventControll
5757
}
5858

5959
// Connection event handlers
60-
connection.on('error', (err) => {
60+
connection.on('error', (err: Error) => {
6161
this.logger.error({
6262
local: 'RabbitmqController.connectionError',
6363
message: 'RabbitMQ connection error',
@@ -71,7 +71,7 @@ export class RabbitmqController extends EventController implements EventControll
7171
this.handleConnectionLoss();
7272
});
7373

74-
connection.createChannel((channelError, channel) => {
74+
connection.createChannel((channelError: Error, channel: amqp.Channel) => {
7575
if (channelError) {
7676
this.logger.error({
7777
local: 'RabbitmqController.createChannel',
@@ -83,7 +83,7 @@ export class RabbitmqController extends EventController implements EventControll
8383
}
8484

8585
// Channel event handlers
86-
channel.on('error', (err) => {
86+
channel.on('error', (err: Error) => {
8787
this.logger.error({
8888
local: 'RabbitmqController.channelError',
8989
message: 'RabbitMQ channel error',
@@ -136,8 +136,7 @@ export class RabbitmqController extends EventController implements EventControll
136136
return; // Already attempting to reconnect
137137
}
138138

139-
this.amqpChannel = null;
140-
this.amqpConnection = null;
139+
this.cleanup();
141140
this.scheduleReconnect();
142141
}
143142

@@ -406,4 +405,25 @@ export class RabbitmqController extends EventController implements EventControll
406405
}
407406
}
408407
}
408+
409+
public async cleanup(): Promise<void> {
410+
try {
411+
if (this.amqpChannel) {
412+
await this.amqpChannel.close();
413+
this.amqpChannel = null;
414+
}
415+
if (this.amqpConnection) {
416+
await this.amqpConnection.close();
417+
this.amqpConnection = null;
418+
}
419+
} catch (error) {
420+
this.logger.warn({
421+
local: 'RabbitmqController.cleanup',
422+
message: 'Error during cleanup',
423+
error: error.message || error,
424+
});
425+
this.amqpChannel = null;
426+
this.amqpConnection = null;
427+
}
428+
}
409429
}

0 commit comments

Comments
 (0)