Skip to content

Conversation

@LuisSantosJS
Copy link
Contributor

@LuisSantosJS LuisSantosJS commented Aug 26, 2025

melhorias em reconexões no rabbitmqController

Summary by Sourcery

Enhance RabbitMQController reconnection robustness by centralizing resource cleanup and adding typed error handling

Enhancements:

  • Introduce a cleanup() method to gracefully close and nullify the AMQP channel and connection with warning on failure
  • Replace manual null assignments with cleanup() before scheduling reconnection
  • Add TypeScript type annotations to AMQP connect and channel callbacks and event handlers

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 26, 2025

Reviewer's Guide

This PR refactors the RabbitMQ controller’s reconnection flow by adding strong TypeScript annotations for AMQP callbacks, extracting manual teardown logic into a dedicated cleanup() method, and replacing direct resets with safe resource closure and error-handled cleanup before scheduling reconnects.

Sequence diagram for RabbitMQ reconnection and cleanup flow

sequenceDiagram
    participant RabbitmqController
    participant amqpConnection
    participant amqpChannel
    participant Logger

    RabbitmqController->>amqpConnection: connect()
    alt Connection error
        amqpConnection-->>RabbitmqController: error
        RabbitmqController->>Logger: log error
        RabbitmqController->>RabbitmqController: handleConnectionLoss()
        RabbitmqController->>RabbitmqController: cleanup()
        RabbitmqController->>RabbitmqController: scheduleReconnect()
    else Connection success
        amqpConnection->>amqpChannel: createChannel()
        alt Channel error
            amqpChannel-->>RabbitmqController: error
            RabbitmqController->>Logger: log error
            RabbitmqController->>RabbitmqController: handleConnectionLoss()
            RabbitmqController->>RabbitmqController: cleanup()
            RabbitmqController->>RabbitmqController: scheduleReconnect()
        end
    end
    Note over RabbitmqController: cleanup() safely closes channel and connection
    Note over RabbitmqController: Errors during cleanup are logged as warnings
Loading

Class diagram for updated RabbitmqController cleanup and reconnection logic

classDiagram
    class RabbitmqController {
        +amqpConnection: amqp.Connection | null
        +amqpChannel: amqp.Channel | null
        +connect()
        +handleConnectionLoss()
        +scheduleReconnect()
        +cleanup() Promise<void>
    }
    class Logger {
        +error()
        +warn()
    }
    RabbitmqController --> Logger
    RabbitmqController : uses amqp.Connection
    RabbitmqController : uses amqp.Channel
Loading

File-Level Changes

Change Details Files
Add TypeScript type annotations to AMQP callbacks
  • Annotate error callback parameters as Error
  • Specify connection type as amqp.Connection
  • Specify channel type as amqp.Channel
src/api/integrations/event/rabbitmq/rabbitmq.controller.ts
Extract and centralize teardown logic into cleanup()
  • Implement async cleanup() to close channel and connection
  • Handle exceptions during cleanup and log warnings
  • Nullify amqpChannel and amqpConnection inside cleanup
src/api/integrations/event/rabbitmq/rabbitmq.controller.ts
Integrate cleanup() into reconnection workflow
  • Replace direct null assignments with cleanup() call in handleConnectionLoss
  • Maintain existing scheduleReconnect() behavior after cleanup
src/api/integrations/event/rabbitmq/rabbitmq.controller.ts

Possibly linked issues

  • #0: PR improves RabbitMQ connection resilience and error handling by adding a cleanup method and using it during connection loss, directly addressing the issue's connection stability problems.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@DavidsonGomes DavidsonGomes changed the base branch from main to develop August 27, 2025 19:04
@DavidsonGomes DavidsonGomes merged commit 20352e7 into EvolutionAPI:develop Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants