3.5 KiB
3.5 KiB
Data-Ingestion Service Refactor Summary
What Was Done
Successfully refactored the data-ingestion service to use the new connection pool pattern, completely removing dependencies on the singleton anti-pattern.
Key Changes
-
Service Container Setup
- Created
database-setup.tswith proper connection factory configuration - Configured appropriate pool sizes for data ingestion workloads
- Added optional dynamic pool sizing for production environments
- Created
-
Main Service Refactor (
index.ts)- Removed
connectMongoDB()andconnectPostgreSQL()singleton calls - Replaced with
setupServiceContainer()initialization - Updated shutdown handlers to dispose container properly
- Routes now have access to the service container
- Removed
-
Handler Updates
- All handlers now accept
ServiceContainerparameter - QM handler operations use container-based OperationContext
- IB, Proxy, and WebShare handlers updated to accept container
- Added proper resource disposal with
ctx.dispose()
- All handlers now accept
-
Route Refactoring
- Created
create-routes.tsfactory function - Routes can access container through Hono context
- Maintains backward compatibility for simple routes
- Created
-
Migration Helper
- Created temporary migration helper for legacy operations
- Provides
getMongoDBClient()for IB operations still being migrated - Includes cleanup in shutdown sequence
Configuration Changes
- Added
@stock-bot/connection-factorydependency - Updated
tsconfig.jsonwith proper references - Pool sizes optimized for data ingestion:
- MongoDB: 50 connections (batch imports)
- PostgreSQL: 30 connections
- Cache: 20 connections
Benefits Achieved
- No More Global State: Each service manages its own connections
- Better Resource Management: Proper cleanup on shutdown
- Scalability: Dynamic pool sizing for production workloads
- Monitoring: Pool metrics available for observability
- Testing: Easier to test with mock containers
- Gradual Migration: Legacy operations still work during transition
Next Steps
- Complete Operation Migration: Update IB operations to use container
- Remove Migration Helper: Once all operations are migrated
- Add Monitoring: Set up dashboards for pool metrics
- Performance Testing: Validate pool sizes under load
- Replicate Pattern: Apply same refactor to other services
Example Usage
// Handler with container
export function initializeHandler(container: ServiceContainer) {
const config = {
operations: {
'my-operation': createJobHandler(async (payload) => {
// Operation uses container
const ctx = OperationContext.create('handler', 'operation', { container });
try {
// Use databases through context
await ctx.mongodb.insertOne(data);
await ctx.postgres.query('...');
return { success: true };
} finally {
await ctx.dispose(); // Clean up resources
}
})
}
};
}
Migration Checklist
- Remove singleton imports from index.ts
- Create service container setup
- Update all handlers to accept container
- Create route factory with container access
- Add migration helper for legacy code
- Update shutdown handlers
- Test build successfully
- Migrate remaining operations
- Remove migration helper
- Deploy and monitor