fixing up db's and data-service

This commit is contained in:
Boki 2025-06-19 23:45:38 -04:00
parent aca98fdce4
commit 71f9b0a886
9 changed files with 211 additions and 19 deletions

View file

@ -8,8 +8,8 @@ import { cors } from 'hono/cors';
// Library imports
import { initializeServiceConfig } from '@stock-bot/config';
import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { createAndConnectMongoDBClient, MongoDBClient } from '@stock-bot/mongodb-client';
import { createAndConnectPostgreSQLClient, PostgreSQLClient } from '@stock-bot/postgres-client';
import { connectMongoDB } from '@stock-bot/mongodb-client';
import { connectPostgreSQL } from '@stock-bot/postgres-client';
import { QueueManager, handlerRegistry, type QueueManagerConfig } from '@stock-bot/queue';
import { Shutdown } from '@stock-bot/shutdown';
// Local imports
@ -48,8 +48,7 @@ app.use(
const logger = getLogger('data-service');
const PORT = serviceConfig.port;
let server: ReturnType<typeof Bun.serve> | null = null;
let postgresClient: PostgreSQLClient | null = null;
let mongoClient: MongoDBClient | null = null;
// Singleton clients are managed in libraries
let queueManager: QueueManager | null = null;
// Initialize shutdown manager
@ -65,10 +64,10 @@ async function initializeServices() {
logger.info('Initializing data service...');
try {
// Initialize MongoDB client
// Initialize MongoDB client singleton
logger.info('Connecting to MongoDB...');
const mongoConfig = databaseConfig.mongodb;
mongoClient = await createAndConnectMongoDBClient({
await connectMongoDB({
uri: mongoConfig.uri,
database: mongoConfig.database,
host: mongoConfig.host || 'localhost',
@ -81,10 +80,10 @@ async function initializeServices() {
});
logger.info('MongoDB connected');
// Initialize PostgreSQL client
// Initialize PostgreSQL client singleton
logger.info('Connecting to PostgreSQL...');
const pgConfig = databaseConfig.postgres;
postgresClient = await createAndConnectPostgreSQLClient({
await connectPostgreSQL({
host: pgConfig.host,
port: pgConfig.port,
database: pgConfig.database,
@ -130,6 +129,40 @@ async function initializeServices() {
initializeWebShareProvider();
logger.info('Data providers initialized');
// Create scheduled jobs from registered handlers
logger.info('Creating scheduled jobs from registered handlers...');
const { handlerRegistry } = await import('@stock-bot/queue');
const allHandlers = handlerRegistry.getAllHandlers();
let totalScheduledJobs = 0;
for (const [handlerName, config] of allHandlers) {
if (config.scheduledJobs && config.scheduledJobs.length > 0) {
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,
...(scheduledJob.payload || {}),
};
await queue.addScheduledJob(
scheduledJob.operation,
jobData,
scheduledJob.cronPattern
);
totalScheduledJobs++;
logger.info('Scheduled job created', {
handler: handlerName,
operation: scheduledJob.operation,
cronPattern: scheduledJob.cronPattern
});
}
}
}
logger.info('Scheduled jobs created', { totalJobs: totalScheduledJobs });
logger.info('All services initialized successfully');
} catch (error) {
logger.error('Failed to initialize services', { error });
@ -178,12 +211,11 @@ shutdown.onShutdown(async () => {
shutdown.onShutdown(async () => {
logger.info('Disconnecting from databases...');
try {
if (mongoClient) {
await mongoClient.disconnect();
}
if (postgresClient) {
await postgresClient.disconnect();
}
const { disconnectMongoDB } = await import('@stock-bot/mongodb-client');
const { disconnectPostgreSQL } = await import('@stock-bot/postgres-client');
await disconnectMongoDB();
await disconnectPostgreSQL();
logger.info('Database connections closed');
} catch (error) {
logger.error('Error closing database connections', { error });

View file

@ -321,7 +321,7 @@ async function searchQMSymbolsAPI(query: string): Promise<string[]> {
}
const symbols = await response.json();
const client = getMongoDBClient();
const mongoClient = getMongoDBClient();
const updatedSymbols = symbols.map((symbol: any) => {
return {
...symbol,
@ -329,7 +329,7 @@ async function searchQMSymbolsAPI(query: string): Promise<string[]> {
symbol: symbol.symbol.split(':')[0], // Extract symbol from "symbol:exchange"
};
});
await client.batchUpsert('qmSymbols', updatedSymbols, ['qmSearchCode']);
await mongoClient.batchUpsert('qmSymbols', updatedSymbols, ['qmSearchCode']);
const exchanges: Exchange[] = [];
for (const symbol of symbols) {
if (!exchanges.some(ex => ex.exchange === symbol.exchange)) {
@ -342,7 +342,7 @@ async function searchQMSymbolsAPI(query: string): Promise<string[]> {
});
}
}
await client.batchUpsert('qmExchanges', exchanges, ['exchange']);
await mongoClient.batchUpsert('qmExchanges', exchanges, ['exchange']);
session.successfulCalls++;
session.lastUsed = new Date();