libs ready i think

This commit is contained in:
Boki 2025-06-21 19:15:58 -04:00
parent 1b34da9a69
commit 9673ae70ef
9 changed files with 242 additions and 129 deletions

View file

@ -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)}`;
}
}