created a service container and moved all possible stuff to it to make light index files with reusable container

This commit is contained in:
Boki 2025-06-22 19:01:16 -04:00
parent 9a5e87ef4a
commit eeb5d1aca2
7 changed files with 766 additions and 633 deletions

View file

@ -7,10 +7,10 @@ import type { IServiceContainer } from '@stock-bot/handlers';
import { autoRegisterHandlers } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';
// Import handlers for bundling (ensures they're included in the build)
import './qm/qm.handler';
import './webshare/webshare.handler';
import './ceo/ceo.handler';
import './ib/ib.handler';
import './qm/qm.handler';
import './webshare/webshare.handler';
// Add more handler imports as needed
@ -46,18 +46,10 @@ export async function initializeAllHandlers(serviceContainer: IServiceContainer)
/**
* Manual fallback registration
*/
async function manualHandlerRegistration(serviceContainer: any): Promise<void> {
async function manualHandlerRegistration(_serviceContainer: any): Promise<void> {
logger.warn('Falling back to manual handler registration');
try {
// // Import and register handlers manually
// const { QMHandler } = await import('./qm/qm.handler');
// const qmHandler = new QMHandler(serviceContainer);
// qmHandler.register();
// const { WebShareHandler } = await import('./webshare/webshare.handler');
// const webShareHandler = new WebShareHandler(serviceContainer);
// webShareHandler.register();
logger.info('Manual handler registration complete');
} catch (error) {

View file

@ -1,256 +1,73 @@
/**
* Data Ingestion Service with Improved Dependency Injection
* This is the new version using type-safe services and constructor injection
* Data Ingestion Service
* Simplified entry point using ServiceApplication framework
*/
// Framework imports
import { initializeServiceConfig } from '@stock-bot/config';
import { Hono } from 'hono';
import { cors } from 'hono/cors';
// Library imports
import {
ServiceApplication,
createServiceContainerFromConfig,
initializeServices as initializeAwilixServices,
type ServiceContainer,
} from '@stock-bot/di';
import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { Shutdown } from '@stock-bot/shutdown';
import { handlerRegistry } from '@stock-bot/types';
import { getLogger } from '@stock-bot/logger';
// Local imports
import { initializeAllHandlers } from './handlers';
import { createRoutes } from './routes/create-routes';
// Initialize configuration
const config = initializeServiceConfig();
console.log('Data Service Configuration:', JSON.stringify(config, null, 2));
const serviceConfig = config.service;
if (config.log) {
setLoggerConfig({
logLevel: config.log.level,
logConsole: true,
logFile: false,
environment: config.environment,
hideObject: config.log.hideObject,
// Create service application
const app = new ServiceApplication(
config,
{
serviceName: 'data-ingestion',
enableHandlers: true,
enableScheduledJobs: true,
corsConfig: {
origin: '*',
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
allowHeaders: ['Content-Type', 'Authorization'],
credentials: false,
},
serviceMetadata: {
version: '1.0.0',
description: 'Market data ingestion from multiple providers',
endpoints: {
health: '/health',
handlers: '/api/handlers',
},
},
},
{
// Lifecycle hooks if needed
onStarted: (port) => {
const logger = getLogger('data-ingestion');
logger.info('Data ingestion service startup initiated with ServiceApplication framework');
},
}
);
// Container factory function
async function createContainer(config: any) {
const container = createServiceContainerFromConfig(config, {
enableQuestDB: false, // Data ingestion doesn't need QuestDB yet
enableMongoDB: true,
enablePostgres: true,
enableCache: true,
enableQueue: true,
enableBrowser: true, // Data ingestion needs browser for web scraping
enableProxy: true, // Data ingestion needs proxy for rate limiting
});
await initializeAwilixServices(container);
return container;
}
// Create logger AFTER config is set
const logger = getLogger('data-ingestion');
const PORT = serviceConfig.port;
let server: ReturnType<typeof Bun.serve> | null = null;
let container: ServiceContainer | null = null;
let app: Hono | null = null;
// Initialize shutdown manager
const shutdown = Shutdown.getInstance({ timeout: 15000 });
// Initialize services with new DI pattern
async function initializeServices() {
logger.info('Initializing data-ingestion service with improved DI...');
try {
// Create Awilix container directly from AppConfig
logger.debug('Creating Awilix DI container...');
container = createServiceContainerFromConfig(config, {
enableQuestDB: false, // Data ingestion doesn't need QuestDB yet
enableMongoDB: true,
enablePostgres: true,
enableCache: true,
enableQueue: true,
enableBrowser: true, // Data ingestion needs browser for web scraping
enableProxy: true, // Data ingestion needs proxy for rate limiting
});
await initializeAwilixServices(container);
logger.info('Awilix container created and initialized');
// Get the service container for handlers
const serviceContainer = container.resolve('serviceContainer');
// Create app with routes
app = new Hono();
// Add CORS middleware
app.use(
'*',
cors({
origin: '*',
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
allowHeaders: ['Content-Type', 'Authorization'],
credentials: false,
})
);
// Create and mount routes using the service container
const routes = createRoutes(serviceContainer);
app.route('/', routes);
// Initialize handlers with service container from Awilix
logger.debug('Initializing data handlers with Awilix DI pattern...');
// Auto-register all handlers with the service container from Awilix
await initializeAllHandlers(serviceContainer);
logger.info('Data handlers initialized with new DI pattern');
// Create scheduled jobs from registered handlers
logger.debug('Creating scheduled jobs from registered handlers...');
const allHandlers = handlerRegistry.getAllHandlersWithSchedule();
let totalScheduledJobs = 0;
for (const [handlerName, config] of allHandlers) {
if (config.scheduledJobs && config.scheduledJobs.length > 0) {
const queueManager = container.resolve('queueManager');
if(!queueManager) {
logger.error('Queue manager is not initialized, cannot create scheduled jobs');
continue;
}
const queue = queueManager.getQueue(handlerName);
for (const scheduledJob of config.scheduledJobs) {
// Include handler and operation info in job data
const jobData = {
handler: handlerName,
operation: scheduledJob.operation,
payload: scheduledJob.payload, // Don't default to {} - let it be undefined
};
// Build job options from scheduled job config
const jobOptions = {
priority: scheduledJob.priority,
delay: scheduledJob.delay,
repeat: {
immediately: scheduledJob.immediately,
},
};
await queue.addScheduledJob(
scheduledJob.operation,
jobData,
scheduledJob.cronPattern,
jobOptions
);
totalScheduledJobs++;
logger.debug('Scheduled job created', {
handler: handlerName,
operation: scheduledJob.operation,
cronPattern: scheduledJob.cronPattern,
immediately: scheduledJob.immediately,
priority: scheduledJob.priority,
});
}
}
}
logger.info('Scheduled jobs created', { totalJobs: totalScheduledJobs });
// Start queue workers
logger.debug('Starting queue workers...');
const queueManager = container.resolve('queueManager');
if (queueManager) {
queueManager.startAllWorkers();
logger.info('Queue workers started');
}
logger.info('All services initialized successfully');
} catch (error) {
console.error('DETAILED ERROR:', error);
logger.error('Failed to initialize services', {
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
details: JSON.stringify(error, null, 2),
});
throw error;
}
}
// Start server
async function startServer() {
await initializeServices();
if (!app) {
throw new Error('App not initialized');
}
server = Bun.serve({
port: PORT,
fetch: app.fetch,
development: config.environment === 'development',
});
logger.info(`Data-ingestion service started on port ${PORT}`);
}
// Register shutdown handlers with priorities
// Priority 1: Queue system (highest priority)
shutdown.onShutdownHigh(async () => {
logger.info('Shutting down queue system...');
try {
const queueManager = container?.resolve('queueManager');
if (queueManager) {
await queueManager.shutdown();
}
logger.info('Queue system shut down');
} catch (error) {
logger.error('Error shutting down queue system', { error });
}
}, 'Queue System');
// Priority 1: HTTP Server (high priority)
shutdown.onShutdownHigh(async () => {
if (server) {
logger.info('Stopping HTTP server...');
try {
server.stop();
logger.info('HTTP server stopped');
} catch (error) {
logger.error('Error stopping HTTP server', { error });
}
}
}, 'HTTP Server');
// Priority 2: Services and connections (medium priority)
shutdown.onShutdownMedium(async () => {
logger.info('Disposing services and connections...');
try {
if (container) {
// Disconnect database clients
const mongoClient = container.resolve('mongoClient');
if (mongoClient?.disconnect) {
await mongoClient.disconnect();
}
const postgresClient = container.resolve('postgresClient');
if (postgresClient?.disconnect) {
await postgresClient.disconnect();
}
const questdbClient = container.resolve('questdbClient');
if (questdbClient?.disconnect) {
await questdbClient.disconnect();
}
logger.info('All services disposed successfully');
}
} catch (error) {
logger.error('Error disposing services', { error });
}
}, 'Services');
// Priority 3: Logger shutdown (lowest priority - runs last)
shutdown.onShutdownLow(async () => {
try {
logger.info('Shutting down loggers...');
await shutdownLoggers();
// Don't log after shutdown
} catch {
// Silently ignore logger shutdown errors
}
}, 'Loggers');
// Start the service
startServer().catch(error => {
app.start(createContainer, createRoutes, initializeAllHandlers).catch(error => {
const logger = getLogger('data-ingestion');
logger.fatal('Failed to start data service', { error });
process.exit(1);
});
logger.info('Data service startup initiated with improved DI pattern');
});

View file

@ -1,248 +1,80 @@
/**
* Data Pipeline Service with Dependency Injection
* Uses Awilix container for managing database connections and services
* Data Pipeline Service
* Simplified entry point using ServiceApplication framework
*/
// Framework imports
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { initializeServiceConfig } from '@stock-bot/config';
// Library imports
import {
ServiceApplication,
createServiceContainerFromConfig,
initializeServices as initializeAwilixServices,
type ServiceContainer
} from '@stock-bot/di';
import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { Shutdown } from '@stock-bot/shutdown';
import { handlerRegistry } from '@stock-bot/types';
import { getLogger } from '@stock-bot/logger';
// Local imports
import { initializeAllHandlers } from './handlers';
import { createRoutes } from './routes/create-routes';
import { setupServiceContainer } from './container-setup';
import { initializeAllHandlers } from './handlers';
// Initialize configuration
const config = initializeServiceConfig();
console.log('Data Pipeline Service Configuration:', JSON.stringify(config, null, 2));
const serviceConfig = config.service;
if (config.log) {
setLoggerConfig({
logLevel: config.log.level,
logConsole: true,
logFile: false,
environment: config.environment,
hideObject: config.log.hideObject,
// Create service application
const app = new ServiceApplication(
config,
{
serviceName: 'data-pipeline',
enableHandlers: true,
enableScheduledJobs: true,
corsConfig: {
origin: '*',
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
allowHeaders: ['Content-Type', 'Authorization'],
credentials: false,
},
serviceMetadata: {
version: '1.0.0',
description: 'Data processing and transformation pipeline',
endpoints: {
health: '/health',
operations: '/api/operations',
},
},
},
{
// Custom lifecycle hooks
onContainerReady: (container) => {
// Setup service-specific configuration
const enhancedContainer = setupServiceContainer(config, container);
return enhancedContainer;
},
onStarted: (port) => {
const logger = getLogger('data-pipeline');
logger.info('Data pipeline service startup initiated with ServiceApplication framework');
},
}
);
// Container factory function
async function createContainer(config: any) {
const container = createServiceContainerFromConfig(config, {
enableQuestDB: config.database.questdb?.enabled || false,
// Data pipeline needs all databases
enableMongoDB: true,
enablePostgres: true,
enableCache: true,
enableQueue: true,
enableBrowser: false, // Data pipeline doesn't need browser
enableProxy: false, // Data pipeline doesn't need proxy
});
await initializeAwilixServices(container);
return container;
}
// Create logger AFTER config is set
const logger = getLogger('data-pipeline');
const PORT = serviceConfig.port;
let server: ReturnType<typeof Bun.serve> | null = null;
let container: ServiceContainer | null = null;
let app: Hono | null = null;
// Initialize shutdown manager
const shutdown = Shutdown.getInstance({ timeout: 15000 });
// Initialize services with DI pattern
async function initializeServices() {
logger.info('Initializing data pipeline service with DI...');
try {
// Create Awilix container directly from AppConfig
logger.debug('Creating Awilix DI container...');
container = createServiceContainerFromConfig(config, {
enableQuestDB: config.database.questdb?.enabled || false,
// Data pipeline needs all databases
enableMongoDB: true,
enablePostgres: true,
enableCache: true,
enableQueue: true,
enableBrowser: false, // Data pipeline doesn't need browser
enableProxy: false, // Data pipeline doesn't need proxy
});
await initializeAwilixServices(container);
logger.info('Awilix container created and initialized');
// Setup service-specific configuration
const serviceContainer = setupServiceContainer(config, container.resolve('serviceContainer'));
// Create app with routes
app = new Hono();
// Add CORS middleware
app.use(
'*',
cors({
origin: '*',
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
allowHeaders: ['Content-Type', 'Authorization'],
credentials: false,
})
);
// Create and mount routes using the service container
const routes = createRoutes(serviceContainer);
app.route('/', routes);
// Initialize handlers with service container
logger.debug('Initializing pipeline handlers with DI pattern...');
await initializeAllHandlers(serviceContainer);
logger.info('Pipeline handlers initialized with DI pattern');
// Create scheduled jobs from registered handlers
logger.debug('Creating scheduled jobs from registered handlers...');
const allHandlers = handlerRegistry.getAllHandlersWithSchedule();
let totalScheduledJobs = 0;
for (const [handlerName, config] of allHandlers) {
if (config.scheduledJobs && config.scheduledJobs.length > 0) {
const queueManager = container!.resolve('queueManager');
const queue = queueManager.getQueue(handlerName);
for (const scheduledJob of config.scheduledJobs) {
// Include handler and operation info in job data
const jobData = {
handler: handlerName,
operation: scheduledJob.operation,
payload: scheduledJob.payload,
};
// Build job options from scheduled job config
const jobOptions = {
priority: scheduledJob.priority,
delay: scheduledJob.delay,
repeat: {
immediately: scheduledJob.immediately,
},
};
await queue.addScheduledJob(
scheduledJob.operation,
jobData,
scheduledJob.cronPattern,
jobOptions
);
totalScheduledJobs++;
logger.debug('Scheduled job created', {
handler: handlerName,
operation: scheduledJob.operation,
cronPattern: scheduledJob.cronPattern,
immediately: scheduledJob.immediately,
priority: scheduledJob.priority,
});
}
}
}
logger.info('Scheduled jobs created', { totalJobs: totalScheduledJobs });
// Start queue workers
logger.debug('Starting queue workers...');
const queueManager = container.resolve('queueManager');
if (queueManager) {
queueManager.startAllWorkers();
logger.info('Queue workers started');
}
logger.info('All services initialized successfully');
} catch (error) {
console.error('DETAILED ERROR:', error);
logger.error('Failed to initialize services', {
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
details: JSON.stringify(error, null, 2)
});
throw error;
}
}
// Start server
async function startServer() {
await initializeServices();
if (!app) {
throw new Error('App not initialized');
}
server = Bun.serve({
port: PORT,
fetch: app.fetch,
development: config.environment === 'development',
});
logger.info(`Data pipeline service started on port ${PORT}`);
}
// Register shutdown handlers with priorities
// Priority 1: Queue system (highest priority)
shutdown.onShutdownHigh(async () => {
logger.info('Shutting down queue system...');
try {
const queueManager = container?.resolve('queueManager');
if (queueManager) {
await queueManager.shutdown();
}
logger.info('Queue system shut down');
} catch (error) {
logger.error('Error shutting down queue system', { error });
}
}, 'Queue System');
// Priority 1: HTTP Server (high priority)
shutdown.onShutdownHigh(async () => {
if (server) {
logger.info('Stopping HTTP server...');
try {
server.stop();
logger.info('HTTP server stopped');
} catch (error) {
logger.error('Error stopping HTTP server', { error });
}
}
}, 'HTTP Server');
// Priority 2: Services and connections (medium priority)
shutdown.onShutdownMedium(async () => {
logger.info('Disposing services and connections...');
try {
if (container) {
// Disconnect database clients
const mongoClient = container.resolve('mongoClient');
if (mongoClient?.disconnect) await mongoClient.disconnect();
const postgresClient = container.resolve('postgresClient');
if (postgresClient?.disconnect) await postgresClient.disconnect();
const questdbClient = container.resolve('questdbClient');
if (questdbClient?.disconnect) await questdbClient.disconnect();
logger.info('All services disposed successfully');
}
} catch (error) {
logger.error('Error disposing services', { error });
}
}, 'Services');
// Priority 3: Logger shutdown (lowest priority - runs last)
shutdown.onShutdownLow(async () => {
try {
logger.info('Shutting down loggers...');
await shutdownLoggers();
// Don't log after shutdown
} catch {
// Silently ignore logger shutdown errors
}
}, 'Loggers');
// Start the service
startServer().catch(error => {
app.start(createContainer, createRoutes, initializeAllHandlers).catch(error => {
const logger = getLogger('data-pipeline');
logger.fatal('Failed to start data pipeline service', { error });
process.exit(1);
});
logger.info('Data pipeline service startup initiated with DI pattern');
});

View file

@ -1,183 +1,78 @@
/**
* Stock Bot Web API with Dependency Injection
* REST API service using Awilix container for managing connections
* Stock Bot Web API
* Simplified entry point using ServiceApplication framework
*/
// Framework imports
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { initializeServiceConfig } from '@stock-bot/config';
// Library imports
import {
ServiceApplication,
createServiceContainerFromConfig,
initializeServices as initializeAwilixServices,
type ServiceContainer
} from '@stock-bot/di';
import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { Shutdown } from '@stock-bot/shutdown';
import { getLogger } from '@stock-bot/logger';
// Local imports
import { createRoutes } from './routes/create-routes';
import { setupServiceContainer } from './container-setup';
// Initialize configuration
const config = initializeServiceConfig();
console.log('Web API Service Configuration:', JSON.stringify(config, null, 2));
const serviceConfig = config.service;
if (config.log) {
setLoggerConfig({
logLevel: config.log.level,
logConsole: true,
logFile: false,
environment: config.environment,
hideObject: config.log.hideObject,
// Create service application
const app = new ServiceApplication(
config,
{
serviceName: 'web-api',
enableHandlers: false, // Web API doesn't use handlers
enableScheduledJobs: false, // Web API doesn't use scheduled jobs
corsConfig: {
origin: ['http://localhost:4200', 'http://localhost:3000', 'http://localhost:3002'],
allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization'],
credentials: true,
},
serviceMetadata: {
version: '1.0.0',
description: 'Stock Bot REST API',
endpoints: {
health: '/health',
exchanges: '/api/exchanges',
},
},
},
{
// Custom lifecycle hooks
onContainerReady: (container) => {
// Setup service-specific configuration
const enhancedContainer = setupServiceContainer(config, container);
return enhancedContainer;
},
onStarted: (port) => {
const logger = getLogger('web-api');
logger.info('Web API service startup initiated with ServiceApplication framework');
},
}
);
// Container factory function
async function createContainer(config: any) {
const container = createServiceContainerFromConfig(config, {
enableQuestDB: false, // Web API doesn't need QuestDB
enableMongoDB: true,
enablePostgres: true,
enableCache: true,
enableQueue: false, // Web API doesn't need queue processing
enableBrowser: false, // Web API doesn't need browser
enableProxy: false, // Web API doesn't need proxy
});
await initializeAwilixServices(container);
return container;
}
// Create logger AFTER config is set
const logger = getLogger('web-api');
const PORT = serviceConfig.port;
let server: ReturnType<typeof Bun.serve> | null = null;
let container: ServiceContainer | null = null;
let app: Hono | null = null;
// Initialize shutdown manager
const shutdown = Shutdown.getInstance({ timeout: 15000 });
// Initialize services with DI pattern
async function initializeServices() {
logger.info('Initializing web API service with DI...');
try {
// Create Awilix container directly from AppConfig
logger.debug('Creating Awilix DI container...');
container = createServiceContainerFromConfig(config, {
enableQuestDB: false, // Web API doesn't need QuestDB
enableMongoDB: true,
enablePostgres: true,
enableCache: true,
enableQueue: false, // Web API doesn't need queue processing
enableBrowser: false, // Web API doesn't need browser
enableProxy: false, // Web API doesn't need proxy
});
await initializeAwilixServices(container);
logger.info('Awilix container created and initialized');
// Setup service-specific configuration
const serviceContainer = setupServiceContainer(config, container.resolve('serviceContainer'));
// Create app with routes
app = new Hono();
// Add CORS middleware
app.use(
'*',
cors({
origin: ['http://localhost:4200', 'http://localhost:3000', 'http://localhost:3002'],
allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization'],
credentials: true,
})
);
// Basic API info endpoint
app.get('/', c => {
return c.json({
name: 'Stock Bot Web API',
version: '1.0.0',
status: 'running',
timestamp: new Date().toISOString(),
endpoints: {
health: '/health',
exchanges: '/api/exchanges',
},
});
});
// Create and mount routes using the service container
const routes = createRoutes(serviceContainer);
app.route('/', routes);
logger.info('All services initialized successfully');
} catch (error) {
console.error('DETAILED ERROR:', error);
logger.error('Failed to initialize services', {
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
details: JSON.stringify(error, null, 2)
});
throw error;
}
}
// Start server
async function startServer() {
await initializeServices();
if (!app) {
throw new Error('App not initialized');
}
server = Bun.serve({
port: PORT,
fetch: app.fetch,
development: config.environment === 'development',
});
logger.info(`Web API service started on port ${PORT}`);
}
// Register shutdown handlers with priorities
// Priority 1: HTTP Server (high priority)
shutdown.onShutdownHigh(async () => {
if (server) {
logger.info('Stopping HTTP server...');
try {
server.stop();
logger.info('HTTP server stopped');
} catch (error) {
logger.error('Error stopping HTTP server', { error });
}
}
}, 'HTTP Server');
// Priority 2: Services and connections (medium priority)
shutdown.onShutdownMedium(async () => {
logger.info('Disposing services and connections...');
try {
if (container) {
// Disconnect database clients
const mongoClient = container.resolve('mongoClient');
if (mongoClient?.disconnect) await mongoClient.disconnect();
const postgresClient = container.resolve('postgresClient');
if (postgresClient?.disconnect) await postgresClient.disconnect();
logger.info('All services disposed successfully');
}
} catch (error) {
logger.error('Error disposing services', { error });
}
}, 'Services');
// Priority 3: Logger shutdown (lowest priority - runs last)
shutdown.onShutdownLow(async () => {
try {
logger.info('Shutting down loggers...');
await shutdownLoggers();
// Don't log after shutdown
} catch {
// Silently ignore logger shutdown errors
}
}, 'Loggers');
// Start the service
startServer().catch(error => {
app.start(createContainer, createRoutes).catch(error => {
const logger = getLogger('web-api');
logger.fatal('Failed to start web API service', { error });
process.exit(1);
});
logger.info('Web API service startup initiated with DI pattern');
});