refactored handler

This commit is contained in:
Boki 2025-06-22 07:17:21 -04:00
parent 62a2f15dab
commit 8b17f98845
2 changed files with 24 additions and 14 deletions

View file

@ -8,24 +8,34 @@ import type { ExecutionContext, IHandler } from '../types/types';
* Provides common functionality and structure for queue/event operations
*/
export abstract class BaseHandler implements IHandler {
protected readonly logger;
// Direct service properties - flattened for cleaner access
readonly logger;
readonly cache;
readonly queue;
readonly http;
readonly proxy;
readonly mongodb;
readonly postgres;
readonly questdb;
private handlerName: string;
constructor(readonly services: IServiceContainer, handlerName?: string) {
constructor(services: IServiceContainer, handlerName?: string) {
// Flatten all services onto the handler instance
this.logger = getLogger(this.constructor.name);
this.cache = services.cache;
this.queue = services.queue;
this.http = services.http;
this.proxy = services.proxy;
this.mongodb = services.mongodb;
this.postgres = services.postgres;
this.questdb = services.questdb;
// Read handler name from decorator first, then fallback to parameter or class name
const constructor = this.constructor as any;
this.handlerName = constructor.__handlerName || handlerName || this.constructor.name.toLowerCase();
}
// Convenience getters for common services
protected get mongodb() { return this.services.mongodb; }
protected get postgres() { return this.services.postgres; }
protected get questdb() { return this.services.questdb; }
protected get cache() { return this.services.cache; }
protected get queue() { return this.services.queue; }
protected get http() { return this.services.http; }
/**
* Main execution method - automatically routes to decorated methods
* Works with queue (events commented for future)
@ -66,7 +76,7 @@ export abstract class BaseHandler implements IHandler {
}
async scheduleOperation(operation: string, payload: unknown, delay?: number): Promise<void> {
const queue = this.services.queue.getQueue(this.handlerName);
const queue = this.queue.getQueue(this.handlerName);
const jobData = {
handler: this.handlerName,
operation,