This commit is contained in:
Boki 2025-06-26 18:28:38 -04:00
parent c5f6b37022
commit d989c0c814
4 changed files with 2 additions and 167 deletions

View file

@ -1,85 +0,0 @@
# Awilix DI Container Migration Guide
This guide explains how to use the new Awilix dependency injection container in the data-ingestion service.
## Overview
The Awilix container provides proper dependency injection for decoupled libraries, allowing them to be reused in other projects without stock-bot specific dependencies.
## Current Implementation
The data-ingestion service now uses a hybrid approach:
1. Awilix container for ProxyManager and other decoupled services
2. Legacy service factory for backward compatibility
## Usage Example
```typescript
// Create Awilix container
const awilixConfig = {
redis: {
host: config.database.dragonfly.host,
port: config.database.dragonfly.port,
db: config.database.dragonfly.db,
},
mongodb: {
uri: config.database.mongodb.uri,
database: config.database.mongodb.database,
},
postgres: {
host: config.database.postgres.host,
port: config.database.postgres.port,
database: config.database.postgres.database,
user: config.database.postgres.user,
password: config.database.postgres.password,
},
proxy: {
cachePrefix: 'proxy:',
ttl: 3600,
},
};
const container = createServiceContainer(awilixConfig);
await initializeServices(container);
// Access services from container
const proxyManager = container.resolve('proxyManager');
const cache = container.resolve('cache');
```
## Handler Integration
Handlers receive services through the enhanced service container:
```typescript
// Create service adapter with proxy from Awilix
const serviceContainerWithProxy = createServiceAdapter(services);
Object.defineProperty(serviceContainerWithProxy, 'proxy', {
get: () => container.resolve('proxyManager'),
enumerable: true,
configurable: true
});
// Handlers can now access proxy service
class MyHandler extends BaseHandler {
async myOperation() {
const proxy = this.proxy.getRandomProxy();
// Use proxy...
}
}
```
## Benefits
1. **Decoupled Libraries**: Libraries no longer depend on @stock-bot/config
2. **Reusability**: Libraries can be used in other projects
3. **Testability**: Easy to mock dependencies for testing
4. **Type Safety**: Full TypeScript support with Awilix
## Next Steps
To fully migrate to Awilix:
1. Update HTTP library to accept dependencies via constructor
2. Update Queue library to accept Redis config via constructor
3. Create actual MongoDB, PostgreSQL, and QuestDB clients in the container
4. Remove legacy service factory once all services are migrated

View file

@ -1,72 +0,0 @@
module.exports = {
apps: [
{
name: 'stock-ingestion',
script: './data-ingestion/dist/index.js',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 2001
},
env_development: {
NODE_ENV: 'development',
PORT: 2001
}
},
{
name: 'stock-pipeline',
script: './data-pipeline/dist/index.js',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 2002
},
env_development: {
NODE_ENV: 'development',
PORT: 2002
}
},
{
name: 'stock-api',
script: './web-api/dist/index.js',
instances: 2,
autorestart: true,
watch: false,
max_memory_restart: '1G',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 2003
},
env_development: {
NODE_ENV: 'development',
PORT: 2003
}
}
],
deploy: {
production: {
user: 'deploy',
host: 'production-server',
ref: 'origin/master',
repo: 'git@github.com:username/stock-bot.git',
path: '/var/www/stock-bot',
'post-deploy': 'cd apps/stock && npm install && npm run build && pm2 reload ecosystem.config.js --env production'
},
staging: {
user: 'deploy',
host: 'staging-server',
ref: 'origin/develop',
repo: 'git@github.com:username/stock-bot.git',
path: '/var/www/stock-bot-staging',
'post-deploy': 'cd apps/stock && npm install && npm run build && pm2 reload ecosystem.config.js --env development'
}
}
};

View file

@ -49,19 +49,11 @@
"docker:build": "docker-compose build", "docker:build": "docker-compose build",
"docker:up": "docker-compose up", "docker:up": "docker-compose up",
"docker:down": "docker-compose down", "docker:down": "docker-compose down",
"pm2:start": "pm2 start ecosystem.config.js",
"pm2:stop": "pm2 stop all",
"pm2:restart": "pm2 restart all",
"pm2:logs": "pm2 logs",
"pm2:status": "pm2 status",
"db:migrate": "cd data-ingestion && bun run db:migrate", "db:migrate": "cd data-ingestion && bun run db:migrate",
"db:seed": "cd data-ingestion && bun run db:seed", "db:seed": "cd data-ingestion && bun run db:seed",
"health:check": "bun scripts/health-check.js", "health:check": "bun scripts/health-check.js"
"monitor": "bun run pm2:logs",
"status": "bun run pm2:status"
}, },
"devDependencies": { "devDependencies": {
"pm2": "^5.3.0",
"@types/node": "^20.11.0", "@types/node": "^20.11.0",
"typescript": "^5.3.3", "typescript": "^5.3.3",
"turbo": "^2.5.4" "turbo": "^2.5.4"

View file

@ -15,4 +15,4 @@
{ "path": "./web-api" }, { "path": "./web-api" },
{ "path": "./web-app" } { "path": "./web-app" }
] ]
} }