commit 2 refactor
This commit is contained in:
parent
588f666943
commit
32e43c9243
310 changed files with 33318 additions and 31189 deletions
|
|
@ -11,7 +11,12 @@ import type {
|
|||
HandlerRegistry,
|
||||
} from '@stock-bot/handler-registry';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { ExecutionContext, IHandler } from '@stock-bot/types';
|
||||
import type {
|
||||
DecoratedHandlerClass,
|
||||
ExecutionContext,
|
||||
IHandler,
|
||||
JobHandler,
|
||||
} from '@stock-bot/types';
|
||||
|
||||
export interface HandlerScannerOptions {
|
||||
serviceName?: string;
|
||||
|
|
@ -21,7 +26,7 @@ export interface HandlerScannerOptions {
|
|||
|
||||
export class HandlerScanner {
|
||||
private logger = getLogger('handler-scanner');
|
||||
private discoveredHandlers = new Map<string, any>();
|
||||
private discoveredHandlers = new Map<string, DecoratedHandlerClass>();
|
||||
|
||||
constructor(
|
||||
private registry: HandlerRegistry,
|
||||
|
|
@ -69,7 +74,7 @@ export class HandlerScanner {
|
|||
/**
|
||||
* Register handlers found in a module
|
||||
*/
|
||||
private registerHandlersFromModule(module: any, filePath: string): void {
|
||||
private registerHandlersFromModule(module: Record<string, unknown>, filePath: string): void {
|
||||
for (const [exportName, exported] of Object.entries(module)) {
|
||||
if (this.isHandler(exported)) {
|
||||
this.registerHandler(exported, exportName, filePath);
|
||||
|
|
@ -80,14 +85,14 @@ export class HandlerScanner {
|
|||
/**
|
||||
* Check if an exported value is a handler
|
||||
*/
|
||||
private isHandler(exported: any): boolean {
|
||||
private isHandler(exported: unknown): exported is DecoratedHandlerClass {
|
||||
if (typeof exported !== 'function') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for handler metadata added by decorators
|
||||
const hasHandlerName = !!(exported as any).__handlerName;
|
||||
const hasOperations = Array.isArray((exported as any).__operations);
|
||||
const hasHandlerName = !!(exported as DecoratedHandlerClass).__handlerName;
|
||||
const hasOperations = Array.isArray((exported as DecoratedHandlerClass).__operations);
|
||||
|
||||
return hasHandlerName && hasOperations;
|
||||
}
|
||||
|
|
@ -95,8 +100,15 @@ export class HandlerScanner {
|
|||
/**
|
||||
* Register a handler with the registry and DI container
|
||||
*/
|
||||
private registerHandler(HandlerClass: any, exportName: string, filePath: string): void {
|
||||
private registerHandler(
|
||||
HandlerClass: DecoratedHandlerClass,
|
||||
exportName: string,
|
||||
filePath: string
|
||||
): void {
|
||||
const handlerName = HandlerClass.__handlerName;
|
||||
if (!handlerName) {
|
||||
throw new Error('Handler name is missing');
|
||||
}
|
||||
const operations = HandlerClass.__operations || [];
|
||||
const schedules = HandlerClass.__schedules || [];
|
||||
const isDisabled = HandlerClass.__disabled || false;
|
||||
|
|
@ -110,24 +122,24 @@ export class HandlerScanner {
|
|||
}
|
||||
|
||||
// Filter out disabled operations
|
||||
const enabledOperations = operations.filter((op: any) => {
|
||||
const enabledOperations = operations.filter(op => {
|
||||
const isDisabled = disabledOperations.includes(op.method);
|
||||
if (isDisabled) {
|
||||
this.logger.debug('Skipping disabled operation', {
|
||||
handlerName,
|
||||
this.logger.debug('Skipping disabled operation', {
|
||||
handlerName,
|
||||
operation: op.name,
|
||||
method: op.method
|
||||
method: op.method,
|
||||
});
|
||||
}
|
||||
return !isDisabled;
|
||||
});
|
||||
|
||||
const enabledSchedules = schedules.filter((schedule: any) => {
|
||||
const enabledSchedules = schedules.filter(schedule => {
|
||||
const isDisabled = disabledOperations.includes(schedule.operation);
|
||||
if (isDisabled) {
|
||||
this.logger.debug('Skipping disabled scheduled operation', {
|
||||
handlerName,
|
||||
operation: schedule.operation
|
||||
this.logger.debug('Skipping disabled scheduled operation', {
|
||||
handlerName,
|
||||
operation: schedule.operation,
|
||||
});
|
||||
}
|
||||
return !isDisabled;
|
||||
|
|
@ -137,12 +149,12 @@ export class HandlerScanner {
|
|||
const metadata: HandlerMetadata = {
|
||||
name: handlerName,
|
||||
service: this.options.serviceName,
|
||||
operations: enabledOperations.map((op: any) => ({
|
||||
operations: enabledOperations.map(op => ({
|
||||
name: op.name,
|
||||
method: op.method,
|
||||
rateLimit: operationRateLimits[op.method] || null,
|
||||
})),
|
||||
schedules: enabledSchedules.map((schedule: any) => ({
|
||||
schedules: enabledSchedules.map(schedule => ({
|
||||
operation: schedule.operation,
|
||||
cronPattern: schedule.cronPattern,
|
||||
priority: schedule.priority,
|
||||
|
|
@ -155,9 +167,9 @@ export class HandlerScanner {
|
|||
};
|
||||
|
||||
// Build configuration with operation handlers
|
||||
const operationHandlers: Record<string, any> = {};
|
||||
const operationHandlers: Record<string, JobHandler> = {};
|
||||
for (const op of enabledOperations) {
|
||||
operationHandlers[op.name] = async (payload: any) => {
|
||||
operationHandlers[op.name] = async (payload: unknown) => {
|
||||
const handler = this.container.resolve<IHandler>(handlerName);
|
||||
const context: ExecutionContext = {
|
||||
type: 'queue',
|
||||
|
|
@ -170,8 +182,8 @@ export class HandlerScanner {
|
|||
const configuration: HandlerConfiguration = {
|
||||
name: handlerName,
|
||||
operations: operationHandlers,
|
||||
scheduledJobs: enabledSchedules.map((schedule: any) => {
|
||||
const operation = enabledOperations.find((op: any) => op.method === schedule.operation);
|
||||
scheduledJobs: enabledSchedules.map(schedule => {
|
||||
const operation = enabledOperations.find(op => op.method === schedule.operation);
|
||||
return {
|
||||
type: `${handlerName}-${schedule.operation}`,
|
||||
operation: operation?.name || schedule.operation,
|
||||
|
|
@ -192,7 +204,7 @@ export class HandlerScanner {
|
|||
if (this.options.autoRegister !== false) {
|
||||
this.container.register({
|
||||
[handlerName]: asFunction(({ serviceContainer }) => {
|
||||
return new HandlerClass(serviceContainer);
|
||||
return Reflect.construct(HandlerClass, [serviceContainer]) as IHandler;
|
||||
}).singleton(),
|
||||
});
|
||||
}
|
||||
|
|
@ -214,14 +226,17 @@ export class HandlerScanner {
|
|||
/**
|
||||
* Get all discovered handlers
|
||||
*/
|
||||
getDiscoveredHandlers(): Map<string, any> {
|
||||
getDiscoveredHandlers(): Map<string, DecoratedHandlerClass> {
|
||||
return new Map(this.discoveredHandlers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually register a handler class
|
||||
*/
|
||||
registerHandlerClass(HandlerClass: any, options: { serviceName?: string } = {}): void {
|
||||
registerHandlerClass(
|
||||
HandlerClass: DecoratedHandlerClass,
|
||||
options: { serviceName?: string } = {}
|
||||
): void {
|
||||
const serviceName = options.serviceName || this.options.serviceName;
|
||||
const originalServiceName = this.options.serviceName;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue