libs ready i think
This commit is contained in:
parent
1b34da9a69
commit
9673ae70ef
9 changed files with 242 additions and 129 deletions
|
|
@ -71,7 +71,9 @@ export class ConnectionFactory implements IConnectionFactory {
|
|||
}
|
||||
},
|
||||
dispose: async () => {
|
||||
await client.disconnect();
|
||||
if (client && typeof client.disconnect === 'function') {
|
||||
await client.disconnect();
|
||||
}
|
||||
this.pools.delete(key);
|
||||
},
|
||||
};
|
||||
|
|
@ -116,7 +118,9 @@ export class ConnectionFactory implements IConnectionFactory {
|
|||
metrics: client.getPoolMetrics(),
|
||||
health: async () => client.connected,
|
||||
dispose: async () => {
|
||||
await client.disconnect();
|
||||
if (client && typeof client.disconnect === 'function') {
|
||||
await client.disconnect();
|
||||
}
|
||||
this.pools.delete(key);
|
||||
},
|
||||
};
|
||||
|
|
@ -129,7 +133,7 @@ export class ConnectionFactory implements IConnectionFactory {
|
|||
}
|
||||
}
|
||||
|
||||
createCache(poolConfig: CachePoolConfig): ConnectionPool<any> {
|
||||
async createCache(poolConfig: CachePoolConfig): Promise<ConnectionPool<any>> {
|
||||
const key = `cache:${poolConfig.name}`;
|
||||
|
||||
if (this.pools.has(key)) {
|
||||
|
|
@ -142,16 +146,51 @@ export class ConnectionFactory implements IConnectionFactory {
|
|||
});
|
||||
|
||||
try {
|
||||
// TODO: Implement cache creation with dynamic import
|
||||
throw new Error('Cache creation temporarily disabled');
|
||||
const { createCache } = await import('@stock-bot/cache');
|
||||
|
||||
const client = createCache({
|
||||
redisConfig: poolConfig.config as any,
|
||||
keyPrefix: 'app:',
|
||||
ttl: 3600,
|
||||
enableMetrics: true,
|
||||
});
|
||||
|
||||
await client.waitForReady(10000);
|
||||
|
||||
const pool: ConnectionPool<any> = {
|
||||
name: poolConfig.name,
|
||||
client,
|
||||
metrics: {
|
||||
created: new Date(),
|
||||
totalConnections: 1,
|
||||
activeConnections: 1,
|
||||
idleConnections: 0,
|
||||
waitingRequests: 0,
|
||||
errors: 0,
|
||||
},
|
||||
health: async () => {
|
||||
try {
|
||||
await client.waitForReady(1000);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
dispose: async () => {
|
||||
// Cache provider manages its own connections
|
||||
this.pools.delete(key);
|
||||
},
|
||||
};
|
||||
|
||||
this.pools.set(key, pool);
|
||||
return pool;
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to create cache pool', { name: poolConfig.name, error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
createQueue(poolConfig: QueuePoolConfig): ConnectionPool<any> {
|
||||
async createQueue(poolConfig: QueuePoolConfig): Promise<ConnectionPool<any>> {
|
||||
const key = `queue:${poolConfig.name}`;
|
||||
|
||||
if (this.pools.has(key)) {
|
||||
|
|
@ -164,9 +203,46 @@ export class ConnectionFactory implements IConnectionFactory {
|
|||
});
|
||||
|
||||
try {
|
||||
// TODO: Implement queue creation with dynamic import
|
||||
throw new Error('Queue creation temporarily disabled');
|
||||
const { QueueManager } = await import('@stock-bot/queue');
|
||||
|
||||
const manager = QueueManager.initialize({
|
||||
redis: poolConfig.config as any,
|
||||
defaultQueueOptions: {
|
||||
defaultJobOptions: {
|
||||
removeOnComplete: 100,
|
||||
removeOnFail: 50,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const pool: ConnectionPool<any> = {
|
||||
name: poolConfig.name,
|
||||
client: manager,
|
||||
metrics: {
|
||||
created: new Date(),
|
||||
totalConnections: 1,
|
||||
activeConnections: 1,
|
||||
idleConnections: 0,
|
||||
waitingRequests: 0,
|
||||
errors: 0,
|
||||
},
|
||||
health: async () => {
|
||||
try {
|
||||
return true; // QueueManager doesn't have isHealthy method yet
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
dispose: async () => {
|
||||
if (manager && typeof manager.shutdown === 'function') {
|
||||
await manager.shutdown();
|
||||
}
|
||||
this.pools.delete(key);
|
||||
},
|
||||
};
|
||||
|
||||
this.pools.set(key, pool);
|
||||
return pool;
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to create queue manager', { name: poolConfig.name, error });
|
||||
throw error;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,5 @@
|
|||
/**
|
||||
* OperationContext - Unified context for handler operations
|
||||
*
|
||||
* TEMPORARILY DISABLED to avoid circular dependencies during library build
|
||||
* Will be re-enabled once all core libraries are built
|
||||
*/
|
||||
|
||||
import { getLogger, type Logger } from '@stock-bot/logger';
|
||||
|
|
@ -13,25 +10,41 @@ export interface OperationContextOptions {
|
|||
operationName: string;
|
||||
parentLogger?: Logger;
|
||||
container?: ServiceResolver;
|
||||
metadata?: Record<string, any>;
|
||||
traceId?: string;
|
||||
}
|
||||
|
||||
export class OperationContext {
|
||||
public readonly logger: Logger;
|
||||
public readonly traceId: string;
|
||||
public readonly metadata: Record<string, any>;
|
||||
private readonly container?: ServiceResolver;
|
||||
private readonly startTime: Date;
|
||||
|
||||
constructor(options: OperationContextOptions) {
|
||||
this.container = options.container;
|
||||
this.logger = options.parentLogger || getLogger(`${options.handlerName}:${options.operationName}`);
|
||||
this.metadata = options.metadata || {};
|
||||
this.traceId = options.traceId || this.generateTraceId();
|
||||
this.startTime = new Date();
|
||||
|
||||
this.logger = options.parentLogger || getLogger(`${options.handlerName}:${options.operationName}`, {
|
||||
traceId: this.traceId,
|
||||
metadata: this.metadata,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new OperationContext with automatic resource management
|
||||
* TEMPORARILY SIMPLIFIED - full implementation will be restored after build fixes
|
||||
*/
|
||||
static create(
|
||||
handlerName: string,
|
||||
operationName: string,
|
||||
options: { container?: ServiceResolver; parentLogger?: Logger } = {}
|
||||
options: {
|
||||
container?: ServiceResolver;
|
||||
parentLogger?: Logger;
|
||||
metadata?: Record<string, any>;
|
||||
traceId?: string;
|
||||
} = {}
|
||||
): OperationContext {
|
||||
return new OperationContext({
|
||||
handlerName,
|
||||
|
|
@ -41,21 +54,85 @@ export class OperationContext {
|
|||
}
|
||||
|
||||
/**
|
||||
* Cleanup method - simplified for now
|
||||
* Resolve a service from the container
|
||||
*/
|
||||
async dispose(): Promise<void> {
|
||||
// Cleanup will be implemented when dependencies are resolved
|
||||
resolve<T>(serviceName: string): T {
|
||||
if (!this.container) {
|
||||
throw new Error('No service container available');
|
||||
}
|
||||
return this.container.resolve<T>(serviceName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create child context - simplified for now
|
||||
* Resolve a service asynchronously from the container
|
||||
*/
|
||||
createChild(operationName: string): OperationContext {
|
||||
async resolveAsync<T>(serviceName: string): Promise<T> {
|
||||
if (!this.container) {
|
||||
throw new Error('No service container available');
|
||||
}
|
||||
return this.container.resolveAsync<T>(serviceName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add metadata to the context
|
||||
*/
|
||||
addMetadata(key: string, value: any): void {
|
||||
this.metadata[key] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get execution time in milliseconds
|
||||
*/
|
||||
getExecutionTime(): number {
|
||||
return Date.now() - this.startTime.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log operation completion with metrics
|
||||
*/
|
||||
logCompletion(success: boolean, error?: Error): void {
|
||||
const executionTime = this.getExecutionTime();
|
||||
|
||||
if (success) {
|
||||
this.logger.info('Operation completed successfully', {
|
||||
executionTime,
|
||||
metadata: this.metadata,
|
||||
});
|
||||
} else {
|
||||
this.logger.error('Operation failed', {
|
||||
executionTime,
|
||||
error: error?.message,
|
||||
stack: error?.stack,
|
||||
metadata: this.metadata,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup method
|
||||
*/
|
||||
async dispose(): Promise<void> {
|
||||
this.logCompletion(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create child context
|
||||
*/
|
||||
createChild(operationName: string, metadata?: Record<string, any>): OperationContext {
|
||||
return new OperationContext({
|
||||
handlerName: 'child',
|
||||
operationName,
|
||||
parentLogger: this.logger,
|
||||
container: this.container,
|
||||
traceId: this.traceId,
|
||||
metadata: { ...this.metadata, ...metadata },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique trace ID
|
||||
*/
|
||||
private generateTraceId(): string {
|
||||
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
}
|
||||
}
|
||||
|
|
@ -170,8 +170,8 @@ export function createServiceContainer(
|
|||
|
||||
container.register({
|
||||
name: 'cache',
|
||||
factory: () => {
|
||||
const pool = connectionFactory.createCache({
|
||||
factory: async () => {
|
||||
const pool = await connectionFactory.createCache({
|
||||
name: 'default',
|
||||
config: {} as any, // Config injected by factory
|
||||
});
|
||||
|
|
@ -182,8 +182,8 @@ export function createServiceContainer(
|
|||
|
||||
container.register({
|
||||
name: 'queue',
|
||||
factory: () => {
|
||||
const pool = connectionFactory.createQueue({
|
||||
factory: async () => {
|
||||
const pool = await connectionFactory.createQueue({
|
||||
name: 'default',
|
||||
config: {} as any, // Config injected by factory
|
||||
});
|
||||
|
|
@ -192,43 +192,10 @@ export function createServiceContainer(
|
|||
singleton: true,
|
||||
});
|
||||
|
||||
// Optional services - comment out for now to avoid circular dependencies
|
||||
// These can be registered manually by apps that need them
|
||||
|
||||
// // Register ProxyManager
|
||||
// container.register({
|
||||
// name: 'proxyManager',
|
||||
// factory: async () => {
|
||||
// const { ProxyManager } = await import('@stock-bot/utils');
|
||||
// await ProxyManager.initialize();
|
||||
// return ProxyManager.getInstance();
|
||||
// },
|
||||
// singleton: true,
|
||||
// });
|
||||
|
||||
// // Register Browser service
|
||||
// container.register({
|
||||
// name: 'browser',
|
||||
// factory: async () => {
|
||||
// const { Browser } = await import('@stock-bot/browser');
|
||||
// return Browser;
|
||||
// },
|
||||
// singleton: true,
|
||||
// });
|
||||
|
||||
// // Register HttpClient with default configuration
|
||||
// container.register({
|
||||
// name: 'httpClient',
|
||||
// factory: async () => {
|
||||
// const { createHttpClient } = await import('@stock-bot/http');
|
||||
// return createHttpClient({
|
||||
// timeout: 30000,
|
||||
// retries: 3,
|
||||
// userAgent: 'stock-bot/1.0',
|
||||
// });
|
||||
// },
|
||||
// singleton: true,
|
||||
// });
|
||||
// Note: Additional services can be registered by individual applications as needed:
|
||||
// - ProxyManager: container.register({ name: 'proxyManager', factory: () => ProxyManager.getInstance() })
|
||||
// - Browser: container.register({ name: 'browser', factory: () => Browser })
|
||||
// - HttpClient: container.register({ name: 'httpClient', factory: () => createHttpClient(...) })
|
||||
|
||||
return container;
|
||||
}
|
||||
|
|
@ -60,8 +60,8 @@ export interface PoolMetrics {
|
|||
export interface ConnectionFactory {
|
||||
createMongoDB(config: MongoDBPoolConfig): Promise<ConnectionPool<any>>;
|
||||
createPostgreSQL(config: PostgreSQLPoolConfig): Promise<ConnectionPool<any>>;
|
||||
createCache(config: CachePoolConfig): ConnectionPool<any>;
|
||||
createQueue(config: QueuePoolConfig): ConnectionPool<any>;
|
||||
createCache(config: CachePoolConfig): Promise<ConnectionPool<any>>;
|
||||
createQueue(config: QueuePoolConfig): Promise<ConnectionPool<any>>;
|
||||
getPool(type: 'mongodb' | 'postgres' | 'cache' | 'queue', name: string): ConnectionPool<any> | undefined;
|
||||
listPools(): Array<{ type: string; name: string; metrics: PoolMetrics }>;
|
||||
disposeAll(): Promise<void>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue