Skip to content

fix(finance): retry RabbitMQ connect at startup and self-heal on drop - #167

Merged
ilramdhan merged 1 commit into
mutugading:mainfrom
ilramdhan:be/poy-rm-v2-chain-202607
Aug 13, 2026
Merged

fix(finance): retry RabbitMQ connect at startup and self-heal on drop#167
ilramdhan merged 1 commit into
mutugading:mainfrom
ilramdhan:be/poy-rm-v2-chain-202607

Conversation

@ilramdhan

Copy link
Copy Markdown
Member

Description

Fixes an issue where setupRabbitMQ() dialed RabbitMQ only once at startup with no retry logic. During rolling restarts or startup races with RabbitMQ's readiness, pods permanently lost connection, causing all dependent RPCs (such as RM cost recalculation, cost sheet export, and Oracle sync triggers) to fail continuously with "message queue unavailable: RabbitMQ not connected".

Connection initialization now retries on startup via NewConnectionWithRetry and self-heals after connection drops through a mutex-guarded Supervise loop that swaps in a fresh connection and reruns registered OnReconnect callbacks.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that changes existing API)
  • ♻️ Refactor (code change without new feature or bug fix)
  • 📚 Documentation update
  • 🧪 Test update
  • 🔧 Chore (dependencies, config, etc.)

Service(s) Affected

  • Finance Service
  • IAM Service
  • Shared Proto (gen/)
  • Root/Common

Changes Made

  • Replaced single-dial RabbitMQ initialization in setupRabbitMQ() with NewConnectionWithRetry for startup resilience.
  • Added a mutex-guarded Supervise loop to automatically detect connection drops, establish a fresh connection, and re-trigger OnReconnect callbacks.
  • Retained the "RabbitMQ not connected" error substring in ErrPublisherUnavailable to maintain backward compatibility with existing tests and error handling.

Related Issues

Fixes #
Related to #

API Changes (if applicable)

Proto Changes

N/A

Breaking Changes

None. Error substrings remain unchanged to preserve existing error-handling behavior.

Testing Performed

Unit Tests

  • New unit tests added
  • Existing unit tests pass
  • Coverage maintained/improved

Integration Tests

  • New integration tests added
  • Existing integration tests pass

Manual Testing

# Verify finance service tests pass
go test -v ./...

Lint & Build

  • golangci-lint run ./... passes
  • go build ./... succeeds
  • go test -race ./... passes

Database (if applicable)

  • Migration added
  • Migration tested (up and down)
  • No breaking schema changes (or documented)

Documentation

  • README.md updated (if needed)
  • RULES.md updated (if needed)
  • Proto comments updated
  • OpenAPI regenerated

Rollback Plan

Revert this PR if unexpected reconnect behavior occurs. The system will fall back to single-attempt dialing at startup.

Screenshots/Logs (if applicable)

N/A


Pre-merge Checklist

  • I have read and followed RULES.md
  • I have read and followed CONTRIBUTING.md
  • Clean Architecture principles followed
  • All errors are properly handled
  • Context is passed appropriately
  • Structured logging is used
  • No hardcoded secrets
  • PR description is complete and clear
  • CI checks are passing

Reviewer Notes

None

setupRabbitMQ() previously dialed RabbitMQ exactly once at startup with
no retry, so any pod that raced RabbitMQ's readiness during a rolling
restart permanently lost its publisher and every dependent RPC (RM cost
recalc, cost sheet export, Oracle sync trigger) failed forever with
"message queue unavailable: RabbitMQ not connected" until the pod was
manually restarted.

Connection now retries on startup (NewConnectionWithRetry) and
self-heals via a mutex-guarded Supervise loop that swaps in a fresh
connection and reruns registered OnReconnect callbacks after a drop.
ErrPublisherUnavailable keeps the "RabbitMQ not connected" substring so
existing error-based tests/checks still match.
@ilramdhan ilramdhan added this to the Costing Release Milestone milestone Aug 13, 2026
@ilramdhan ilramdhan self-assigned this Aug 13, 2026
Copilot AI lite review requested due to automatic review settings August 13, 2026 08:17
@ilramdhan ilramdhan added bug Something isn't working enhancement New feature or request fix feat labels Aug 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@ilramdhan
ilramdhan merged commit 5c7a61c into mutugading:main Aug 13, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request feat fix

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants