commit 2 refactor
This commit is contained in:
parent
588f666943
commit
32e43c9243
310 changed files with 33318 additions and 31189 deletions
|
|
@ -1,15 +1,17 @@
|
|||
import type { Collection, Document } from 'mongodb';
|
||||
import { createNamespacedCache } from '@stock-bot/cache';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { IServiceContainer, ServiceTypes } from '@stock-bot/services';
|
||||
import type {
|
||||
DecoratedHandlerClass,
|
||||
ExecutionContext,
|
||||
HandlerConfigWithSchedule,
|
||||
HandlerMetadata,
|
||||
IHandler,
|
||||
JobHandler,
|
||||
} from '@stock-bot/types';
|
||||
import type { IServiceContainer, ServiceTypes } from '@stock-bot/services';
|
||||
import type { FetchOptions } from '@stock-bot/utils';
|
||||
import { fetch } from '@stock-bot/utils';
|
||||
import type { Collection, Document } from 'mongodb';
|
||||
// Handler registry is now injected, not imported
|
||||
import { createJobHandler } from '../utils/create-job-handler';
|
||||
|
||||
|
|
@ -40,7 +42,9 @@ export interface JobScheduleOptions {
|
|||
* Abstract base class for all handlers with improved DI
|
||||
* Provides common functionality and structure for queue/event operations
|
||||
*/
|
||||
export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes> implements IHandler {
|
||||
export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
||||
implements IHandler
|
||||
{
|
||||
// Direct service properties - flattened for cleaner access with proper types
|
||||
readonly logger: TServices['logger'];
|
||||
readonly cache: TServices['cache'];
|
||||
|
|
@ -57,7 +61,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
|
||||
constructor(services: TServices, handlerName?: string) {
|
||||
// Read handler name from decorator first, then fallback to parameter or class name
|
||||
const constructor = this.constructor as any;
|
||||
const constructor = this.constructor as DecoratedHandlerClass;
|
||||
this.handlerName =
|
||||
constructor.__handlerName || handlerName || this.constructor.name.toLowerCase();
|
||||
|
||||
|
|
@ -83,28 +87,28 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
* Works with queue (events commented for future)
|
||||
*/
|
||||
async execute(operation: string, input: unknown, context: ExecutionContext): Promise<unknown> {
|
||||
const constructor = this.constructor as any;
|
||||
const constructor = this.constructor as DecoratedHandlerClass;
|
||||
const operations = constructor.__operations || [];
|
||||
|
||||
// Debug logging
|
||||
this.logger.debug('Handler execute called', {
|
||||
handler: this.handlerName,
|
||||
operation,
|
||||
availableOperations: operations.map((op: any) => ({ name: op.name, method: op.method })),
|
||||
availableOperations: operations.map(op => ({ name: op.name, method: op.method })),
|
||||
});
|
||||
|
||||
// Find the operation metadata
|
||||
const operationMeta = operations.find((op: any) => op.name === operation);
|
||||
const operationMeta = operations.find(op => op.name === operation);
|
||||
if (!operationMeta) {
|
||||
this.logger.error('Operation not found', {
|
||||
requestedOperation: operation,
|
||||
availableOperations: operations.map((op: any) => op.name),
|
||||
availableOperations: operations.map(op => op.name),
|
||||
});
|
||||
throw new Error(`Unknown operation: ${operation}`);
|
||||
}
|
||||
|
||||
// Get the method from the instance and call it
|
||||
const method = (this as any)[operationMeta.method];
|
||||
const method: unknown = Reflect.get(this, operationMeta.method);
|
||||
if (typeof method !== 'function') {
|
||||
throw new Error(`Operation method '${operationMeta.method}' not found on handler`);
|
||||
}
|
||||
|
|
@ -125,7 +129,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
if (!this.queue) {
|
||||
throw new Error('Queue service is not available for this handler');
|
||||
}
|
||||
|
||||
|
||||
const jobData = {
|
||||
handler: this.handlerName,
|
||||
operation,
|
||||
|
|
@ -140,7 +144,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
*/
|
||||
protected createExecutionContext(
|
||||
type: 'http' | 'queue' | 'scheduled',
|
||||
metadata: Record<string, any> = {}
|
||||
metadata: Record<string, unknown> = {}
|
||||
): ExecutionContext {
|
||||
return {
|
||||
type,
|
||||
|
|
@ -177,18 +181,18 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
/**
|
||||
* Set cache with handler-prefixed key
|
||||
*/
|
||||
protected async cacheSet(key: string, value: any, ttl?: number): Promise<void> {
|
||||
protected async cacheSet(key: string, value: unknown, ttl?: number): Promise<void> {
|
||||
if (!this.cache) {
|
||||
return;
|
||||
}
|
||||
// Don't add 'cache:' prefix since the cache already has its own prefix
|
||||
return this.cache.set(`${this.handlerName}:${key}`, value, ttl);
|
||||
await this.cache.set(`${this.handlerName}:${key}`, value, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache with handler-prefixed key
|
||||
*/
|
||||
protected async cacheGet<T = any>(key: string): Promise<T | null> {
|
||||
protected async cacheGet<T = unknown>(key: string): Promise<T | null> {
|
||||
if (!this.cache) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -210,17 +214,17 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
/**
|
||||
* Set global cache with key
|
||||
*/
|
||||
protected async globalCacheSet(key: string, value: any, ttl?: number): Promise<void> {
|
||||
protected async globalCacheSet(key: string, value: unknown, ttl?: number): Promise<void> {
|
||||
if (!this.globalCache) {
|
||||
return;
|
||||
}
|
||||
return this.globalCache.set(key, value, ttl);
|
||||
await this.globalCache.set(key, value, ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get global cache with key
|
||||
*/
|
||||
protected async globalCacheGet<T = any>(key: string): Promise<T | null> {
|
||||
protected async globalCacheGet<T = unknown>(key: string): Promise<T | null> {
|
||||
if (!this.globalCache) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -254,7 +258,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
/**
|
||||
* Log with handler context
|
||||
*/
|
||||
protected log(level: 'info' | 'warn' | 'error' | 'debug', message: string, meta?: any): void {
|
||||
protected log(level: 'info' | 'warn' | 'error' | 'debug', message: string, meta?: object): void {
|
||||
this.logger[level](message, { handler: this.handlerName, ...meta });
|
||||
}
|
||||
|
||||
|
|
@ -263,9 +267,9 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
*/
|
||||
protected get http() {
|
||||
return {
|
||||
get: (url: string, options?: any) =>
|
||||
get: (url: string, options?: FetchOptions) =>
|
||||
fetch(url, { ...options, method: 'GET', logger: this.logger }),
|
||||
post: (url: string, data?: any, options?: any) =>
|
||||
post: (url: string, data?: unknown, options?: FetchOptions) =>
|
||||
fetch(url, {
|
||||
...options,
|
||||
method: 'POST',
|
||||
|
|
@ -273,7 +277,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
headers: { 'Content-Type': 'application/json', ...options?.headers },
|
||||
logger: this.logger,
|
||||
}),
|
||||
put: (url: string, data?: any, options?: any) =>
|
||||
put: (url: string, data?: unknown, options?: FetchOptions) =>
|
||||
fetch(url, {
|
||||
...options,
|
||||
method: 'PUT',
|
||||
|
|
@ -281,7 +285,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
headers: { 'Content-Type': 'application/json', ...options?.headers },
|
||||
logger: this.logger,
|
||||
}),
|
||||
delete: (url: string, options?: any) =>
|
||||
delete: (url: string, options?: FetchOptions) =>
|
||||
fetch(url, { ...options, method: 'DELETE', logger: this.logger }),
|
||||
};
|
||||
}
|
||||
|
|
@ -314,7 +318,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
|
||||
const operationHandlers: Record<string, JobHandler> = {};
|
||||
for (const opName of metadata.operations) {
|
||||
operationHandlers[opName] = createJobHandler(async (payload: any) => {
|
||||
operationHandlers[opName] = createJobHandler(async (payload: unknown) => {
|
||||
const context: ExecutionContext = {
|
||||
type: 'queue',
|
||||
metadata: { source: 'queue', timestamp: Date.now() },
|
||||
|
|
@ -335,7 +339,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
* This returns metadata only - actual handler instances are created by the scanner
|
||||
*/
|
||||
static extractMetadata(): HandlerMetadata | null {
|
||||
const constructor = this as any;
|
||||
const constructor = this as DecoratedHandlerClass;
|
||||
const handlerName = constructor.__handlerName;
|
||||
if (!handlerName) {
|
||||
return null;
|
||||
|
|
@ -345,9 +349,9 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
const schedules = constructor.__schedules || [];
|
||||
|
||||
// Create scheduled jobs from decorator metadata
|
||||
const scheduledJobs = schedules.map((schedule: any) => {
|
||||
const scheduledJobs = schedules.map(schedule => {
|
||||
// Find the operation name from the method name
|
||||
const operation = operations.find((op: any) => op.method === schedule.operation);
|
||||
const operation = operations.find(op => op.method === schedule.operation);
|
||||
return {
|
||||
type: `${handlerName}-${schedule.operation}`,
|
||||
operation: operation?.name || schedule.operation,
|
||||
|
|
@ -362,7 +366,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
|
||||
return {
|
||||
name: handlerName,
|
||||
operations: operations.map((op: any) => op.name),
|
||||
operations: operations.map(op => op.name),
|
||||
scheduledJobs,
|
||||
description: constructor.__description,
|
||||
};
|
||||
|
|
@ -373,7 +377,7 @@ export abstract class BaseHandler<TServices extends ServiceTypes = ServiceTypes>
|
|||
* @param operation The operation name that needs a payload
|
||||
* @returns The payload for the scheduled job, or undefined
|
||||
*/
|
||||
protected getScheduledJobPayload?(operation: string): any;
|
||||
protected getScheduledJobPayload?(operation: string): unknown;
|
||||
|
||||
/**
|
||||
* Lifecycle hooks - can be overridden by subclasses
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue