cosmo
cosmo copied to clipboard
fix(controlplane): introduced abstract classes to improve workers & queue
Improvements
- Eliminated Repetition and Boilerplate - Single Source of Truth Before: Each of the 7+ workers contained 30-40 lines of identical boilerplate code for queue setup, Redis connections, job options, and error handling After: All common functionality consolidated into BaseQueue<T> and BaseWorker<T> base classes Impact: Reduced codebase by ~200+ lines of duplicated code, with all configuration centralized in the base classes
- Significantly Less Error-Prone Consistent Behavior: All workers now have identical error handling, logging patterns, and job management Type Safety: Generic base classes (BaseQueue<T>, BaseWorker<T>) provide compile-time type checking Reduced Human Error: No more copy-paste mistakes when creating new workers
- Streamlined Future Worker Integration Minimal Implementation: New workers only need to extend base classes and implement the handler method Plug-and-Play: Consistent constructor patterns and method signatures across all workers Template Pattern: Clear structure guides developers on how to implement new workers correctly Example: Adding a new worker now requires ~15 lines instead of ~60 lines
- Enhanced Long-term Maintainability Centralized Updates: Changes to queue behavior, error handling, or job options only need modification in one place Easier Debugging: Consistent logging and error patterns across all workers Simplified Testing: Base class functionality can be tested once, reducing test complexity Code Readability: Worker classes now focus purely on business logic, making them easier to understand and review
Summary by CodeRabbit
-
Refactor
- Consolidated queue and worker behavior into shared base implementations for consistent job processing, logging, and lifecycle handling.
- Replaced legacy factory-style worker creation with a unified class-based creation flow.
-
New Features
- Added background job support for deleting organization audit logs.
- Added background job support for reactivating organizations.
-
Chores
- Cleaned up and updated imports and public API surface for consistency.
Checklist
- [ ] I have discussed my proposed changes in an issue and have received approval to proceed.
- [x] I have followed the coding standards of the project.
- [ ] Tests or benchmarks have been added or updated.
- [ ] Documentation has been updated on https://github.com/wundergraph/cosmo-docs.
- [x] I have read the Contributors Guide.
Walkthrough
Adds abstract BaseQueue and BaseWorker classes, replaces IQueue/IWorker interfaces, migrates many worker/queue modules to extend the base classes, removes factory functions, updates imports and worker instantiation in bootstrap and CLI scripts, and introduces DeleteOrganizationAuditLogs and ReactivateOrganization worker modules.
Changes
| Cohort / File(s) | Change Summary |
|---|---|
Core base classescontrolplane/src/core/workers/base/Queue.ts, controlplane/src/core/workers/base/Worker.ts, controlplane/src/core/workers/base/index.ts |
Added BaseQueue and BaseWorker abstractions and an index re-export to centralize BullMQ queue/worker creation, logging, and error handling. |
Worker modules migrated to base classescontrolplane/src/core/workers/AIGraphReadme.ts, .../CacheWarmer.ts, .../DeactivateOrganization.ts, .../DeleteOrganization.ts, .../DeleteUser.ts |
Converted existing queue/worker implementations to extend BaseQueue/BaseWorker; removed direct BullMQ instantiation and factory functions; changed handler visibility to protected; updated imports and constructor parameter types. |
New worker modulescontrolplane/src/core/workers/DeleteOrganizationAuditLogs.ts, controlplane/src/core/workers/ReactivateOrganization.ts |
Added queue and worker classes (with input interfaces) for deleting organization audit logs and reactivating organizations, implemented using the new base classes. |
Removed / replaced filescontrolplane/src/core/workers/Worker.ts, controlplane/src/core/workers/DeleteOrganizationAuditLogsWorker.ts |
Removed old custom interfaces and obsolete factory-style worker file; functionality migrated into base or new class-based modules. |
Build/server bootstrap changescontrolplane/src/core/build-server.ts |
Replaced factory function imports/usage with class-based worker imports and new ...(...).create() calls; removed MetricsPluginOptions import. |
Repository & route import updatescontrolplane/src/core/repositories/OrganizationRepository.ts, controlplane/src/core/repositories/UserRepository.ts, controlplane/src/core/routes.ts |
Updated import paths to new worker module filenames (removed "Worker" suffix); no logic changes. |
CLI scripts import updatescontrolplane/src/bin/deactivate-org.ts, controlplane/src/bin/delete-user.ts, controlplane/src/bin/reactivate-org.ts |
Updated imports to reference renamed worker modules; no other changes. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
Pre-merge checks and finishing touches
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title Check | ✅ Passed | The title succinctly captures the introduction of abstract classes for workers and queues in the controlplane module, clearly reflecting the primary change and its intent to improve code reuse and maintainability. It follows a conventional commit style with a defined scope, avoids vague terms, and remains concise and focused on the main refactor. |
✨ Finishing touches
- [ ] 📝 Generate docstrings
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between d080371b2bd6ba9ff5bf172705c98bf40c13235f and 26158378025ed24abd5083c84c764501ef475755.
📒 Files selected for processing (2)
controlplane/src/core/build-server.ts(3 hunks)controlplane/src/core/repositories/OrganizationRepository.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
controlplane/src/core/build-server.ts (6)
controlplane/src/core/workers/AIGraphReadme.ts (1)
AIGraphReadmeWorker(57-136)controlplane/src/core/workers/DeleteOrganizationAuditLogs.ts (2)
DeleteOrganizationAuditLogsQueue(14-33)DeleteOrganizationAuditLogsWorker(35-61)controlplane/src/core/workers/DeleteOrganization.ts (2)
DeleteOrganizationQueue(19-38)DeleteOrganizationWorker(40-109)controlplane/src/core/workers/DeactivateOrganization.ts (2)
DeactivateOrganizationQueue(18-38)DeactivateOrganizationWorker(40-85)controlplane/src/core/workers/ReactivateOrganization.ts (2)
ReactivateOrganizationQueue(16-35)ReactivateOrganizationWorker(37-74)controlplane/src/core/workers/DeleteUser.ts (2)
DeleteUserQueue(21-40)DeleteUserWorker(42-101)
🔇 Additional comments (5)
controlplane/src/core/repositories/OrganizationRepository.ts (1)
37-37: Approve import path changes
The refactored queue modules maintain the existingaddJob/removeJobAPI.controlplane/src/core/build-server.ts (4)
13-13: LGTM!The import updates correctly reflect the migration from factory-based workers to class-based workers extending
BaseWorkerandBaseQueue.Also applies to: 39-50
324-332: LGTM!The refactored worker instantiation correctly follows the new pattern: instantiate the worker class, then call
.create()to initialize the BullMQ worker. The conditional creation based onopenaiAPIKeyavailability is appropriate.
334-340: LGTM!The worker instantiation follows the consistent pattern and correctly passes all required dependencies.
343-352: LGTM!All worker instantiations consistently follow the refactored pattern and correctly pass dependencies. The order of creation ensures that queue dependencies exist before the workers that consume them.
Also applies to: 355-363, 366-372, 375-385
Comment @coderabbitai help to get the list of available commands and usage tips.
Hi @fahimfaisaal, thanks for the PR. That looks like a useful refactor. We're gonna take a look soon.
This PR was marked stale due to lack of activity. It will be closed in 14 days.
This PR was marked stale due to lack of activity. It will be closed in 14 days.
This PR was marked stale due to lack of activity. It will be closed in 14 days.
Hi @StarpTech, will this PR be reviewed?