improve rabbit controller

This commit is contained in:
luissantosjs 2025-08-26 19:15:59 +01:00
parent 4681576cfc
commit b325500310

View File

@ -45,7 +45,7 @@ export class RabbitmqController extends EventController implements EventControll
heartbeat: 30, // Add heartbeat of 30 seconds
};
amqp.connect(connectionOptions, (error, connection) => {
amqp.connect(connectionOptions, (error: Error, connection: amqp.Connection) => {
if (error) {
this.logger.error({
local: 'RabbitmqController.connect',
@ -57,7 +57,7 @@ export class RabbitmqController extends EventController implements EventControll
}
// Connection event handlers
connection.on('error', (err) => {
connection.on('error', (err: Error) => {
this.logger.error({
local: 'RabbitmqController.connectionError',
message: 'RabbitMQ connection error',
@ -71,7 +71,7 @@ export class RabbitmqController extends EventController implements EventControll
this.handleConnectionLoss();
});
connection.createChannel((channelError, channel) => {
connection.createChannel((channelError: Error, channel: amqp.Channel) => {
if (channelError) {
this.logger.error({
local: 'RabbitmqController.createChannel',
@ -83,7 +83,7 @@ export class RabbitmqController extends EventController implements EventControll
}
// Channel event handlers
channel.on('error', (err) => {
channel.on('error', (err: Error) => {
this.logger.error({
local: 'RabbitmqController.channelError',
message: 'RabbitMQ channel error',
@ -136,8 +136,7 @@ export class RabbitmqController extends EventController implements EventControll
return; // Already attempting to reconnect
}
this.amqpChannel = null;
this.amqpConnection = null;
this.cleanup();
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;
}
}
}