refactor(router): split worker-manager god module into focused modules#513
refactor(router): split worker-manager god module into focused modules#513zbigniewsobiecki merged 2 commits intodevfrom
Conversation
|
🤔 Just a sec, looking into that router refactor now |
|
Thanks for taking a look! The refactor is complete and all tests are passing. Let me know if you have any questions or feedback. 👍 |
|
🔍 Reviewing code — Examining the PR changes for quality and correctness... |
nhopeatall
left a comment
There was a problem hiding this comment.
Summary
Clean, well-executed refactoring that splits a 418-line god module into three focused modules with clear responsibilities. The decomposition is logical, backward compatibility is preserved via re-exports, and test coverage is thorough (43 new tests). Approving.
Code Issues
Should Fix
- src/router/bullmq-workers.ts:25 —
parseRedisConnectionis a third copy of the same Redis URL parsing logic that already exists asparseRedisUrlin bothsrc/router/queue.ts:8andsrc/queue/client.ts:44(identical implementations). Consider extracting a shared utility or re-exporting from one of the existing locations. This could be a follow-up.
Notes
- The new
parseRedisConnectionincludespassword: parsed.password || undefined, which is actually a fix — the originalworker-manager.tsinline construction dropped the password field entirely. This means Redis URLs with authentication now work correctly for BullMQ workers. - Backward compatibility verified: the sole external consumer (
src/router/index.ts) importsgetActiveWorkerCount,getActiveWorkers,startWorkerProcessor,stopWorkerProcessor— all still exported fromworker-manager.ts. - CI is green (lint, typecheck, 2885 tests passing).
| /** | ||
| * Parse a Redis URL string into BullMQ ConnectionOptions. | ||
| */ | ||
| export function parseRedisConnection(redisUrl: string): ConnectionOptions { |
There was a problem hiding this comment.
This is the third copy of the same Redis URL → BullMQ ConnectionOptions parser. Identical implementations already exist at:
src/router/queue.ts:8(parseRedisUrl)src/queue/client.ts:44(parseRedisUrl)
Consider extracting a shared utility (e.g. src/router/redis-utils.ts or similar) and having all three call sites import from it. Fine as a follow-up.
There was a problem hiding this comment.
Addressed — extracted parseRedisUrl into a shared src/utils/redis.ts utility and updated all three call sites to import from it:
src/router/queue.ts— removed inlineparseRedisUrl, now imports fromutils/redis.tssrc/queue/client.ts— removed inlineparseRedisUrl, now imports fromutils/redis.tssrc/router/bullmq-workers.ts— removedparseRedisConnection, re-exportsparseRedisUrlfrom the shared utility for backward compatibility
All 2885 tests pass, typecheck and lint clean.
|
@aaight address code review concerns |
Request AddressedWhat was askedAddress code review concerns — specifically the duplicate Changes Made
Verification
|
…iplication Consolidates three identical Redis URL → BullMQ ConnectionOptions parsers (src/router/queue.ts, src/queue/client.ts, src/router/bullmq-workers.ts) into a single shared utility at src/utils/redis.ts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
🔍 Reviewing code — Examining the PR changes for quality and correctness... |
nhopeatall
left a comment
There was a problem hiding this comment.
LGTM — Clean decomposition of a god module into three focused modules with proper separation of concerns.
What's good:
- Module split aligns well with single-responsibility: container lifecycle (
container-manager.ts), BullMQ boilerplate (bullmq-workers.ts), and orchestration (worker-manager.ts) - Backward compatibility maintained —
worker-manager.tsre-exports the public API, sosrc/router/index.tsand all other consumers are unaffected parseRedisUrldeduplication eliminates 3 copies acrossqueue.ts,client.ts, andworker-manager.tsinto a singleutils/redis.ts- Bonus bug fix: the old
worker-manager.tsinline Redis connection (new URL(...).hostname/port) silently dropped the password field, whilequeue.tsandclient.tsboth used the fullparseRedisUrlwith password. Now all three consumers use the same function createQueueWorkerfactory cleanly eliminates the copy-pasted event handler boilerplate between the two queue workers- Test coverage is thorough — each module tested in isolation with appropriate mocking boundaries, and the orchestrator tests verify delegation to the sub-modules
No issues found.
Summary
Refactors the god module
src/router/worker-manager.ts(418 lines, 5 mixed responsibilities) into three focused modules:src/router/container-manager.ts— Docker container lifecycle:spawnWorker,killWorker,cleanupWorker,buildWorkerEnv,extractProjectIdFromJob,detachAll, plus theactiveWorkersMap andActiveWorkerinterfacesrc/router/bullmq-workers.ts— BullMQ consumer factory:createQueueWorkerde-duplicates the identical event handler boilerplate that was copy-pasted for both queues, plusparseRedisConnectionsrc/router/worker-manager.ts— Thin orchestrator (~90 lines): wires the two modules together, re-exports the public API for backward compatibilityKey duplication eliminated: The two near-identical BullMQ
Workersetups (lines 300–344 and 348–385) that shared the same capacity check, concurrency/lockDuration config, and completed/failed/error event handlers are now a singlecreateQueueWorker()factory call.Zero API changes:
getActiveWorkerCount,getActiveWorkers,startWorkerProcessor, andstopWorkerProcessorare still importable fromworker-manager.ts(via re-exports).Test plan
tests/unit/router/bullmq-workers.test.ts— 10 tests coveringparseRedisConnectionandcreateQueueWorkerfactory (event handler dispatch, Sentry capture, label usage)tests/unit/router/container-manager.test.ts— 21 tests coveringextractProjectIdFromJob,buildWorkerEnv,spawnWorker,killWorker,cleanupWorker,detachAlltests/unit/router/worker-manager.test.ts— 12 tests covering re-exports,startWorkerProcessor, andstopWorkerProcessororchestrationnpm run typecheck— zero errorsnpm run lint— zero errorsTrello card: https://trello.com/c/JeJc7qN6/96-find-top-candidate-for-refactoring-and-plan-clean-refactoring-of-it-look-for-god-classes-modules-functions-files-and-code-duplic
🤖 Generated with Claude Code