updated di
This commit is contained in:
parent
3227388d25
commit
c5a114d544
9 changed files with 545 additions and 306 deletions
|
|
@ -1,18 +1,24 @@
|
|||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { ServiceContainer } from '@stock-bot/di';
|
||||
import type { IDataIngestionServices, IExecutionContext } from '@stock-bot/di';
|
||||
import type { IHandler, ExecutionContext } from '../types/types';
|
||||
|
||||
/**
|
||||
* Abstract base class for all handlers
|
||||
* Abstract base class for all handlers with improved DI
|
||||
* Provides common functionality and structure for queue/event operations
|
||||
*/
|
||||
export abstract class BaseHandler implements IHandler {
|
||||
protected readonly logger;
|
||||
|
||||
constructor(protected readonly container: ServiceContainer) {
|
||||
constructor(protected readonly services: IDataIngestionServices) {
|
||||
this.logger = getLogger(this.constructor.name);
|
||||
}
|
||||
|
||||
// Convenience getters for common services
|
||||
protected get mongodb() { return this.services.mongodb; }
|
||||
protected get postgres() { return this.services.postgres; }
|
||||
protected get cache() { return this.services.cache; }
|
||||
protected get queue() { return this.services.queue; }
|
||||
|
||||
/**
|
||||
* Main execution method - must be implemented by subclasses
|
||||
* Works with queue (events commented for future)
|
||||
|
|
@ -20,18 +26,28 @@ export abstract class BaseHandler implements IHandler {
|
|||
abstract execute(operation: string, input: unknown, context: ExecutionContext): Promise<unknown>;
|
||||
|
||||
/**
|
||||
* Queue helper methods
|
||||
* Queue helper methods - now type-safe and direct
|
||||
*/
|
||||
protected async scheduleOperation(operation: string, payload: unknown, delay?: number): Promise<void> {
|
||||
const queue = await this.container.resolveAsync('queue') as any;
|
||||
await queue.add(operation, payload, { delay });
|
||||
const queue = this.services.queue.getQueue(this.constructor.name.toLowerCase());
|
||||
const jobData = {
|
||||
handler: this.constructor.name.toLowerCase(),
|
||||
operation,
|
||||
payload
|
||||
};
|
||||
await queue.add(operation, jobData, { delay });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a service from the container
|
||||
* Create execution context for operations
|
||||
*/
|
||||
protected async getService<T>(serviceName: string): Promise<T> {
|
||||
return await this.container.resolveAsync(serviceName);
|
||||
protected createExecutionContext(type: 'http' | 'queue' | 'scheduled', metadata: Record<string, any> = {}): IExecutionContext {
|
||||
return {
|
||||
type,
|
||||
services: this.services,
|
||||
metadata,
|
||||
traceId: `${this.constructor.name}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue