Skip to content

fix: remove legacy RebalancingService and migrate WebSocket broadcast to AutoRebalancerService#23

Merged
Uchechukwu-Ekezie merged 32 commits into
grantFoxin:mainfrom
Jaydbrown:fix/remove-legacy-rebalancing-service
Jun 24, 2026
Merged

fix: remove legacy RebalancingService and migrate WebSocket broadcast to AutoRebalancerService#23
Uchechukwu-Ekezie merged 32 commits into
grantFoxin:mainfrom
Jaydbrown:fix/remove-legacy-rebalancing-service

Conversation

@Jaydbrown

@Jaydbrown Jaydbrown commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Closes #5

Problem

RebalancingService (monitoring/rebalancer.ts) was left instantiated and started in index.ts even after it was superseded by AutoRebalancerService. Two problems remained:

  1. Dead code at startupnew RebalancingService(wss) + rebalancingService.start() ran on every server boot, did nothing (the cron had already been removed), and added noise.
  2. Lost WebSocket broadcastnotifyClients() and broadcastToAllClients() lived only inside RebalancingService and were never called, silently dropping real-time portfolio events.

Changes

backend/src/index.ts — 1 line removed, 9 lines removed, 2 added

-import { RebalancingService } from './monitoring/rebalancer.js'

-// Start existing rebalancing service (now queue-backed, no cron)
-try {
-    const rebalancingService = new RebalancingService(wss)
-    rebalancingService.start()
-    console.log('[REBALANCING-SERVICE] Monitoring service started (queue-backed)')
-} catch (error) {
-    console.error('Failed to start rebalancing service:', error)
-}
+// Wire wss into autoRebalancer so it can push real-time portfolio events to clients
+autoRebalancer.setWss(wss)

backend/src/services/autoRebalancer.ts — 54 lines added

  • Added import { WebSocketServer } from 'ws'
  • Added private wss: WebSocketServer | null = null field
  • Added setWss(wss) — called from index.ts after the WebSocket server is created
  • Added hasWss() — useful for health checks and tests
  • Migrated notifyClients(portfolioId, event, data) from RebalancingService
  • Migrated broadcastToAllClients(event, data) from RebalancingService

backend/src/monitoring/rebalancer.ts — deleted

All live functionality has been consolidated into AutoRebalancerService.

Why setWss() instead of constructor injection

autoRebalancer is instantiated at line 86 of index.ts but wss = new WebSocketServer(...) is created at line 214. setWss() avoids reordering the startup sequence and keeps the change minimal.

Failure scenarios resolved

Scenario Before After
Double rebalance (cron + queue) Possible if cron accidentally restored Impossible — one scheduler
Duplicate recordRebalanceEvent() writes Possible Impossible — one code path
WebSocket broadcast silently dropped Always Fixed — notifyClients() now reachable
Dead startup try/catch Present Removed

Jaydbrown added 30 commits June 20, 2026 18:17
@Uchechukwu-Ekezie Uchechukwu-Ekezie merged commit 62ecf86 into grantFoxin:main Jun 24, 2026
3 checks passed
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.

bug: two portfolio-monitoring systems run simultaneously — race conditions and potential double-rebalancing

2 participants