This commit is contained in:
Luis Santos 2025-08-27 16:04:52 -03:00 committed by GitHub
commit c41e8226cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -45,7 +45,7 @@ export class RabbitmqController extends EventController implements EventControll
heartbeat: 30, // Add heartbeat of 30 seconds heartbeat: 30, // Add heartbeat of 30 seconds
}; };
amqp.connect(connectionOptions, (error, connection) => { amqp.connect(connectionOptions, (error: Error, connection: amqp.Connection) => {
if (error) { if (error) {
this.logger.error({ this.logger.error({
local: 'RabbitmqController.connect', local: 'RabbitmqController.connect',
@ -57,7 +57,7 @@ export class RabbitmqController extends EventController implements EventControll
} }
// Connection event handlers // Connection event handlers
connection.on('error', (err) => { connection.on('error', (err: Error) => {
this.logger.error({ this.logger.error({
local: 'RabbitmqController.connectionError', local: 'RabbitmqController.connectionError',
message: 'RabbitMQ connection error', message: 'RabbitMQ connection error',
@ -71,7 +71,7 @@ export class RabbitmqController extends EventController implements EventControll
this.handleConnectionLoss(); this.handleConnectionLoss();
}); });
connection.createChannel((channelError, channel) => { connection.createChannel((channelError: Error, channel: amqp.Channel) => {
if (channelError) { if (channelError) {
this.logger.error({ this.logger.error({
local: 'RabbitmqController.createChannel', local: 'RabbitmqController.createChannel',
@ -83,7 +83,7 @@ export class RabbitmqController extends EventController implements EventControll
} }
// Channel event handlers // Channel event handlers
channel.on('error', (err) => { channel.on('error', (err: Error) => {
this.logger.error({ this.logger.error({
local: 'RabbitmqController.channelError', local: 'RabbitmqController.channelError',
message: 'RabbitMQ channel error', message: 'RabbitMQ channel error',
@ -136,8 +136,7 @@ export class RabbitmqController extends EventController implements EventControll
return; // Already attempting to reconnect return; // Already attempting to reconnect
} }
this.amqpChannel = null; this.cleanup();
this.amqpConnection = null;
this.scheduleReconnect(); this.scheduleReconnect();
} }
@ -406,4 +405,25 @@ export class RabbitmqController extends EventController implements EventControll
} }
} }
} }
public async cleanup(): Promise<void> {
try {
if (this.amqpChannel) {
await this.amqpChannel.close();
this.amqpChannel = null;
}
if (this.amqpConnection) {
await this.amqpConnection.close();
this.amqpConnection = null;
}
} catch (error) {
this.logger.warn({
local: 'RabbitmqController.cleanup',
message: 'Error during cleanup',
error: error.message || error,
});
this.amqpChannel = null;
this.amqpConnection = null;
}
}
} }