huge refactor done

This commit is contained in:
Boki 2025-06-24 11:59:35 -04:00
parent 843a7b9b9b
commit 60d7de1da8
16 changed files with 472 additions and 443 deletions

View file

@ -1,4 +1,3 @@
import type { Collection } from 'mongodb';
import { createNamespacedCache } from '@stock-bot/cache';
import { getLogger } from '@stock-bot/logger';
import type {
@ -11,6 +10,7 @@ import type {
ServiceTypes,
} from '@stock-bot/types';
import { fetch } from '@stock-bot/utils';
import type { Collection } from 'mongodb';
// Handler registry is now injected, not imported
import { createJobHandler } from '../utils/create-job-handler';
@ -45,7 +45,8 @@ export abstract class BaseHandler implements IHandler {
readonly logger: ServiceTypes['logger'];
readonly cache: ServiceTypes['cache'];
readonly globalCache: ServiceTypes['globalCache'];
readonly queue: ServiceTypes['queue'];
readonly queueManager: ServiceTypes['queueManager'];
readonly queue: ServiceTypes['queue']; // Specific queue for this handler
readonly proxy: ServiceTypes['proxy'];
readonly browser: ServiceTypes['browser'];
readonly mongodb: ServiceTypes['mongodb'];
@ -55,21 +56,26 @@ export abstract class BaseHandler implements IHandler {
private handlerName: string;
constructor(services: IServiceContainer, handlerName?: string) {
// 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();
// Flatten all services onto the handler instance
this.logger = getLogger(this.constructor.name);
this.cache = services.cache;
this.globalCache = services.globalCache;
this.queue = services.queue;
this.queueManager = services.queueManager;
this.proxy = services.proxy;
this.browser = services.browser;
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();
// Get the specific queue for this handler
if (this.queueManager) {
this.queue = this.queueManager.getQueue(this.handlerName);
}
}
/**
@ -117,16 +123,15 @@ export abstract class BaseHandler implements IHandler {
options?: JobScheduleOptions
): Promise<void> {
if (!this.queue) {
throw new Error('Queue service is not available');
throw new Error('Queue service is not available for this handler');
}
const queue = this.queue.getQueue(this.handlerName);
const jobData = {
handler: this.handlerName,
operation,
payload,
};
await queue.add(operation, jobData, options || {});
await this.queue.add(operation, jobData, options || {});
}
/**
@ -331,7 +336,9 @@ export abstract class BaseHandler implements IHandler {
static extractMetadata(): HandlerMetadata | null {
const constructor = this as any;
const handlerName = constructor.__handlerName;
if (!handlerName) return null;
if (!handlerName){
return null;
}
const operations = constructor.__operations || [];
const schedules = constructor.__schedules || [];