69 lines
No EOL
2.1 KiB
TypeScript
69 lines
No EOL
2.1 KiB
TypeScript
import type { ServiceContainer } from '@stock-bot/di';
|
|
import { getLogger } from '@stock-bot/logger';
|
|
import type { IHandler, ExecutionContext } from '../types/types';
|
|
|
|
/**
|
|
* Abstract base class for all handlers
|
|
* Provides common functionality and structure for queue/event operations
|
|
*/
|
|
export abstract class BaseHandler implements IHandler {
|
|
protected readonly logger;
|
|
|
|
constructor(protected readonly container: ServiceContainer) {
|
|
this.logger = getLogger(this.constructor.name);
|
|
}
|
|
|
|
/**
|
|
* Main execution method - must be implemented by subclasses
|
|
* Works with queue (events commented for future)
|
|
*/
|
|
abstract execute(operation: string, input: unknown, context: ExecutionContext): Promise<unknown>;
|
|
|
|
/**
|
|
* Queue helper methods
|
|
*/
|
|
protected async scheduleOperation(operation: string, payload: unknown, delay?: number): Promise<void> {
|
|
const queue = await this.container.resolveAsync('queue');
|
|
await queue.add(operation, payload, { delay });
|
|
}
|
|
|
|
/**
|
|
* Get a service from the container
|
|
*/
|
|
protected async getService<T>(serviceName: string): Promise<T> {
|
|
return await this.container.resolveAsync<T>(serviceName);
|
|
}
|
|
|
|
/**
|
|
* Event methods - commented for future
|
|
*/
|
|
// protected async publishEvent(eventName: string, payload: unknown): Promise<void> {
|
|
// const eventBus = await this.container.resolveAsync('eventBus');
|
|
// await eventBus.publish(eventName, payload);
|
|
// }
|
|
|
|
/**
|
|
* Lifecycle hooks - can be overridden by subclasses
|
|
*/
|
|
async onInit?(): Promise<void>;
|
|
async onStart?(): Promise<void>;
|
|
async onStop?(): Promise<void>;
|
|
async onDispose?(): Promise<void>;
|
|
}
|
|
|
|
/**
|
|
* Specialized handler for operations that have scheduled jobs
|
|
*/
|
|
export abstract class ScheduledHandler extends BaseHandler {
|
|
/**
|
|
* Get scheduled job configurations for this handler
|
|
* Override in subclasses to define schedules
|
|
*/
|
|
getScheduledJobs?(): Array<{
|
|
operation: string;
|
|
cronPattern: string;
|
|
priority?: number;
|
|
immediately?: boolean;
|
|
description?: string;
|
|
}>;
|
|
} |